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 bool Language::isPolyglossiaExclusive() const
43 return babel().empty() && !polyglossia().empty() && requires().empty();
47 bool Language::isBabelExclusive() const
49 return !babel().empty() && polyglossia().empty() && requires().empty();
53 docstring const Language::translateLayout(string const & m) const
59 lyxerr << "Warning: not translating `" << m
60 << "' because it is not pure ASCII.\n";
64 TranslationMap::const_iterator it = layoutTranslations_.find(m);
65 if (it != layoutTranslations_.end())
68 docstring t = from_ascii(m);
74 bool Language::readLanguage(Lexer & lex)
97 // Keep these sorted alphabetically!
98 LexerKeyword languageTags[] = {
99 { "asbabeloptions", LA_AS_BABELOPTS },
100 { "babelname", LA_BABELNAME },
101 { "encoding", LA_ENCODING },
103 { "fontencoding", LA_FONTENC },
104 { "guiname", LA_GUINAME },
105 { "hasguisupport", LA_HAS_GUI_SUPPORT },
106 { "internalencoding", LA_INTERNAL_ENC },
107 { "langcode", LA_LANG_CODE },
108 { "langvariety", LA_LANG_VARIETY },
109 { "polyglossianame", LA_POLYGLOSSIANAME },
110 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
111 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
112 { "prebabelpreamble", LA_PREBABELPREAMBLE },
113 { "provides", LA_PROVIDES },
114 { "quotestyle", LA_QUOTESTYLE },
115 { "requires", LA_REQUIRES },
120 bool finished = false;
121 lex.pushTable(languageTags);
122 // parse style section
123 while (!finished && lex.isOK() && !error) {
125 // See comment in LyXRC.cpp.
127 case Lexer::LEX_FEOF:
130 case Lexer::LEX_UNDEF: // parse error
131 lex.printError("Unknown language tag `$$Token'");
138 switch (static_cast<LanguageTags>(le)) {
139 case LA_END: // end of structure
142 case LA_AS_BABELOPTS:
143 lex >> as_babel_options_;
148 case LA_POLYGLOSSIANAME:
149 lex >> polyglossia_name_;
151 case LA_POLYGLOSSIAOPTS:
152 lex >> polyglossia_opts_;
166 case LA_HAS_GUI_SUPPORT:
167 lex >> has_gui_support_;
169 case LA_INTERNAL_ENC:
170 lex >> internal_enc_;
175 case LA_LANG_VARIETY:
178 case LA_POSTBABELPREAMBLE:
179 babel_postsettings_ =
180 lex.getLongString(from_ascii("EndPostBabelPreamble"));
182 case LA_PREBABELPREAMBLE:
184 lex.getLongString(from_ascii("EndPreBabelPreamble"));
198 return finished && !error;
202 bool Language::read(Lexer & lex)
204 as_babel_options_ = 0;
210 lex.printError("No name given for language: `$$Token'.");
214 lang_ = lex.getString();
215 LYXERR(Debug::INFO, "Reading language " << lang_);
216 if (!readLanguage(lex)) {
217 LYXERR0("Error parsing language `" << lang_ << '\'');
221 encoding_ = encodings.fromLyXName(encodingStr_);
222 if (!encoding_ && !encodingStr_.empty()) {
223 encoding_ = encodings.fromLyXName("iso8859-1");
224 LYXERR0("Unknown encoding " << encodingStr_);
230 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
232 TranslationMap::const_iterator const end = trans.end();
233 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
235 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
236 layoutTranslations_[it->first] = it->second;
241 void Languages::read(FileName const & filename)
244 lex.setFile(filename);
245 lex.setContext("Languages::read");
249 case Lexer::LEX_FEOF:
255 if (lex.getString() != "Language") {
256 lex.printError("Unknown Language tag `$$Token'");
263 if (l.lang() == "latex") {
264 // Check if latex language was not already defined.
265 LASSERT(latex_language == 0, continue);
266 static const Language latex_lang = l;
267 latex_language = &latex_lang;
268 } else if (l.lang() == "ignore") {
269 // Check if ignore language was not already defined.
270 LASSERT(ignore_language == 0, continue);
271 static const Language ignore_lang = l;
272 ignore_language = &ignore_lang;
274 languagelist[l.lang()] = l;
277 default_language = getLanguage("english");
278 if (!default_language) {
279 LYXERR0("Default language \"english\" not found!");
280 default_language = &(*languagelist.begin()).second;
281 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
284 // Read layout translations
285 FileName const path = libFileSearch(string(), "layouttranslations");
286 readLayoutTranslations(path);
292 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
295 if (lex.checkFor("End"))
299 string const key = lex.getString();
302 docstring const val = lex.getDocString();
316 Match match(string const & code, Language const & lang)
318 // we need to mimic gettext: code can be a two-letter code, which
319 // should match all variants, e.g. "de" should match "de_DE",
321 // special case for chinese:
322 // simplified => code == "zh_CN", langcode == "zh_CN"
323 // traditional => code == "zh_TW", langcode == "zh_CN"
324 string const variety = lang.variety();
325 string const langcode = variety.empty() ?
326 lang.code() : lang.code() + '_' + variety;
327 string const name = lang.lang();
328 if ((code == langcode && name != "chinese-traditional")
329 || (code == "zh_TW" && name == "chinese-traditional"))
331 if ((code.size() == 2) && (langcode.size() > 2)
332 && (code + '_' == langcode.substr(0, 3)))
333 return ApproximateMatch;
340 void Languages::readLayoutTranslations(support::FileName const & filename)
343 lex.setFile(filename);
344 lex.setContext("Languages::read");
346 // 1) read all translations (exact and approximate matches) into trans
347 typedef std::map<string, Language::TranslationMap> TransMap;
349 LanguageList::iterator const lbeg = languagelist.begin();
350 LanguageList::iterator const lend = languagelist.end();
352 if (!lex.checkFor("Translation")) {
354 lex.printError("Unknown layout translation tag `$$Token'");
359 string const code = lex.getString();
361 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
362 if (match(code, lit->second) != NoMatch) {
368 lex.printError("Unknown language `" + code + "'");
371 if (!readTranslations(lex, trans[code])) {
372 lex.printError("Could not read layout translations for language `"
378 // 2) merge all translations into the languages
379 // exact translations overwrite approximate ones
380 TransMap::const_iterator const tbeg = trans.begin();
381 TransMap::const_iterator const tend = trans.end();
382 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
383 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
384 Match const m = match(tit->first, lit->second);
387 lit->second.readLayoutTranslations(tit->second,
395 Language const * Languages::getLanguage(string const & language) const
397 if (language == "reset")
398 return reset_language;
399 if (language == "ignore")
400 return ignore_language;
401 const_iterator it = languagelist.find(language);
402 return it == languagelist.end() ? reset_language : &it->second;