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