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)
96 // Keep these sorted alphabetically!
97 LexerKeyword languageTags[] = {
98 { "asbabeloptions", LA_AS_BABELOPTS },
99 { "babelname", LA_BABELNAME },
100 { "encoding", LA_ENCODING },
102 { "fontencoding", LA_FONTENC },
103 { "guiname", LA_GUINAME },
104 { "hasguisupport", LA_HAS_GUI_SUPPORT },
105 { "internalencoding", LA_INTERNAL_ENC },
106 { "langcode", LA_LANG_CODE },
107 { "langvariety", LA_LANG_VARIETY },
108 { "polyglossianame", LA_POLYGLOSSIANAME },
109 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
110 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
111 { "prebabelpreamble", LA_PREBABELPREAMBLE },
112 { "quotestyle", LA_QUOTESTYLE },
113 { "requires", LA_REQUIRES },
118 bool finished = false;
119 lex.pushTable(languageTags);
120 // parse style section
121 while (!finished && lex.isOK() && !error) {
123 // See comment in LyXRC.cpp.
125 case Lexer::LEX_FEOF:
128 case Lexer::LEX_UNDEF: // parse error
129 lex.printError("Unknown language tag `$$Token'");
136 switch (static_cast<LanguageTags>(le)) {
137 case LA_END: // end of structure
140 case LA_AS_BABELOPTS:
141 lex >> as_babel_options_;
146 case LA_POLYGLOSSIANAME:
147 lex >> polyglossia_name_;
149 case LA_POLYGLOSSIAOPTS:
150 lex >> polyglossia_opts_;
164 case LA_HAS_GUI_SUPPORT:
165 lex >> has_gui_support_;
167 case LA_INTERNAL_ENC:
168 lex >> internal_enc_;
173 case LA_LANG_VARIETY:
176 case LA_POSTBABELPREAMBLE:
177 babel_postsettings_ =
178 lex.getLongString(from_ascii("EndPostBabelPreamble"));
180 case LA_PREBABELPREAMBLE:
182 lex.getLongString(from_ascii("EndPreBabelPreamble"));
193 return finished && !error;
197 bool Language::read(Lexer & lex)
199 as_babel_options_ = 0;
205 lex.printError("No name given for language: `$$Token'.");
209 lang_ = lex.getString();
210 LYXERR(Debug::INFO, "Reading language " << lang_);
211 if (!readLanguage(lex)) {
212 LYXERR0("Error parsing language `" << lang_ << '\'');
216 encoding_ = encodings.fromLyXName(encodingStr_);
217 if (!encoding_ && !encodingStr_.empty()) {
218 encoding_ = encodings.fromLyXName("iso8859-1");
219 LYXERR0("Unknown encoding " << encodingStr_);
225 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
227 TranslationMap::const_iterator const end = trans.end();
228 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
230 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
231 layoutTranslations_[it->first] = it->second;
236 void Languages::read(FileName const & filename)
239 lex.setFile(filename);
240 lex.setContext("Languages::read");
244 case Lexer::LEX_FEOF:
250 if (lex.getString() != "Language") {
251 lex.printError("Unknown Language tag `$$Token'");
258 if (l.lang() == "latex") {
259 // Check if latex language was not already defined.
260 LASSERT(latex_language == 0, continue);
261 static const Language latex_lang = l;
262 latex_language = &latex_lang;
263 } else if (l.lang() == "ignore") {
264 // Check if ignore language was not already defined.
265 LASSERT(ignore_language == 0, continue);
266 static const Language ignore_lang = l;
267 ignore_language = &ignore_lang;
269 languagelist[l.lang()] = l;
272 default_language = getLanguage("english");
273 if (!default_language) {
274 LYXERR0("Default language \"english\" not found!");
275 default_language = &(*languagelist.begin()).second;
276 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
279 // Read layout translations
280 FileName const path = libFileSearch(string(), "layouttranslations");
281 readLayoutTranslations(path);
287 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
290 if (lex.checkFor("End"))
294 string const key = lex.getString();
297 docstring const val = lex.getDocString();
311 Match match(string const & code, Language const & lang)
313 // we need to mimic gettext: code can be a two-letter code, which
314 // should match all variants, e.g. "de" should match "de_DE",
316 // special case for chinese:
317 // simplified => code == "zh_CN", langcode == "zh_CN"
318 // traditional => code == "zh_TW", langcode == "zh_CN"
319 string const variety = lang.variety();
320 string const langcode = variety.empty() ?
321 lang.code() : lang.code() + '_' + variety;
322 string const name = lang.lang();
323 if ((code == langcode && name != "chinese-traditional")
324 || (code == "zh_TW" && name == "chinese-traditional"))
326 if ((code.size() == 2) && (langcode.size() > 2)
327 && (code + '_' == langcode.substr(0, 3)))
328 return ApproximateMatch;
335 void Languages::readLayoutTranslations(support::FileName const & filename)
338 lex.setFile(filename);
339 lex.setContext("Languages::read");
341 // 1) read all translations (exact and approximate matches) into trans
342 typedef std::map<string, Language::TranslationMap> TransMap;
344 LanguageList::iterator const lbeg = languagelist.begin();
345 LanguageList::iterator const lend = languagelist.end();
347 if (!lex.checkFor("Translation")) {
349 lex.printError("Unknown layout translation tag `$$Token'");
354 string const code = lex.getString();
356 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
357 if (match(code, lit->second) != NoMatch) {
363 lex.printError("Unknown language `" + code + "'");
366 if (!readTranslations(lex, trans[code])) {
367 lex.printError("Could not read layout translations for language `"
373 // 2) merge all translations into the languages
374 // exact translations overwrite approximate ones
375 TransMap::const_iterator const tbeg = trans.begin();
376 TransMap::const_iterator const tend = trans.end();
377 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
378 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
379 Match const m = match(tit->first, lit->second);
382 lit->second.readLayoutTranslations(tit->second,
390 Language const * Languages::getLanguage(string const & language) const
392 if (language == "reset")
393 return reset_language;
394 if (language == "ignore")
395 return ignore_language;
396 const_iterator it = languagelist.find(language);
397 return it == languagelist.end() ? reset_language : &it->second;