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.
19 #include "LaTeXFonts.h"
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Messages.h"
31 using namespace lyx::support;
36 Language const * ignore_language = 0;
37 Language const * default_language = 0;
38 Language const * latex_language = 0;
39 Language const * reset_language = 0;
42 bool Language::isPolyglossiaExclusive() const
44 return babel().empty() && !polyglossia().empty() && requires().empty();
48 bool Language::isBabelExclusive() const
50 return !babel().empty() && polyglossia().empty() && requires().empty();
54 docstring const Language::translateLayout(string const & m) const
60 lyxerr << "Warning: not translating `" << m
61 << "' because it is not pure ASCII.\n";
65 TranslationMap::const_iterator it = layoutTranslations_.find(m);
66 if (it != layoutTranslations_.end())
69 docstring t = from_ascii(m);
75 string Language::fontenc(BufferParams const & params) const
77 // Determine optimal font encoding
78 // We check whether the used rm font supports an encoding our language supports
79 for (auto & fe : fontenc_) {
80 LaTeXFont const & lf = theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
81 // ASCII means: support all T* encodings plus OT1
83 vector<string> const lfe = lf.fontencs();
84 for (auto & afe : lfe) {
85 if (afe == "OT1" || prefixIs(afe, "T"))
86 // we found a suitable one; return that.
90 // For other encodings, just check whether the font supports it
91 if (lf.hasFontenc(fe))
94 // We did not find a suitable one; just take the first in the list,
95 // the priorized one (which is "T1" for ASCII).
96 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
100 bool Language::readLanguage(Lexer & lex)
115 LA_POSTBABELPREAMBLE,
123 // Keep these sorted alphabetically!
124 LexerKeyword languageTags[] = {
125 { "asbabeloptions", LA_AS_BABELOPTS },
126 { "babelname", LA_BABELNAME },
127 { "encoding", LA_ENCODING },
129 { "fontencoding", LA_FONTENC },
130 { "guiname", LA_GUINAME },
131 { "hasguisupport", LA_HAS_GUI_SUPPORT },
132 { "internalencoding", LA_INTERNAL_ENC },
133 { "langcode", LA_LANG_CODE },
134 { "langvariety", LA_LANG_VARIETY },
135 { "polyglossianame", LA_POLYGLOSSIANAME },
136 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
137 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
138 { "prebabelpreamble", LA_PREBABELPREAMBLE },
139 { "provides", LA_PROVIDES },
140 { "quotestyle", LA_QUOTESTYLE },
141 { "requires", LA_REQUIRES },
146 bool finished = false;
147 lex.pushTable(languageTags);
148 // parse style section
149 while (!finished && lex.isOK() && !error) {
151 // See comment in LyXRC.cpp.
153 case Lexer::LEX_FEOF:
156 case Lexer::LEX_UNDEF: // parse error
157 lex.printError("Unknown language tag `$$Token'");
164 switch (static_cast<LanguageTags>(le)) {
165 case LA_END: // end of structure
168 case LA_AS_BABELOPTS:
169 lex >> as_babel_options_;
174 case LA_POLYGLOSSIANAME:
175 lex >> polyglossia_name_;
177 case LA_POLYGLOSSIAOPTS:
178 lex >> polyglossia_opts_;
188 vector<string> const fe =
189 getVectorFromString(lex.getString(true), "|");
190 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
196 case LA_HAS_GUI_SUPPORT:
197 lex >> has_gui_support_;
199 case LA_INTERNAL_ENC:
200 lex >> internal_enc_;
205 case LA_LANG_VARIETY:
208 case LA_POSTBABELPREAMBLE:
209 babel_postsettings_ =
210 lex.getLongString(from_ascii("EndPostBabelPreamble"));
212 case LA_PREBABELPREAMBLE:
214 lex.getLongString(from_ascii("EndPreBabelPreamble"));
228 return finished && !error;
232 bool Language::read(Lexer & lex)
234 as_babel_options_ = 0;
240 lex.printError("No name given for language: `$$Token'.");
244 lang_ = lex.getString();
245 LYXERR(Debug::INFO, "Reading language " << lang_);
246 if (!readLanguage(lex)) {
247 LYXERR0("Error parsing language `" << lang_ << '\'');
251 encoding_ = encodings.fromLyXName(encodingStr_);
252 if (!encoding_ && !encodingStr_.empty()) {
253 encoding_ = encodings.fromLyXName("iso8859-1");
254 LYXERR0("Unknown encoding " << encodingStr_);
256 if (fontenc_.empty())
257 fontenc_.push_back("ASCII");
262 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
264 TranslationMap::const_iterator const end = trans.end();
265 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
267 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
268 layoutTranslations_[it->first] = it->second;
273 void Languages::read(FileName const & filename)
276 lex.setFile(filename);
277 lex.setContext("Languages::read");
281 case Lexer::LEX_FEOF:
287 if (lex.getString() != "Language") {
288 lex.printError("Unknown Language tag `$$Token'");
295 if (l.lang() == "latex") {
296 // Check if latex language was not already defined.
297 LASSERT(latex_language == 0, continue);
298 static const Language latex_lang = l;
299 latex_language = &latex_lang;
300 } else if (l.lang() == "ignore") {
301 // Check if ignore language was not already defined.
302 LASSERT(ignore_language == 0, continue);
303 static const Language ignore_lang = l;
304 ignore_language = &ignore_lang;
306 languagelist[l.lang()] = l;
309 default_language = getLanguage("english");
310 if (!default_language) {
311 LYXERR0("Default language \"english\" not found!");
312 default_language = &(*languagelist.begin()).second;
313 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
316 // Read layout translations
317 FileName const path = libFileSearch(string(), "layouttranslations");
318 readLayoutTranslations(path);
324 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
327 if (lex.checkFor("End"))
331 string const key = lex.getString();
334 docstring const val = lex.getDocString();
348 Match match(string const & code, Language const & lang)
350 // we need to mimic gettext: code can be a two-letter code, which
351 // should match all variants, e.g. "de" should match "de_DE",
353 // special case for chinese:
354 // simplified => code == "zh_CN", langcode == "zh_CN"
355 // traditional => code == "zh_TW", langcode == "zh_CN"
356 string const variety = lang.variety();
357 string const langcode = variety.empty() ?
358 lang.code() : lang.code() + '_' + variety;
359 string const name = lang.lang();
360 if ((code == langcode && name != "chinese-traditional")
361 || (code == "zh_TW" && name == "chinese-traditional"))
363 if ((code.size() == 2) && (langcode.size() > 2)
364 && (code + '_' == langcode.substr(0, 3)))
365 return ApproximateMatch;
372 void Languages::readLayoutTranslations(support::FileName const & filename)
375 lex.setFile(filename);
376 lex.setContext("Languages::read");
378 // 1) read all translations (exact and approximate matches) into trans
379 typedef std::map<string, Language::TranslationMap> TransMap;
381 LanguageList::iterator const lbeg = languagelist.begin();
382 LanguageList::iterator const lend = languagelist.end();
384 if (!lex.checkFor("Translation")) {
386 lex.printError("Unknown layout translation tag `$$Token'");
391 string const code = lex.getString();
393 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
394 if (match(code, lit->second) != NoMatch) {
400 lex.printError("Unknown language `" + code + "'");
403 if (!readTranslations(lex, trans[code])) {
404 lex.printError("Could not read layout translations for language `"
410 // 2) merge all translations into the languages
411 // exact translations overwrite approximate ones
412 TransMap::const_iterator const tbeg = trans.begin();
413 TransMap::const_iterator const tend = trans.end();
414 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
415 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
416 Match const m = match(tit->first, lit->second);
419 lit->second.readLayoutTranslations(tit->second,
427 Language const * Languages::getLanguage(string const & language) const
429 if (language == "reset")
430 return reset_language;
431 if (language == "ignore")
432 return ignore_language;
433 const_iterator it = languagelist.find(language);
434 return it == languagelist.end() ? reset_language : &it->second;