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