]> git.lyx.org Git - lyx.git/blob - src/Language.cpp
- Coding style
[lyx.git] / src / Language.cpp
1 /**
2  * \file Language.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Jürgen Spitzmüller
9  * \author Dekel Tsur
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Language.h"
17
18 #include "Encoding.h"
19 #include "Lexer.h"
20 #include "LyXRC.h"
21
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/Messages.h"
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33 Languages languages;
34 Language ignore_lang;
35 Language latex_lang;
36 Language const * default_language;
37 Language const * ignore_language = &ignore_lang;
38 Language const * latex_language = &latex_lang;
39 Language const * reset_language = 0;
40
41
42 docstring const Language::translateLayout(string const & m) const
43 {
44         if (m.empty())
45                 return docstring();
46
47         if (!isAscii(m)) {
48                 lyxerr << "Warning: not translating `" << m
49                        << "' because it is not pure ASCII.\n";
50                 return from_utf8(m);
51         }
52
53         TranslationMap::const_iterator it = layoutTranslations_.find(m);
54         if (it != layoutTranslations_.end())
55                 return it->second;
56
57         return from_ascii(m);
58 }
59
60
61 bool Language::readLanguage(Lexer & lex)
62 {
63         enum LanguageTags {
64                 LA_AS_BABELOPTS = 1,
65                 LA_BABELNAME,
66                 LA_ENCODING,
67                 LA_END,
68                 LA_GUINAME,
69                 LA_INTERNAL_ENC,
70                 LA_LANG_CODE,
71                 LA_LANG_VARIETY,
72                 LA_POLYGLOSSIANAME,
73                 LA_POLYGLOSSIAOPTS,
74                 LA_POSTBABELPREAMBLE,
75                 LA_PREBABELPREAMBLE,
76                 LA_RTL
77         };
78
79         // Keep these sorted alphabetically!
80         LexerKeyword languageTags[] = {
81                 { "asbabeloptions",       LA_AS_BABELOPTS },
82                 { "babelname",            LA_BABELNAME },
83                 { "encoding",             LA_ENCODING },
84                 { "end",                  LA_END },
85                 { "guiname",              LA_GUINAME },
86                 { "internalencoding",     LA_INTERNAL_ENC },
87                 { "langcode",             LA_LANG_CODE },
88                 { "langvariety",          LA_LANG_VARIETY },
89                 { "polyglossianame",      LA_POLYGLOSSIANAME },
90                 { "polyglossiaopts",      LA_POLYGLOSSIAOPTS },
91                 { "postbabelpreamble",    LA_POSTBABELPREAMBLE },
92                 { "prebabelpreamble",     LA_PREBABELPREAMBLE },
93                 { "rtl",                  LA_RTL }
94         };
95
96         bool error = false;
97         bool finished = false;
98         lex.pushTable(languageTags);
99         // parse style section
100         while (!finished && lex.isOK() && !error) {
101                 int le = lex.lex();
102                 // See comment in LyXRC.cpp.
103                 switch (le) {
104                 case Lexer::LEX_FEOF:
105                         continue;
106
107                 case Lexer::LEX_UNDEF: // parse error
108                         lex.printError("Unknown language tag `$$Token'");
109                         error = true;
110                         continue;
111
112                 default: 
113                         break;
114                 }
115                 switch (static_cast<LanguageTags>(le)) {
116                 case LA_END: // end of structure
117                         finished = true;
118                         break;
119                 case LA_AS_BABELOPTS:
120                         lex >> as_babel_options_;
121                         break;
122                 case LA_BABELNAME:
123                         lex >> babel_;
124                         break;
125                 case LA_POLYGLOSSIANAME:
126                         lex >> polyglossia_name_;
127                         break;
128                 case LA_POLYGLOSSIAOPTS:
129                         lex >> polyglossia_opts_;
130                         break;
131                 case LA_ENCODING:
132                         lex >> encodingStr_;
133                         break;
134                 case LA_GUINAME:
135                         lex >> display_;
136                         break;
137                 case LA_INTERNAL_ENC:
138                         lex >> internal_enc_;
139                         break;
140                 case LA_LANG_CODE:
141                         lex >> code_;
142                         break;
143                 case LA_LANG_VARIETY:
144                         lex >> variety_;
145                         break;
146                 case LA_POSTBABELPREAMBLE:
147                         babel_postsettings_ =
148                                 lex.getLongString("EndPostBabelPreamble");
149                         break;
150                 case LA_PREBABELPREAMBLE:
151                         babel_presettings_ =
152                                 lex.getLongString("EndPreBabelPreamble");
153                         break;
154                 case LA_RTL:
155                         lex >> rightToLeft_;
156                         break;
157                 }
158         }
159         lex.popTable();
160         return finished && !error;
161 }
162
163
164 bool Language::read(Lexer & lex)
165 {
166         as_babel_options_ = 0;
167         encoding_ = 0;
168         internal_enc_ = 0;
169         rightToLeft_ = 0;
170
171         if (!lex.next()) {
172                 lex.printError("No name given for language: `$$Token'.");
173                 return false;
174         }
175
176         lang_ = lex.getString();
177         LYXERR(Debug::INFO, "Reading language " << lang_);
178         if (!readLanguage(lex)) {
179                 LYXERR0("Error parsing language `" << lang_ << '\'');
180                 return false;
181         }
182
183         encoding_ = encodings.fromLyXName(encodingStr_);
184         if (!encoding_ && !encodingStr_.empty()) {
185                 encoding_ = encodings.fromLyXName("iso8859-1");
186                 LYXERR0("Unknown encoding " << encodingStr_);
187         }
188         // cache translation status. Calling getMessages() directly in
189         // PrefLanguage::PrefLanguage() did only work if the gui language
190         // was set to auto (otherwise all languages would be marked as available).
191         translated_ = getMessages(code()).available();
192         return true;
193 }
194
195
196 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
197 {
198         TranslationMap::const_iterator const end = trans.end();
199         for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
200                 if (replace
201                         || layoutTranslations_.find(it->first) == layoutTranslations_.end())
202                         layoutTranslations_[it->first] = it->second;
203         }
204 }
205
206
207 void Languages::read(FileName const & filename)
208 {
209         Lexer lex;
210         lex.setFile(filename);
211         lex.setContext("Languages::read");
212         while (lex.isOK()) {
213                 int le = lex.lex();
214                 switch (le) {
215                 case Lexer::LEX_FEOF:
216                         continue;
217
218                 default:
219                         break;
220                 }
221                 if (lex.getString() != "Language") {
222                         lex.printError("Unknown Language tag `$$Token'");
223                         continue;
224                 }
225                 Language l;
226                 l.read(lex);
227                 if (!lex)
228                         break;
229                 if (l.lang() == "latex")
230                         latex_lang = l;
231                 else if (l.lang() == "ignore")
232                         ignore_lang = l;
233                 else
234                         languagelist[l.lang()] = l;
235         }
236
237         default_language = getLanguage(lyxrc.default_language);
238         if (!default_language) {
239                 LYXERR0("Default language \"" << lyxrc.default_language
240                        << "\" not found!");
241                 default_language = getLanguage("english");
242                 if (!default_language)
243                         default_language = &(*languagelist.begin()).second;
244                 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
245         }
246
247         // Read layout translations
248         FileName const path = libFileSearch(string(), "layouttranslations");
249         readLayoutTranslations(path);
250 }
251
252
253 namespace {
254
255 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
256 {
257         while (lex.isOK()) {
258                 if (lex.checkFor("End"))
259                         break;
260                 if (!lex.next(true))
261                         return false;
262                 string const key = lex.getString();
263                 if (!lex.next(true))
264                         return false;
265                 docstring const val = lex.getDocString();
266                 trans[key] = val;
267         }
268         return true;
269 }
270
271
272 enum Match {
273         NoMatch,
274         ApproximateMatch,
275         ExactMatch
276 };
277
278
279 Match match(string const & code, Language const & lang)
280 {
281         // we need to mimic gettext: code can be a two-letter code, which
282         // should match all variants, e.g. "de" should match "de_DE",
283         // "de_AT" etc.
284         // special case for chinese:
285         // simplified  => code == "zh_CN", langcode == "zh_CN"
286         // traditional => code == "zh_TW", langcode == "zh_CN"
287         string const variety = lang.variety();
288         string const langcode = variety.empty() ?
289                                 lang.code() : lang.code() + '_' + variety;
290         string const name = lang.lang();
291         if ((code == langcode && name != "chinese-traditional")
292                 || (code == "zh_TW"  && name == "chinese-traditional"))
293                 return ExactMatch;
294         if ((code.size() == 2) && (langcode.size() > 2)
295                 && (code + '_' == langcode.substr(0, 3)))
296                 return ApproximateMatch;
297         return NoMatch;
298 }
299
300 }
301
302
303 void Languages::readLayoutTranslations(support::FileName const & filename)
304 {
305         Lexer lex;
306         lex.setFile(filename);
307         lex.setContext("Languages::read");
308
309         // 1) read all translations (exact and approximate matches) into trans
310         typedef std::map<string, Language::TranslationMap> TransMap;
311         TransMap trans;
312         LanguageList::iterator const lbeg = languagelist.begin();
313         LanguageList::iterator const lend = languagelist.end();
314         while (lex.isOK()) {
315                 if (!lex.checkFor("Translation")) {
316                         if (lex.isOK())
317                                 lex.printError("Unknown layout translation tag `$$Token'");
318                         break;
319                 }
320                 if (!lex.next(true))
321                         break;
322                 string const code = lex.getString();
323                 bool readit = false;
324                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
325                         if (match(code, lit->second) != NoMatch) {
326                                 if (readTranslations(lex, trans[code]))
327                                         readit = true;
328                                 else
329                                         lex.printError("Could not read layout "
330                                                        "translations for language "
331                                                        "`" + code + "'");
332                                 break;
333                         }
334                 }
335                 if (!readit) {
336                         lex.printError("Unknown language `" + code + "'");
337                         break;
338                 }
339         }
340
341         // 2) merge all translations into the languages
342         // exact translations overwrite approximate ones
343         TransMap::const_iterator const tbeg = trans.begin();
344         TransMap::const_iterator const tend = trans.end();
345         for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
346                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
347                         Match m = match(tit->first, lit->second);
348                         if (m == NoMatch)
349                                 continue;
350                         lit->second.readLayoutTranslations(tit->second,
351                                                            m == ExactMatch);
352                 }
353         }
354
355 }
356
357
358 Language const * Languages::getLanguage(string const & language) const
359 {
360         if (language == "reset")
361                 return reset_language;
362         if (language == "ignore")
363                 return ignore_language;
364         const_iterator it = languagelist.find(language);
365         return it == languagelist.end() ? reset_language : &it->second;
366 }
367
368
369 } // namespace lyx