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