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/qstring_helpers.h"
29 #include "support/Messages.h"
35 using namespace lyx::support;
40 Language const * ignore_language = nullptr;
41 Language const * default_language = nullptr;
42 Language const * latex_language = nullptr;
43 Language const * reset_language = nullptr;
46 bool Language::isPolyglossiaExclusive() const
48 return babel().empty() && !polyglossia().empty() && required().empty();
52 bool Language::isBabelExclusive() const
54 return !babel().empty() && polyglossia().empty() && required().empty();
58 docstring const Language::translateLayout(string const & m) const
64 lyxerr << "Warning: not translating `" << m
65 << "' because it is not pure ASCII.\n";
69 TranslationMap::const_iterator it = layoutTranslations_.find(m);
70 if (it != layoutTranslations_.end())
73 docstring t = from_ascii(m);
79 string Language::fontenc(BufferParams const & params) const
81 // Don't use LaTeX fonts, so just return the language's preferred
82 // (although this is not used with nonTeXFonts anyway).
83 if (params.useNonTeXFonts)
84 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
86 // Determine optimal font encoding
87 // We check whether the used rm font supports an encoding our language supports
88 LaTeXFont const & lf =
89 theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
90 vector<string> const lfe = lf.fontencs();
91 for (auto & fe : fontenc_) {
92 // ASCII means: support all T* encodings plus OT1
94 for (auto & afe : lfe) {
95 if (afe == "OT1" || prefixIs(afe, "T"))
96 // we found a suitable one; return that.
100 // For other encodings, just check whether the font supports it
101 if (lf.hasFontenc(fe))
104 // We did not find a suitable one; just take the first in the list,
105 // the priorized one (which is "T1" for ASCII).
106 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
110 string Language::dateFormat(size_t i) const
112 if (i > dateformats_.size())
114 return dateformats_.at(i);
118 docstring Language::decimalSeparator() const
120 if (lyxrc.default_decimal_sep == "locale") {
121 QLocale loc = QLocale(toqstr(code()));
122 return qstring_to_ucs4(QString(loc.decimalPoint()));
124 return from_utf8(lyxrc.default_decimal_sep);
128 bool Language::readLanguage(Lexer & lex)
144 LA_POSTBABELPREAMBLE,
154 // Keep these sorted alphabetically!
155 LexerKeyword languageTags[] = {
156 { "activechars", LA_ACTIVECHARS },
157 { "babelname", LA_BABELNAME },
158 { "dateformats", LA_DATEFORMATS },
159 { "encoding", LA_ENCODING },
161 { "fontencoding", LA_FONTENC },
162 { "guiname", LA_GUINAME },
163 { "hasguisupport", LA_HAS_GUI_SUPPORT },
164 { "internalencoding", LA_INTERNAL_ENC },
165 { "langcode", LA_LANG_CODE },
166 { "langvariety", LA_LANG_VARIETY },
167 { "polyglossianame", LA_POLYGLOSSIANAME },
168 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
169 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
170 { "prebabelpreamble", LA_PREBABELPREAMBLE },
171 { "provides", LA_PROVIDES },
172 { "quotestyle", LA_QUOTESTYLE },
173 { "requires", LA_REQUIRES },
175 { "wordwrap", LA_WORDWRAP },
176 { "xindyname", LA_XINDYNAME }
180 bool finished = false;
181 lex.pushTable(languageTags);
182 // parse style section
183 while (!finished && lex.isOK() && !error) {
185 // See comment in LyXRC.cpp.
187 case Lexer::LEX_FEOF:
190 case Lexer::LEX_UNDEF: // parse error
191 lex.printError("Unknown language tag `$$Token'");
198 switch (static_cast<LanguageTags>(le)) {
199 case LA_END: // end of structure
205 case LA_POLYGLOSSIANAME:
206 lex >> polyglossia_name_;
208 case LA_POLYGLOSSIAOPTS:
209 lex >> polyglossia_opts_;
218 lex >> active_chars_;
225 vector<string> const fe =
226 getVectorFromString(lex.getString(true), "|");
227 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
230 case LA_DATEFORMATS: {
232 vector<string> const df =
233 getVectorFromString(trim(lex.getString(true), "\""), "|");
234 dateformats_.insert(dateformats_.end(), df.begin(), df.end());
240 case LA_HAS_GUI_SUPPORT:
241 lex >> has_gui_support_;
243 case LA_INTERNAL_ENC:
244 lex >> internal_enc_;
249 case LA_LANG_VARIETY:
252 case LA_POSTBABELPREAMBLE:
253 babel_postsettings_ =
254 lex.getLongString(from_ascii("EndPostBabelPreamble"));
256 case LA_PREBABELPREAMBLE:
258 lex.getLongString(from_ascii("EndPreBabelPreamble"));
275 return finished && !error;
279 bool Language::read(Lexer & lex)
282 internal_enc_ = false;
283 rightToLeft_ = false;
286 lex.printError("No name given for language: `$$Token'.");
290 lang_ = lex.getString();
291 LYXERR(Debug::INFO, "Reading language " << lang_);
292 if (!readLanguage(lex)) {
293 LYXERR0("Error parsing language `" << lang_ << '\'');
297 encoding_ = encodings.fromLyXName(encodingStr_);
298 if (!encoding_ && !encodingStr_.empty()) {
299 encoding_ = encodings.fromLyXName("iso8859-1");
300 LYXERR0("Unknown encoding " << encodingStr_);
302 if (fontenc_.empty())
303 fontenc_.push_back("ASCII");
304 if (dateformats_.empty()) {
305 dateformats_.push_back("MMMM dd, yyyy");
306 dateformats_.push_back("MMM dd, yyyy");
307 dateformats_.push_back("M/d/yyyy");
313 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
315 for (auto const & t : trans) {
317 || layoutTranslations_.find(t.first) == layoutTranslations_.end())
318 layoutTranslations_[t.first] = t.second;
323 void Languages::read(FileName const & filename)
326 lex.setFile(filename);
327 lex.setContext("Languages::read");
331 case Lexer::LEX_FEOF:
337 if (lex.getString() != "Language") {
338 lex.printError("Unknown Language tag `$$Token'");
345 if (l.lang() == "latex") {
346 // Check if latex language was not already defined.
347 LASSERT(latex_language == nullptr, continue);
348 static const Language latex_lang = l;
349 latex_language = &latex_lang;
350 } else if (l.lang() == "ignore") {
351 // Check if ignore language was not already defined.
352 LASSERT(ignore_language == nullptr, continue);
353 static const Language ignore_lang = l;
354 ignore_language = &ignore_lang;
356 languagelist_[l.lang()] = l;
359 default_language = getLanguage("english");
360 if (!default_language) {
361 LYXERR0("Default language \"english\" not found!");
362 default_language = &(*languagelist_.begin()).second;
363 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
366 // Read layout translations
367 FileName const path = libFileSearch(string(), "layouttranslations");
368 readLayoutTranslations(path);
374 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
377 if (lex.checkFor("End"))
381 string const key = lex.getString();
384 docstring const val = lex.getDocString();
394 VeryApproximateMatch,
399 Match match(string const & code, Language const & lang)
401 // we need to mimic gettext: code can be a two-letter code, which
402 // should match all variants, e.g. "de" should match "de_DE",
404 // special case for chinese:
405 // simplified => code == "zh_CN", langcode == "zh_CN"
406 // traditional => code == "zh_TW", langcode == "zh_CN"
407 string const variety = lang.variety();
408 string const langcode = variety.empty() ?
409 lang.code() : lang.code() + '_' + variety;
410 string const name = lang.lang();
411 if ((code == langcode && name != "chinese-traditional")
412 || (code == "zh_TW" && name == "chinese-traditional"))
414 if ((code.size() == 2) && (langcode.size() > 2)
415 && (code + '_' == langcode.substr(0, 3)))
416 return ApproximateMatch;
417 if (code.substr(0,2) == langcode.substr(0,2))
418 return VeryApproximateMatch;
426 Language const * Languages::getFromCode(string const & code) const
428 // 1/ exact match with any known language
429 for (auto const & l : languagelist_) {
430 if (match(code, l.second) == ExactMatch)
434 // 2/ approximate with any known language
435 for (auto const & l : languagelist_) {
436 if (match(code, l.second) == ApproximateMatch)
443 Language const * Languages::getFromCode(string const & code,
444 set<Language const *> const & tryfirst) const
446 // 1/ exact match with tryfirst list
447 for (auto const * lptr : tryfirst) {
448 if (match(code, *lptr) == ExactMatch)
452 // 2/ approximate match with tryfirst list
453 for (auto const * lptr : tryfirst) {
454 Match const m = match(code, *lptr);
455 if (m == ApproximateMatch || m == VeryApproximateMatch)
459 // 3/ stricter match in all languages
460 return getFromCode(code);
462 LYXERR0("Unknown language `" << code << "'");
467 void Languages::readLayoutTranslations(support::FileName const & filename)
470 lex.setFile(filename);
471 lex.setContext("Languages::read");
473 // 1) read all translations (exact and approximate matches) into trans
474 std::map<string, Language::TranslationMap> trans;
476 if (!lex.checkFor("Translation")) {
478 lex.printError("Unknown layout translation tag `$$Token'");
483 string const code = lex.getString();
484 bool found = getFromCode(code);
486 lex.printError("Unknown language `" + code + "'");
489 if (!readTranslations(lex, trans[code])) {
490 lex.printError("Could not read layout translations for language `"
496 // 2) merge all translations into the languages
497 // exact translations overwrite approximate ones
498 for (auto & tr : trans) {
499 for (auto & lang : languagelist_) {
500 Match const m = match(tr.first, lang.second);
503 lang.second.readLayoutTranslations(tr.second, m == ExactMatch);
510 Language const * Languages::getLanguage(string const & language) const
512 if (language == "reset")
513 return reset_language;
514 if (language == "ignore")
515 return ignore_language;
516 const_iterator it = languagelist_.find(language);
517 return it == languagelist_.end() ? reset_language : &it->second;