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