]> git.lyx.org Git - lyx.git/blob - src/Language.cpp
Move definition out of 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 "LaTeXFonts.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Messages.h"
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 Languages languages;
36 Language const * ignore_language = 0;
37 Language const * default_language = 0;
38 Language const * latex_language = 0;
39 Language const * reset_language = 0;
40
41
42 bool Language::isPolyglossiaExclusive() const
43 {
44         return babel().empty() && !polyglossia().empty() && requires().empty();
45 }
46
47
48 bool Language::isBabelExclusive() const
49 {
50         return !babel().empty() && polyglossia().empty() && requires().empty();
51 }
52
53
54 docstring const Language::translateLayout(string const & m) const
55 {
56         if (m.empty())
57                 return docstring();
58
59         if (!isAscii(m)) {
60                 lyxerr << "Warning: not translating `" << m
61                        << "' because it is not pure ASCII.\n";
62                 return from_utf8(m);
63         }
64
65         TranslationMap::const_iterator it = layoutTranslations_.find(m);
66         if (it != layoutTranslations_.end())
67                 return it->second;
68
69         docstring t = from_ascii(m);
70         cleanTranslation(t);
71         return t;
72 }
73
74
75 string Language::fontenc(BufferParams const & params) const
76 {
77         // Determine optimal font encoding
78         // We check whether the used rm font supports an encoding our language supports
79         LaTeXFont const & lf =
80                 theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
81         vector<string> const lfe = lf.fontencs();
82         for (auto & fe : fontenc_) {
83                 // ASCII means: support all T* encodings plus OT1
84                 if (fe == "ASCII") {
85                         for (auto & afe : lfe) {
86                                 if (afe == "OT1" || prefixIs(afe, "T"))
87                                         // we found a suitable one; return that.
88                                         return afe;
89                         }
90                 }
91                 // For other encodings, just check whether the font supports it
92                 if (lf.hasFontenc(fe))
93                         return fe;
94         }
95         // We did not find a suitable one; just take the first in the list,
96         // the priorized one (which is "T1" for ASCII).
97         return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
98 }
99
100
101 bool Language::readLanguage(Lexer & lex)
102 {
103         enum LanguageTags {
104                 LA_AS_BABELOPTS = 1,
105                 LA_BABELNAME,
106                 LA_ENCODING,
107                 LA_END,
108                 LA_FONTENC,
109                 LA_GUINAME,
110                 LA_HAS_GUI_SUPPORT,
111                 LA_INTERNAL_ENC,
112                 LA_LANG_CODE,
113                 LA_LANG_VARIETY,
114                 LA_POLYGLOSSIANAME,
115                 LA_POLYGLOSSIAOPTS,
116                 LA_POSTBABELPREAMBLE,
117                 LA_PREBABELPREAMBLE,
118                 LA_PROVIDES,
119                 LA_REQUIRES,
120                 LA_QUOTESTYLE,
121                 LA_RTL
122         };
123
124         // Keep these sorted alphabetically!
125         LexerKeyword languageTags[] = {
126                 { "asbabeloptions",       LA_AS_BABELOPTS },
127                 { "babelname",            LA_BABELNAME },
128                 { "encoding",             LA_ENCODING },
129                 { "end",                  LA_END },
130                 { "fontencoding",         LA_FONTENC },
131                 { "guiname",              LA_GUINAME },
132                 { "hasguisupport",        LA_HAS_GUI_SUPPORT },
133                 { "internalencoding",     LA_INTERNAL_ENC },
134                 { "langcode",             LA_LANG_CODE },
135                 { "langvariety",          LA_LANG_VARIETY },
136                 { "polyglossianame",      LA_POLYGLOSSIANAME },
137                 { "polyglossiaopts",      LA_POLYGLOSSIAOPTS },
138                 { "postbabelpreamble",    LA_POSTBABELPREAMBLE },
139                 { "prebabelpreamble",     LA_PREBABELPREAMBLE },
140                 { "provides",             LA_PROVIDES },
141                 { "quotestyle",           LA_QUOTESTYLE },
142                 { "requires",             LA_REQUIRES },
143                 { "rtl",                  LA_RTL }
144         };
145
146         bool error = false;
147         bool finished = false;
148         lex.pushTable(languageTags);
149         // parse style section
150         while (!finished && lex.isOK() && !error) {
151                 int le = lex.lex();
152                 // See comment in LyXRC.cpp.
153                 switch (le) {
154                 case Lexer::LEX_FEOF:
155                         continue;
156
157                 case Lexer::LEX_UNDEF: // parse error
158                         lex.printError("Unknown language tag `$$Token'");
159                         error = true;
160                         continue;
161
162                 default:
163                         break;
164                 }
165                 switch (static_cast<LanguageTags>(le)) {
166                 case LA_END: // end of structure
167                         finished = true;
168                         break;
169                 case LA_AS_BABELOPTS:
170                         lex >> as_babel_options_;
171                         break;
172                 case LA_BABELNAME:
173                         lex >> babel_;
174                         break;
175                 case LA_POLYGLOSSIANAME:
176                         lex >> polyglossia_name_;
177                         break;
178                 case LA_POLYGLOSSIAOPTS:
179                         lex >> polyglossia_opts_;
180                         break;
181                 case LA_QUOTESTYLE:
182                         lex >> quote_style_;
183                         break;
184                 case LA_ENCODING:
185                         lex >> encodingStr_;
186                         break;
187                 case LA_FONTENC: {
188                         lex.eatLine();
189                         vector<string> const fe =
190                                 getVectorFromString(lex.getString(true), "|");
191                         fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
192                         break;
193                 }
194                 case LA_GUINAME:
195                         lex >> display_;
196                         break;
197                 case LA_HAS_GUI_SUPPORT:
198                         lex >> has_gui_support_;
199                         break;
200                 case LA_INTERNAL_ENC:
201                         lex >> internal_enc_;
202                         break;
203                 case LA_LANG_CODE:
204                         lex >> code_;
205                         break;
206                 case LA_LANG_VARIETY:
207                         lex >> variety_;
208                         break;
209                 case LA_POSTBABELPREAMBLE:
210                         babel_postsettings_ =
211                                 lex.getLongString(from_ascii("EndPostBabelPreamble"));
212                         break;
213                 case LA_PREBABELPREAMBLE:
214                         babel_presettings_ =
215                                 lex.getLongString(from_ascii("EndPreBabelPreamble"));
216                         break;
217                 case LA_REQUIRES:
218                         lex >> requires_;
219                         break;
220                 case LA_PROVIDES:
221                         lex >> provides_;
222                         break;
223                 case LA_RTL:
224                         lex >> rightToLeft_;
225                         break;
226                 }
227         }
228         lex.popTable();
229         return finished && !error;
230 }
231
232
233 bool Language::read(Lexer & lex)
234 {
235         as_babel_options_ = 0;
236         encoding_ = 0;
237         internal_enc_ = 0;
238         rightToLeft_ = 0;
239
240         if (!lex.next()) {
241                 lex.printError("No name given for language: `$$Token'.");
242                 return false;
243         }
244
245         lang_ = lex.getString();
246         LYXERR(Debug::INFO, "Reading language " << lang_);
247         if (!readLanguage(lex)) {
248                 LYXERR0("Error parsing language `" << lang_ << '\'');
249                 return false;
250         }
251
252         encoding_ = encodings.fromLyXName(encodingStr_);
253         if (!encoding_ && !encodingStr_.empty()) {
254                 encoding_ = encodings.fromLyXName("iso8859-1");
255                 LYXERR0("Unknown encoding " << encodingStr_);
256         }
257         if (fontenc_.empty())
258                 fontenc_.push_back("ASCII");
259         return true;
260 }
261
262
263 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
264 {
265         TranslationMap::const_iterator const end = trans.end();
266         for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
267                 if (replace
268                         || layoutTranslations_.find(it->first) == layoutTranslations_.end())
269                         layoutTranslations_[it->first] = it->second;
270         }
271 }
272
273
274 void Languages::read(FileName const & filename)
275 {
276         Lexer lex;
277         lex.setFile(filename);
278         lex.setContext("Languages::read");
279         while (lex.isOK()) {
280                 int le = lex.lex();
281                 switch (le) {
282                 case Lexer::LEX_FEOF:
283                         continue;
284
285                 default:
286                         break;
287                 }
288                 if (lex.getString() != "Language") {
289                         lex.printError("Unknown Language tag `$$Token'");
290                         continue;
291                 }
292                 Language l;
293                 l.read(lex);
294                 if (!lex)
295                         break;
296                 if (l.lang() == "latex") {
297                         // Check if latex language was not already defined.
298                         LASSERT(latex_language == 0, continue);
299                         static const Language latex_lang = l;
300                         latex_language = &latex_lang;
301                 } else if (l.lang() == "ignore") {
302                         // Check if ignore language was not already defined.
303                         LASSERT(ignore_language == 0, continue);
304                         static const Language ignore_lang = l;
305                         ignore_language = &ignore_lang;
306                 } else
307                         languagelist[l.lang()] = l;
308         }
309
310         default_language = getLanguage("english");
311         if (!default_language) {
312                 LYXERR0("Default language \"english\" not found!");
313                 default_language = &(*languagelist.begin()).second;
314                 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
315         }
316
317         // Read layout translations
318         FileName const path = libFileSearch(string(), "layouttranslations");
319         readLayoutTranslations(path);
320 }
321
322
323 namespace {
324
325 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
326 {
327         while (lex.isOK()) {
328                 if (lex.checkFor("End"))
329                         break;
330                 if (!lex.next(true))
331                         return false;
332                 string const key = lex.getString();
333                 if (!lex.next(true))
334                         return false;
335                 docstring const val = lex.getDocString();
336                 trans[key] = val;
337         }
338         return true;
339 }
340
341
342 enum Match {
343         NoMatch,
344         ApproximateMatch,
345         ExactMatch
346 };
347
348
349 Match match(string const & code, Language const & lang)
350 {
351         // we need to mimic gettext: code can be a two-letter code, which
352         // should match all variants, e.g. "de" should match "de_DE",
353         // "de_AT" etc.
354         // special case for chinese:
355         // simplified  => code == "zh_CN", langcode == "zh_CN"
356         // traditional => code == "zh_TW", langcode == "zh_CN"
357         string const variety = lang.variety();
358         string const langcode = variety.empty() ?
359                                 lang.code() : lang.code() + '_' + variety;
360         string const name = lang.lang();
361         if ((code == langcode && name != "chinese-traditional")
362                 || (code == "zh_TW"  && name == "chinese-traditional"))
363                 return ExactMatch;
364         if ((code.size() == 2) && (langcode.size() > 2)
365                 && (code + '_' == langcode.substr(0, 3)))
366                 return ApproximateMatch;
367         return NoMatch;
368 }
369
370 } // namespace
371
372
373 void Languages::readLayoutTranslations(support::FileName const & filename)
374 {
375         Lexer lex;
376         lex.setFile(filename);
377         lex.setContext("Languages::read");
378
379         // 1) read all translations (exact and approximate matches) into trans
380         typedef std::map<string, Language::TranslationMap> TransMap;
381         TransMap trans;
382         LanguageList::iterator const lbeg = languagelist.begin();
383         LanguageList::iterator const lend = languagelist.end();
384         while (lex.isOK()) {
385                 if (!lex.checkFor("Translation")) {
386                         if (lex.isOK())
387                                 lex.printError("Unknown layout translation tag `$$Token'");
388                         break;
389                 }
390                 if (!lex.next(true))
391                         break;
392                 string const code = lex.getString();
393                 bool found = false;
394                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
395                         if (match(code, lit->second) != NoMatch) {
396                                 found = true;
397                                 break;
398                         }
399                 }
400                 if (!found) {
401                         lex.printError("Unknown language `" + code + "'");
402                         break;
403                 }
404                 if (!readTranslations(lex, trans[code])) {
405                         lex.printError("Could not read layout translations for language `"
406                                 + code + "'");
407                         break;
408                 }
409         }
410
411         // 2) merge all translations into the languages
412         // exact translations overwrite approximate ones
413         TransMap::const_iterator const tbeg = trans.begin();
414         TransMap::const_iterator const tend = trans.end();
415         for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
416                 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
417                         Match const m = match(tit->first, lit->second);
418                         if (m == NoMatch)
419                                 continue;
420                         lit->second.readLayoutTranslations(tit->second,
421                                                            m == ExactMatch);
422                 }
423         }
424
425 }
426
427
428 Language const * Languages::getLanguage(string const & language) const
429 {
430         if (language == "reset")
431                 return reset_language;
432         if (language == "ignore")
433                 return ignore_language;
434         const_iterator it = languagelist.find(language);
435         return it == languagelist.end() ? reset_language : &it->second;
436 }
437
438
439 } // namespace lyx