3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
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"
30 using namespace lyx::support;
35 Language const * ignore_language = 0;
36 Language const * default_language = 0;
37 Language const * latex_language = 0;
38 Language const * reset_language = 0;
41 docstring const Language::translateLayout(string const & m) const
47 lyxerr << "Warning: not translating `" << m
48 << "' because it is not pure ASCII.\n";
52 TranslationMap::const_iterator it = layoutTranslations_.find(m);
53 if (it != layoutTranslations_.end())
56 docstring t = from_ascii(m);
62 bool Language::readLanguage(Lexer & lex)
82 // Keep these sorted alphabetically!
83 LexerKeyword languageTags[] = {
84 { "asbabeloptions", LA_AS_BABELOPTS },
85 { "babelname", LA_BABELNAME },
86 { "encoding", LA_ENCODING },
88 { "guiname", LA_GUINAME },
89 { "internalencoding", LA_INTERNAL_ENC },
90 { "langcode", LA_LANG_CODE },
91 { "langvariety", LA_LANG_VARIETY },
92 { "polyglossianame", LA_POLYGLOSSIANAME },
93 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
94 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
95 { "prebabelpreamble", LA_PREBABELPREAMBLE },
96 { "quotestyle", LA_QUOTESTYLE },
97 { "requires", LA_REQUIRES },
102 bool finished = false;
103 lex.pushTable(languageTags);
104 // parse style section
105 while (!finished && lex.isOK() && !error) {
107 // See comment in LyXRC.cpp.
109 case Lexer::LEX_FEOF:
112 case Lexer::LEX_UNDEF: // parse error
113 lex.printError("Unknown language tag `$$Token'");
120 switch (static_cast<LanguageTags>(le)) {
121 case LA_END: // end of structure
124 case LA_AS_BABELOPTS:
125 lex >> as_babel_options_;
130 case LA_POLYGLOSSIANAME:
131 lex >> polyglossia_name_;
133 case LA_POLYGLOSSIAOPTS:
134 lex >> polyglossia_opts_;
145 case LA_INTERNAL_ENC:
146 lex >> internal_enc_;
151 case LA_LANG_VARIETY:
154 case LA_POSTBABELPREAMBLE:
155 babel_postsettings_ =
156 lex.getLongString("EndPostBabelPreamble");
158 case LA_PREBABELPREAMBLE:
160 lex.getLongString("EndPreBabelPreamble");
171 return finished && !error;
175 bool Language::read(Lexer & lex)
177 as_babel_options_ = 0;
183 lex.printError("No name given for language: `$$Token'.");
187 lang_ = lex.getString();
188 LYXERR(Debug::INFO, "Reading language " << lang_);
189 if (!readLanguage(lex)) {
190 LYXERR0("Error parsing language `" << lang_ << '\'');
194 encoding_ = encodings.fromLyXName(encodingStr_);
195 if (!encoding_ && !encodingStr_.empty()) {
196 encoding_ = encodings.fromLyXName("iso8859-1");
197 LYXERR0("Unknown encoding " << encodingStr_);
199 // cache translation status. Calling getMessages() directly in
200 // PrefLanguage::PrefLanguage() did only work if the gui language
201 // was set to auto (otherwise all languages would be marked as available).
202 translated_ = getMessages(code()).available();
207 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
209 TranslationMap::const_iterator const end = trans.end();
210 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
212 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
213 layoutTranslations_[it->first] = it->second;
218 void Languages::read(FileName const & filename)
221 lex.setFile(filename);
222 lex.setContext("Languages::read");
226 case Lexer::LEX_FEOF:
232 if (lex.getString() != "Language") {
233 lex.printError("Unknown Language tag `$$Token'");
240 if (l.lang() == "latex") {
241 // Check if latex language was not already defined.
242 LASSERT(latex_language == 0, continue);
243 static const Language latex_lang = l;
244 latex_language = &latex_lang;
245 } else if (l.lang() == "ignore") {
246 // Check if ignore language was not already defined.
247 LASSERT(ignore_language == 0, continue);
248 static const Language ignore_lang = l;
249 ignore_language = &ignore_lang;
251 languagelist[l.lang()] = l;
254 default_language = getLanguage(lyxrc.default_language);
255 if (!default_language) {
256 LYXERR0("Default language \"" << lyxrc.default_language
258 default_language = getLanguage("english");
259 if (!default_language)
260 default_language = &(*languagelist.begin()).second;
261 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
264 // Read layout translations
265 FileName const path = libFileSearch(string(), "layouttranslations");
266 readLayoutTranslations(path);
272 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
275 if (lex.checkFor("End"))
279 string const key = lex.getString();
282 docstring const val = lex.getDocString();
296 Match match(string const & code, Language const & lang)
298 // we need to mimic gettext: code can be a two-letter code, which
299 // should match all variants, e.g. "de" should match "de_DE",
301 // special case for chinese:
302 // simplified => code == "zh_CN", langcode == "zh_CN"
303 // traditional => code == "zh_TW", langcode == "zh_CN"
304 string const variety = lang.variety();
305 string const langcode = variety.empty() ?
306 lang.code() : lang.code() + '_' + variety;
307 string const name = lang.lang();
308 if ((code == langcode && name != "chinese-traditional")
309 || (code == "zh_TW" && name == "chinese-traditional"))
311 if ((code.size() == 2) && (langcode.size() > 2)
312 && (code + '_' == langcode.substr(0, 3)))
313 return ApproximateMatch;
320 void Languages::readLayoutTranslations(support::FileName const & filename)
323 lex.setFile(filename);
324 lex.setContext("Languages::read");
326 // 1) read all translations (exact and approximate matches) into trans
327 typedef std::map<string, Language::TranslationMap> TransMap;
329 LanguageList::iterator const lbeg = languagelist.begin();
330 LanguageList::iterator const lend = languagelist.end();
332 if (!lex.checkFor("Translation")) {
334 lex.printError("Unknown layout translation tag `$$Token'");
339 string const code = lex.getString();
341 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
342 if (match(code, lit->second) != NoMatch) {
348 lex.printError("Unknown language `" + code + "'");
351 if (!readTranslations(lex, trans[code])) {
352 lex.printError("Could not read layout translations for language `"
358 // 2) merge all translations into the languages
359 // exact translations overwrite approximate ones
360 TransMap::const_iterator const tbeg = trans.begin();
361 TransMap::const_iterator const tend = trans.end();
362 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
363 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
364 Match const m = match(tit->first, lit->second);
367 lit->second.readLayoutTranslations(tit->second,
375 Language const * Languages::getLanguage(string const & language) const
377 if (language == "reset")
378 return reset_language;
379 if (language == "ignore")
380 return ignore_language;
381 const_iterator it = languagelist.find(language);
382 return it == languagelist.end() ? reset_language : &it->second;