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