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