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;
36 Language const * default_language;
37 Language const * ignore_language = &ignore_lang;
38 Language const * latex_language = &latex_lang;
39 Language const * reset_language = 0;
42 docstring const Language::translateLayout(string const & m) const
48 lyxerr << "Warning: not translating `" << m
49 << "' because it is not pure ASCII.\n";
53 TranslationMap::const_iterator it = layoutTranslations_.find(m);
54 if (it != layoutTranslations_.end())
61 bool Language::readLanguage(Lexer & lex)
79 // Keep these sorted alphabetically!
80 LexerKeyword languageTags[] = {
81 { "asbabeloptions", LA_AS_BABELOPTS },
82 { "babelname", LA_BABELNAME },
83 { "encoding", LA_ENCODING },
85 { "guiname", LA_GUINAME },
86 { "internalencoding", LA_INTERNAL_ENC },
87 { "langcode", LA_LANG_CODE },
88 { "langvariety", LA_LANG_VARIETY },
89 { "polyglossianame", LA_POLYGLOSSIANAME },
90 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
91 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
92 { "prebabelpreamble", LA_PREBABELPREAMBLE },
97 bool finished = false;
98 lex.pushTable(languageTags);
99 // parse style section
100 while (!finished && lex.isOK() && !error) {
102 // See comment in LyXRC.cpp.
104 case Lexer::LEX_FEOF:
107 case Lexer::LEX_UNDEF: // parse error
108 lex.printError("Unknown language tag `$$Token'");
115 switch (static_cast<LanguageTags>(le)) {
116 case LA_END: // end of structure
119 case LA_AS_BABELOPTS:
120 lex >> as_babel_options_;
125 case LA_POLYGLOSSIANAME:
126 lex >> polyglossia_name_;
128 case LA_POLYGLOSSIAOPTS:
129 lex >> polyglossia_opts_;
137 case LA_INTERNAL_ENC:
138 lex >> internal_enc_;
143 case LA_LANG_VARIETY:
146 case LA_POSTBABELPREAMBLE:
147 babel_postsettings_ =
148 lex.getLongString("EndPostBabelPreamble");
150 case LA_PREBABELPREAMBLE:
152 lex.getLongString("EndPreBabelPreamble");
160 return finished && !error;
164 bool Language::read(Lexer & lex)
166 as_babel_options_ = 0;
172 lex.printError("No name given for language: `$$Token'.");
176 lang_ = lex.getString();
177 LYXERR(Debug::INFO, "Reading language " << lang_);
178 if (!readLanguage(lex)) {
179 LYXERR0("Error parsing language `" << lang_ << '\'');
183 encoding_ = encodings.fromLyXName(encodingStr_);
184 if (!encoding_ && !encodingStr_.empty()) {
185 encoding_ = encodings.fromLyXName("iso8859-1");
186 LYXERR0("Unknown encoding " << encodingStr_);
188 // cache translation status. Calling getMessages() directly in
189 // PrefLanguage::PrefLanguage() did only work if the gui language
190 // was set to auto (otherwise all languages would be marked as available).
191 translated_ = getMessages(code()).available();
196 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
198 TranslationMap::const_iterator const end = trans.end();
199 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
201 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
202 layoutTranslations_[it->first] = it->second;
207 void Languages::read(FileName const & filename)
210 lex.setFile(filename);
211 lex.setContext("Languages::read");
215 case Lexer::LEX_FEOF:
221 if (lex.getString() != "Language") {
222 lex.printError("Unknown Language tag `$$Token'");
229 if (l.lang() == "latex")
231 else if (l.lang() == "ignore")
234 languagelist[l.lang()] = l;
237 default_language = getLanguage(lyxrc.default_language);
238 if (!default_language) {
239 LYXERR0("Default language \"" << lyxrc.default_language
241 default_language = getLanguage("english");
242 if (!default_language)
243 default_language = &(*languagelist.begin()).second;
244 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
247 // Read layout translations
248 FileName const path = libFileSearch(string(), "layouttranslations");
249 readLayoutTranslations(path);
255 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
258 if (lex.checkFor("End"))
262 string const key = lex.getString();
265 docstring const val = lex.getDocString();
279 Match match(string const & code, Language const & lang)
281 // we need to mimic gettext: code can be a two-letter code, which
282 // should match all variants, e.g. "de" should match "de_DE",
284 // special case for chinese:
285 // simplified => code == "zh_CN", langcode == "zh_CN"
286 // traditional => code == "zh_TW", langcode == "zh_CN"
287 string const variety = lang.variety();
288 string const langcode = variety.empty() ?
289 lang.code() : lang.code() + '_' + variety;
290 string const name = lang.lang();
291 if ((code == langcode && name != "chinese-traditional")
292 || (code == "zh_TW" && name == "chinese-traditional"))
294 if ((code.size() == 2) && (langcode.size() > 2)
295 && (code + '_' == langcode.substr(0, 3)))
296 return ApproximateMatch;
303 void Languages::readLayoutTranslations(support::FileName const & filename)
306 lex.setFile(filename);
307 lex.setContext("Languages::read");
309 // 1) read all translations (exact and approximate matches) into trans
310 typedef std::map<string, Language::TranslationMap> TransMap;
312 LanguageList::iterator const lbeg = languagelist.begin();
313 LanguageList::iterator const lend = languagelist.end();
315 if (!lex.checkFor("Translation")) {
317 lex.printError("Unknown layout translation tag `$$Token'");
322 string const code = lex.getString();
324 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
325 if (match(code, lit->second) != NoMatch) {
326 if (readTranslations(lex, trans[code]))
329 lex.printError("Could not read layout "
330 "translations for language "
336 lex.printError("Unknown language `" + code + "'");
341 // 2) merge all translations into the languages
342 // exact translations overwrite approximate ones
343 TransMap::const_iterator const tbeg = trans.begin();
344 TransMap::const_iterator const tend = trans.end();
345 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
346 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
347 Match m = match(tit->first, lit->second);
350 lit->second.readLayoutTranslations(tit->second,
358 Language const * Languages::getLanguage(string const & language) const
360 if (language == "reset")
361 return reset_language;
362 if (language == "ignore")
363 return ignore_language;
364 const_iterator it = languagelist.find(language);
365 return it == languagelist.end() ? reset_language : &it->second;