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/lstrings.h"
26 #include "support/Messages.h"
29 using namespace lyx::support;
37 Language const * default_language;
38 Language const * ignore_language = &ignore_lang;
39 Language const * latex_language = &latex_lang;
40 Language const * reset_language = 0;
43 docstring const Language::translateLayout(string const & m) const
49 lyxerr << "Warning: not translating `" << m
50 << "' because it is not pure ASCII.\n";
54 TranslationMap::const_iterator it = layoutTranslations_.find(m);
55 if (it != layoutTranslations_.end())
62 bool Language::readLanguage(Lexer & lex)
80 // Keep these sorted alphabetically!
81 LexerKeyword languageTags[] = {
82 { "asbabeloptions", LA_AS_BABELOPTS },
83 { "babelname", LA_BABELNAME },
84 { "encoding", LA_ENCODING },
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 },
98 bool finished = false;
99 lex.pushTable(languageTags);
100 // parse style section
101 while (!finished && lex.isOK() && !error) {
103 // See comment in LyXRC.cpp.
105 case Lexer::LEX_FEOF:
108 case Lexer::LEX_UNDEF: // parse error
109 lex.printError("Unknown language tag `$$Token'");
116 switch (static_cast<LanguageTags>(le)) {
117 case LA_END: // end of structure
120 case LA_AS_BABELOPTS:
121 lex >> as_babel_options_;
126 case LA_POLYGLOSSIANAME:
127 lex >> polyglossia_name_;
129 case LA_POLYGLOSSIAOPTS:
130 lex >> polyglossia_opts_;
138 case LA_INTERNAL_ENC:
139 lex >> internal_enc_;
144 case LA_LANG_VARIETY:
147 case LA_POSTBABELPREAMBLE:
148 babel_postsettings_ =
149 lex.getLongString("EndPostBabelPreamble");
151 case LA_PREBABELPREAMBLE:
153 lex.getLongString("EndPreBabelPreamble");
161 return finished && !error;
165 bool Language::read(Lexer & lex)
167 as_babel_options_ = 0;
173 lex.printError("No name given for language: `$$Token'.");
177 lang_ = lex.getString();
178 LYXERR(Debug::INFO, "Reading language " << lang_);
179 if (!readLanguage(lex)) {
180 LYXERR0("Error parsing language `" << lang_ << '\'');
184 encoding_ = encodings.fromLyXName(encodingStr_);
185 if (!encoding_ && !encodingStr_.empty()) {
186 encoding_ = encodings.fromLyXName("iso8859-1");
187 LYXERR0("Unknown encoding " << encodingStr_);
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();
199 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
202 if (lex.checkFor("End"))
206 string const key = lex.getString();
209 docstring const val = lex.getDocString();
215 enum Match{NoMatch, ApproximateMatch, ExactMatch};
217 Match match(string const & code, Language const & lang)
219 // we need to mimic gettext: code can be a two-letter code, which
220 // should match all variants, e.g. "de" should match "de_DE",
222 // special case for chinese:
223 // simplified => code == "zh_CN", langcode == "zh_CN"
224 // traditional => code == "zh_TW", langcode == "zh_CN"
225 string const variety = lang.variety();
226 string const langcode = variety.empty() ?
227 lang.code() : lang.code() + '_' + variety;
228 string const name = lang.lang();
229 if ((code == langcode && name != "chinese-traditional") ||
230 (code == "zh_TW" && name == "chinese-traditional"))
232 if ((code.size() == 2 && langcode.size() > 2 &&
233 code + '_' == langcode.substr(0, 3)))
234 return ApproximateMatch;
241 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
243 TranslationMap::const_iterator const end = trans.end();
244 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it)
246 layoutTranslations_.find(it->first) == layoutTranslations_.end())
247 layoutTranslations_[it->first] = it->second;
251 void Languages::read(FileName const & filename)
254 lex.setFile(filename);
255 lex.setContext("Languages::read");
259 case Lexer::LEX_FEOF:
265 if (lex.getString() != "Language") {
266 lex.printError("Unknown Language tag `$$Token'");
273 if (l.lang() == "latex")
275 else if (l.lang() == "ignore")
278 languagelist[l.lang()] = l;
281 default_language = getLanguage(lyxrc.default_language);
282 if (!default_language) {
283 LYXERR0("Default language \"" << lyxrc.default_language
285 default_language = getLanguage("english");
286 if (!default_language)
287 default_language = &(*languagelist.begin()).second;
288 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
291 // Read layout translations
292 FileName const path = libFileSearch(string(), "layouttranslations");
293 readLayoutTranslations(path);
297 void Languages::readLayoutTranslations(support::FileName const & filename)
300 lex.setFile(filename);
301 lex.setContext("Languages::read");
303 // 1) read all translations (exact and approximate matches) into trans
304 typedef std::map<string, Language::TranslationMap> TransMap;
306 LanguageList::iterator const lbeg = languagelist.begin();
307 LanguageList::iterator const lend = languagelist.end();
309 if (!lex.checkFor("Translation")) {
311 lex.printError("Unknown layout translation tag `$$Token'");
316 string const code = lex.getString();
318 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
319 if (match(code, lit->second) != NoMatch) {
320 if (readTranslations(lex, trans[code]))
323 lex.printError("Could not read layout "
324 "translations for language "
330 lex.printError("Unknown language `" + code + "'");
335 // 2) merge all translations into the languages
336 // exact translations overwrite approximate ones
337 TransMap::const_iterator const tbeg = trans.begin();
338 TransMap::const_iterator const tend = trans.end();
339 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
340 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
341 Match m = match(tit->first, lit->second);
344 lit->second.readLayoutTranslations(tit->second,
352 Language const * Languages::getLanguage(string const & language) const
354 if (language == "reset")
355 return reset_language;
356 if (language == "ignore")
357 return ignore_language;
358 const_iterator it = languagelist.find(language);
359 return it == languagelist.end() ? reset_language : &it->second;