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