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