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)
88 // Keep these sorted alphabetically!
89 LexerKeyword languageTags[] = {
90 { "asbabeloptions", LA_AS_BABELOPTS },
91 { "babelname", LA_BABELNAME },
92 { "encoding", LA_ENCODING },
94 { "guiname", LA_GUINAME },
95 { "internalencoding", LA_INTERNAL_ENC },
96 { "langcode", LA_LANG_CODE },
97 { "langvariety", LA_LANG_VARIETY },
98 { "polyglossianame", LA_POLYGLOSSIANAME },
99 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
100 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
101 { "prebabelpreamble", LA_PREBABELPREAMBLE },
102 { "quotestyle", LA_QUOTESTYLE },
103 { "requires", LA_REQUIRES },
108 bool finished = false;
109 lex.pushTable(languageTags);
110 // parse style section
111 while (!finished && lex.isOK() && !error) {
113 // See comment in LyXRC.cpp.
115 case Lexer::LEX_FEOF:
118 case Lexer::LEX_UNDEF: // parse error
119 lex.printError("Unknown language tag `$$Token'");
126 switch (static_cast<LanguageTags>(le)) {
127 case LA_END: // end of structure
130 case LA_AS_BABELOPTS:
131 lex >> as_babel_options_;
136 case LA_POLYGLOSSIANAME:
137 lex >> polyglossia_name_;
139 case LA_POLYGLOSSIAOPTS:
140 lex >> polyglossia_opts_;
151 case LA_INTERNAL_ENC:
152 lex >> internal_enc_;
157 case LA_LANG_VARIETY:
160 case LA_POSTBABELPREAMBLE:
161 babel_postsettings_ =
162 lex.getLongString("EndPostBabelPreamble");
164 case LA_PREBABELPREAMBLE:
166 lex.getLongString("EndPreBabelPreamble");
177 return finished && !error;
181 bool Language::read(Lexer & lex)
183 as_babel_options_ = 0;
189 lex.printError("No name given for language: `$$Token'.");
193 lang_ = lex.getString();
194 LYXERR(Debug::INFO, "Reading language " << lang_);
195 if (!readLanguage(lex)) {
196 LYXERR0("Error parsing language `" << lang_ << '\'');
200 encoding_ = encodings.fromLyXName(encodingStr_);
201 if (!encoding_ && !encodingStr_.empty()) {
202 encoding_ = encodings.fromLyXName("iso8859-1");
203 LYXERR0("Unknown encoding " << encodingStr_);
209 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
211 TranslationMap::const_iterator const end = trans.end();
212 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
214 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
215 layoutTranslations_[it->first] = it->second;
220 void Languages::read(FileName const & filename)
223 lex.setFile(filename);
224 lex.setContext("Languages::read");
228 case Lexer::LEX_FEOF:
234 if (lex.getString() != "Language") {
235 lex.printError("Unknown Language tag `$$Token'");
242 if (l.lang() == "latex") {
243 // Check if latex language was not already defined.
244 LASSERT(latex_language == 0, continue);
245 static const Language latex_lang = l;
246 latex_language = &latex_lang;
247 } else if (l.lang() == "ignore") {
248 // Check if ignore language was not already defined.
249 LASSERT(ignore_language == 0, continue);
250 static const Language ignore_lang = l;
251 ignore_language = &ignore_lang;
253 languagelist[l.lang()] = l;
256 default_language = getLanguage("english");
257 if (!default_language) {
258 LYXERR0("Default language \"english\" not found!");
259 default_language = &(*languagelist.begin()).second;
260 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
263 // Read layout translations
264 FileName const path = libFileSearch(string(), "layouttranslations");
265 readLayoutTranslations(path);
271 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
274 if (lex.checkFor("End"))
278 string const key = lex.getString();
281 docstring const val = lex.getDocString();
295 Match match(string const & code, Language const & lang)
297 // we need to mimic gettext: code can be a two-letter code, which
298 // should match all variants, e.g. "de" should match "de_DE",
300 // special case for chinese:
301 // simplified => code == "zh_CN", langcode == "zh_CN"
302 // traditional => code == "zh_TW", langcode == "zh_CN"
303 string const variety = lang.variety();
304 string const langcode = variety.empty() ?
305 lang.code() : lang.code() + '_' + variety;
306 string const name = lang.lang();
307 if ((code == langcode && name != "chinese-traditional")
308 || (code == "zh_TW" && name == "chinese-traditional"))
310 if ((code.size() == 2) && (langcode.size() > 2)
311 && (code + '_' == langcode.substr(0, 3)))
312 return ApproximateMatch;
319 void Languages::readLayoutTranslations(support::FileName const & filename)
322 lex.setFile(filename);
323 lex.setContext("Languages::read");
325 // 1) read all translations (exact and approximate matches) into trans
326 typedef std::map<string, Language::TranslationMap> TransMap;
328 LanguageList::iterator const lbeg = languagelist.begin();
329 LanguageList::iterator const lend = languagelist.end();
331 if (!lex.checkFor("Translation")) {
333 lex.printError("Unknown layout translation tag `$$Token'");
338 string const code = lex.getString();
340 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
341 if (match(code, lit->second) != NoMatch) {
347 lex.printError("Unknown language `" + code + "'");
350 if (!readTranslations(lex, trans[code])) {
351 lex.printError("Could not read layout translations for language `"
357 // 2) merge all translations into the languages
358 // exact translations overwrite approximate ones
359 TransMap::const_iterator const tbeg = trans.begin();
360 TransMap::const_iterator const tend = trans.end();
361 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
362 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
363 Match const m = match(tit->first, lit->second);
366 lit->second.readLayoutTranslations(tit->second,
374 Language const * Languages::getLanguage(string const & language) const
376 if (language == "reset")
377 return reset_language;
378 if (language == "ignore")
379 return ignore_language;
380 const_iterator it = languagelist.find(language);
381 return it == languagelist.end() ? reset_language : &it->second;