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 docstring const Language::translateLayout(string const & m) const
47 lyxerr << "Warning: not translating `" << m
48 << "' because it is not pure ASCII.\n";
52 TranslationMap::const_iterator it = layoutTranslations_.find(m);
53 if (it != layoutTranslations_.end())
60 bool Language::readLanguage(Lexer & lex)
78 // Keep these sorted alphabetically!
79 LexerKeyword languageTags[] = {
80 { "asbabeloptions", LA_AS_BABELOPTS },
81 { "babelname", LA_BABELNAME },
82 { "encoding", LA_ENCODING },
84 { "guiname", LA_GUINAME },
85 { "internalencoding", LA_INTERNAL_ENC },
86 { "langcode", LA_LANG_CODE },
87 { "langvariety", LA_LANG_VARIETY },
88 { "polyglossianame", LA_POLYGLOSSIANAME },
89 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
90 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
91 { "prebabelpreamble", LA_PREBABELPREAMBLE },
96 bool finished = false;
97 lex.pushTable(languageTags);
98 // parse style section
99 while (!finished && lex.isOK() && !error) {
101 // See comment in LyXRC.cpp.
103 case Lexer::LEX_FEOF:
106 case Lexer::LEX_UNDEF: // parse error
107 lex.printError("Unknown language tag `$$Token'");
114 switch (static_cast<LanguageTags>(le)) {
115 case LA_END: // end of structure
118 case LA_AS_BABELOPTS:
119 lex >> as_babel_options_;
124 case LA_POLYGLOSSIANAME:
125 lex >> polyglossia_name_;
127 case LA_POLYGLOSSIAOPTS:
128 lex >> polyglossia_opts_;
136 case LA_INTERNAL_ENC:
137 lex >> internal_enc_;
142 case LA_LANG_VARIETY:
145 case LA_POSTBABELPREAMBLE:
146 babel_postsettings_ =
147 lex.getLongString("EndPostBabelPreamble");
149 case LA_PREBABELPREAMBLE:
151 lex.getLongString("EndPreBabelPreamble");
159 return finished && !error;
163 bool Language::read(Lexer & lex)
165 as_babel_options_ = 0;
171 lex.printError("No name given for language: `$$Token'.");
175 lang_ = lex.getString();
176 LYXERR(Debug::INFO, "Reading language " << lang_);
177 if (!readLanguage(lex)) {
178 LYXERR0("Error parsing language `" << lang_ << '\'');
182 encoding_ = encodings.fromLyXName(encodingStr_);
183 if (!encoding_ && !encodingStr_.empty()) {
184 encoding_ = encodings.fromLyXName("iso8859-1");
185 LYXERR0("Unknown encoding " << encodingStr_);
187 // cache translation status. Calling getMessages() directly in
188 // PrefLanguage::PrefLanguage() did only work if the gui language
189 // was set to auto (otherwise all languages would be marked as available).
190 translated_ = getMessages(code()).available();
195 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
197 TranslationMap::const_iterator const end = trans.end();
198 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
200 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
201 layoutTranslations_[it->first] = it->second;
206 void Languages::read(FileName const & filename)
209 lex.setFile(filename);
210 lex.setContext("Languages::read");
214 case Lexer::LEX_FEOF:
220 if (lex.getString() != "Language") {
221 lex.printError("Unknown Language tag `$$Token'");
228 if (l.lang() == "latex") {
229 // Check if latex language was not already defined.
230 LASSERT(latex_language == 0, continue);
231 static const Language latex_lang = l;
232 latex_language = &latex_lang;
233 } else if (l.lang() == "ignore") {
234 // Check if ignore language was not already defined.
235 LASSERT(ignore_language == 0, continue);
236 static const Language ignore_lang = l;
237 ignore_language = &ignore_lang;
239 languagelist[l.lang()] = l;
242 default_language = getLanguage(lyxrc.default_language);
243 if (!default_language) {
244 LYXERR0("Default language \"" << lyxrc.default_language
246 default_language = getLanguage("english");
247 if (!default_language)
248 default_language = &(*languagelist.begin()).second;
249 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
252 // Read layout translations
253 FileName const path = libFileSearch(string(), "layouttranslations");
254 readLayoutTranslations(path);
260 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
263 if (lex.checkFor("End"))
267 string const key = lex.getString();
270 docstring const val = lex.getDocString();
284 Match match(string const & code, Language const & lang)
286 // we need to mimic gettext: code can be a two-letter code, which
287 // should match all variants, e.g. "de" should match "de_DE",
289 // special case for chinese:
290 // simplified => code == "zh_CN", langcode == "zh_CN"
291 // traditional => code == "zh_TW", langcode == "zh_CN"
292 string const variety = lang.variety();
293 string const langcode = variety.empty() ?
294 lang.code() : lang.code() + '_' + variety;
295 string const name = lang.lang();
296 if ((code == langcode && name != "chinese-traditional")
297 || (code == "zh_TW" && name == "chinese-traditional"))
299 if ((code.size() == 2) && (langcode.size() > 2)
300 && (code + '_' == langcode.substr(0, 3)))
301 return ApproximateMatch;
308 void Languages::readLayoutTranslations(support::FileName const & filename)
311 lex.setFile(filename);
312 lex.setContext("Languages::read");
314 // 1) read all translations (exact and approximate matches) into trans
315 typedef std::map<string, Language::TranslationMap> TransMap;
317 LanguageList::iterator const lbeg = languagelist.begin();
318 LanguageList::iterator const lend = languagelist.end();
320 if (!lex.checkFor("Translation")) {
322 lex.printError("Unknown layout translation tag `$$Token'");
327 string const code = lex.getString();
329 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
330 if (match(code, lit->second) != NoMatch) {
336 lex.printError("Unknown language `" + code + "'");
339 if (!readTranslations(lex, trans[code])) {
340 lex.printError("Could not read layout translations for language `"
346 // 2) merge all translations into the languages
347 // exact translations overwrite approximate ones
348 TransMap::const_iterator const tbeg = trans.begin();
349 TransMap::const_iterator const tend = trans.end();
350 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
351 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
352 Match const m = match(tit->first, lit->second);
355 lit->second.readLayoutTranslations(tit->second,
363 Language const * Languages::getLanguage(string const & language) const
365 if (language == "reset")
366 return reset_language;
367 if (language == "ignore")
368 return ignore_language;
369 const_iterator it = languagelist.find(language);
370 return it == languagelist.end() ? reset_language : &it->second;