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 docstring const Language::translateLayout(string const & m) const
53 lyxerr << "Warning: not translating `" << m
54 << "' because it is not pure ASCII.\n";
58 TranslationMap::const_iterator it = layoutTranslations_.find(m);
59 if (it != layoutTranslations_.end())
62 docstring t = from_ascii(m);
68 bool Language::readLanguage(Lexer & lex)
89 // Keep these sorted alphabetically!
90 LexerKeyword languageTags[] = {
91 { "asbabeloptions", LA_AS_BABELOPTS },
92 { "babelname", LA_BABELNAME },
93 { "encoding", LA_ENCODING },
95 { "fontencoding", LA_FONTENC },
96 { "guiname", LA_GUINAME },
97 { "internalencoding", LA_INTERNAL_ENC },
98 { "langcode", LA_LANG_CODE },
99 { "langvariety", LA_LANG_VARIETY },
100 { "polyglossianame", LA_POLYGLOSSIANAME },
101 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
102 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
103 { "prebabelpreamble", LA_PREBABELPREAMBLE },
104 { "quotestyle", LA_QUOTESTYLE },
105 { "requires", LA_REQUIRES },
110 bool finished = false;
111 lex.pushTable(languageTags);
112 // parse style section
113 while (!finished && lex.isOK() && !error) {
115 // See comment in LyXRC.cpp.
117 case Lexer::LEX_FEOF:
120 case Lexer::LEX_UNDEF: // parse error
121 lex.printError("Unknown language tag `$$Token'");
128 switch (static_cast<LanguageTags>(le)) {
129 case LA_END: // end of structure
132 case LA_AS_BABELOPTS:
133 lex >> as_babel_options_;
138 case LA_POLYGLOSSIANAME:
139 lex >> polyglossia_name_;
141 case LA_POLYGLOSSIAOPTS:
142 lex >> polyglossia_opts_;
156 case LA_INTERNAL_ENC:
157 lex >> internal_enc_;
162 case LA_LANG_VARIETY:
165 case LA_POSTBABELPREAMBLE:
166 babel_postsettings_ =
167 lex.getLongString("EndPostBabelPreamble");
169 case LA_PREBABELPREAMBLE:
171 lex.getLongString("EndPreBabelPreamble");
182 return finished && !error;
186 bool Language::read(Lexer & lex)
188 as_babel_options_ = 0;
194 lex.printError("No name given for language: `$$Token'.");
198 lang_ = lex.getString();
199 LYXERR(Debug::INFO, "Reading language " << lang_);
200 if (!readLanguage(lex)) {
201 LYXERR0("Error parsing language `" << lang_ << '\'');
205 encoding_ = encodings.fromLyXName(encodingStr_);
206 if (!encoding_ && !encodingStr_.empty()) {
207 encoding_ = encodings.fromLyXName("iso8859-1");
208 LYXERR0("Unknown encoding " << encodingStr_);
214 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
216 TranslationMap::const_iterator const end = trans.end();
217 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
219 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
220 layoutTranslations_[it->first] = it->second;
225 void Languages::read(FileName const & filename)
228 lex.setFile(filename);
229 lex.setContext("Languages::read");
233 case Lexer::LEX_FEOF:
239 if (lex.getString() != "Language") {
240 lex.printError("Unknown Language tag `$$Token'");
247 if (l.lang() == "latex") {
248 // Check if latex language was not already defined.
249 LASSERT(latex_language == 0, continue);
250 static const Language latex_lang = l;
251 latex_language = &latex_lang;
252 } else if (l.lang() == "ignore") {
253 // Check if ignore language was not already defined.
254 LASSERT(ignore_language == 0, continue);
255 static const Language ignore_lang = l;
256 ignore_language = &ignore_lang;
258 languagelist[l.lang()] = l;
261 default_language = getLanguage("english");
262 if (!default_language) {
263 LYXERR0("Default language \"english\" not found!");
264 default_language = &(*languagelist.begin()).second;
265 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
268 // Read layout translations
269 FileName const path = libFileSearch(string(), "layouttranslations");
270 readLayoutTranslations(path);
276 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
279 if (lex.checkFor("End"))
283 string const key = lex.getString();
286 docstring const val = lex.getDocString();
300 Match match(string const & code, Language const & lang)
302 // we need to mimic gettext: code can be a two-letter code, which
303 // should match all variants, e.g. "de" should match "de_DE",
305 // special case for chinese:
306 // simplified => code == "zh_CN", langcode == "zh_CN"
307 // traditional => code == "zh_TW", langcode == "zh_CN"
308 string const variety = lang.variety();
309 string const langcode = variety.empty() ?
310 lang.code() : lang.code() + '_' + variety;
311 string const name = lang.lang();
312 if ((code == langcode && name != "chinese-traditional")
313 || (code == "zh_TW" && name == "chinese-traditional"))
315 if ((code.size() == 2) && (langcode.size() > 2)
316 && (code + '_' == langcode.substr(0, 3)))
317 return ApproximateMatch;
324 void Languages::readLayoutTranslations(support::FileName const & filename)
327 lex.setFile(filename);
328 lex.setContext("Languages::read");
330 // 1) read all translations (exact and approximate matches) into trans
331 typedef std::map<string, Language::TranslationMap> TransMap;
333 LanguageList::iterator const lbeg = languagelist.begin();
334 LanguageList::iterator const lend = languagelist.end();
336 if (!lex.checkFor("Translation")) {
338 lex.printError("Unknown layout translation tag `$$Token'");
343 string const code = lex.getString();
345 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
346 if (match(code, lit->second) != NoMatch) {
352 lex.printError("Unknown language `" + code + "'");
355 if (!readTranslations(lex, trans[code])) {
356 lex.printError("Could not read layout translations for language `"
362 // 2) merge all translations into the languages
363 // exact translations overwrite approximate ones
364 TransMap::const_iterator const tbeg = trans.begin();
365 TransMap::const_iterator const tend = trans.end();
366 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
367 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
368 Match const m = match(tit->first, lit->second);
371 lit->second.readLayoutTranslations(tit->second,
379 Language const * Languages::getLanguage(string const & language) const
381 if (language == "reset")
382 return reset_language;
383 if (language == "ignore")
384 return ignore_language;
385 const_iterator it = languagelist.find(language);
386 return it == languagelist.end() ? reset_language : &it->second;