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 = 0;
41 Language const * default_language = 0;
42 Language const * latex_language = 0;
43 Language const * reset_language = 0;
46 bool Language::isPolyglossiaExclusive() const
48 return babel().empty() && !polyglossia().empty() && requires().empty();
52 bool Language::isBabelExclusive() const
54 return !babel().empty() && polyglossia().empty() && requires().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)
143 LA_POSTBABELPREAMBLE,
153 // Keep these sorted alphabetically!
154 LexerKeyword languageTags[] = {
155 { "activechars", LA_ACTIVECHARS },
156 { "babelname", LA_BABELNAME },
157 { "dateformats", LA_DATEFORMATS },
158 { "encoding", LA_ENCODING },
160 { "fontencoding", LA_FONTENC },
161 { "guiname", LA_GUINAME },
162 { "hasguisupport", LA_HAS_GUI_SUPPORT },
163 { "internalencoding", LA_INTERNAL_ENC },
164 { "langcode", LA_LANG_CODE },
165 { "langvariety", LA_LANG_VARIETY },
166 { "polyglossianame", LA_POLYGLOSSIANAME },
167 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
168 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
169 { "prebabelpreamble", LA_PREBABELPREAMBLE },
170 { "provides", LA_PROVIDES },
171 { "quotestyle", LA_QUOTESTYLE },
172 { "requires", LA_REQUIRES },
174 {"wordwrap", LA_WORDWRAP }
178 bool finished = false;
179 lex.pushTable(languageTags);
180 // parse style section
181 while (!finished && lex.isOK() && !error) {
183 // See comment in LyXRC.cpp.
185 case Lexer::LEX_FEOF:
188 case Lexer::LEX_UNDEF: // parse error
189 lex.printError("Unknown language tag `$$Token'");
196 switch (static_cast<LanguageTags>(le)) {
197 case LA_END: // end of structure
203 case LA_POLYGLOSSIANAME:
204 lex >> polyglossia_name_;
206 case LA_POLYGLOSSIAOPTS:
207 lex >> polyglossia_opts_;
213 lex >> active_chars_;
220 vector<string> const fe =
221 getVectorFromString(lex.getString(true), "|");
222 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
225 case LA_DATEFORMATS: {
227 vector<string> const df =
228 getVectorFromString(trim(lex.getString(true), "\""), "|");
229 dateformats_.insert(dateformats_.end(), df.begin(), df.end());
235 case LA_HAS_GUI_SUPPORT:
236 lex >> has_gui_support_;
238 case LA_INTERNAL_ENC:
239 lex >> internal_enc_;
244 case LA_LANG_VARIETY:
247 case LA_POSTBABELPREAMBLE:
248 babel_postsettings_ =
249 lex.getLongString(from_ascii("EndPostBabelPreamble"));
251 case LA_PREBABELPREAMBLE:
253 lex.getLongString(from_ascii("EndPreBabelPreamble"));
270 return finished && !error;
274 bool Language::read(Lexer & lex)
281 lex.printError("No name given for language: `$$Token'.");
285 lang_ = lex.getString();
286 LYXERR(Debug::INFO, "Reading language " << lang_);
287 if (!readLanguage(lex)) {
288 LYXERR0("Error parsing language `" << lang_ << '\'');
292 encoding_ = encodings.fromLyXName(encodingStr_);
293 if (!encoding_ && !encodingStr_.empty()) {
294 encoding_ = encodings.fromLyXName("iso8859-1");
295 LYXERR0("Unknown encoding " << encodingStr_);
297 if (fontenc_.empty())
298 fontenc_.push_back("ASCII");
299 if (dateformats_.empty()) {
300 dateformats_.push_back("MMMM dd, yyyy");
301 dateformats_.push_back("MMM dd, yyyy");
302 dateformats_.push_back("M/d/yyyy");
308 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
310 for (auto const & t : trans) {
312 || layoutTranslations_.find(t.first) == layoutTranslations_.end())
313 layoutTranslations_[t.first] = t.second;
318 void Languages::read(FileName const & filename)
321 lex.setFile(filename);
322 lex.setContext("Languages::read");
326 case Lexer::LEX_FEOF:
332 if (lex.getString() != "Language") {
333 lex.printError("Unknown Language tag `$$Token'");
340 if (l.lang() == "latex") {
341 // Check if latex language was not already defined.
342 LASSERT(latex_language == 0, continue);
343 static const Language latex_lang = l;
344 latex_language = &latex_lang;
345 } else if (l.lang() == "ignore") {
346 // Check if ignore language was not already defined.
347 LASSERT(ignore_language == 0, continue);
348 static const Language ignore_lang = l;
349 ignore_language = &ignore_lang;
351 languagelist_[l.lang()] = l;
354 default_language = getLanguage("english");
355 if (!default_language) {
356 LYXERR0("Default language \"english\" not found!");
357 default_language = &(*languagelist_.begin()).second;
358 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
361 // Read layout translations
362 FileName const path = libFileSearch(string(), "layouttranslations");
363 readLayoutTranslations(path);
369 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
372 if (lex.checkFor("End"))
376 string const key = lex.getString();
379 docstring const val = lex.getDocString();
389 VeryApproximateMatch,
394 Match match(string const & code, Language const & lang)
396 // we need to mimic gettext: code can be a two-letter code, which
397 // should match all variants, e.g. "de" should match "de_DE",
399 // special case for chinese:
400 // simplified => code == "zh_CN", langcode == "zh_CN"
401 // traditional => code == "zh_TW", langcode == "zh_CN"
402 string const variety = lang.variety();
403 string const langcode = variety.empty() ?
404 lang.code() : lang.code() + '_' + variety;
405 string const name = lang.lang();
406 if ((code == langcode && name != "chinese-traditional")
407 || (code == "zh_TW" && name == "chinese-traditional"))
409 if ((code.size() == 2) && (langcode.size() > 2)
410 && (code + '_' == langcode.substr(0, 3)))
411 return ApproximateMatch;
412 if (code.substr(0,2) == langcode.substr(0,2))
413 return VeryApproximateMatch;
421 Language const * Languages::getFromCode(string const & code) const
423 // 1/ exact match with any known language
424 for (auto const & l : languagelist_) {
425 if (match(code, l.second) == ExactMatch)
429 // 2/ approximate with any known language
430 for (auto const & l : languagelist_) {
431 if (match(code, l.second) == ApproximateMatch)
438 Language const * Languages::getFromCode(string const & code,
439 set<Language const *> const & tryfirst) const
441 // 1/ exact match with tryfirst list
442 for (auto const * lptr : tryfirst) {
443 if (match(code, *lptr) == ExactMatch)
447 // 2/ approximate match with tryfirst list
448 for (auto const * lptr : tryfirst) {
449 Match const m = match(code, *lptr);
450 if (m == ApproximateMatch || m == VeryApproximateMatch)
454 // 3/ stricter match in all languages
455 return getFromCode(code);
457 LYXERR0("Unknown language `" << code << "'");
462 void Languages::readLayoutTranslations(support::FileName const & filename)
465 lex.setFile(filename);
466 lex.setContext("Languages::read");
468 // 1) read all translations (exact and approximate matches) into trans
469 std::map<string, Language::TranslationMap> trans;
471 if (!lex.checkFor("Translation")) {
473 lex.printError("Unknown layout translation tag `$$Token'");
478 string const code = lex.getString();
479 bool found = getFromCode(code);
481 lex.printError("Unknown language `" + code + "'");
484 if (!readTranslations(lex, trans[code])) {
485 lex.printError("Could not read layout translations for language `"
491 // 2) merge all translations into the languages
492 // exact translations overwrite approximate ones
493 for (auto & tr : trans) {
494 for (auto & lang : languagelist_) {
495 Match const m = match(tr.first, lang.second);
498 lang.second.readLayoutTranslations(tr.second, m == ExactMatch);
505 Language const * Languages::getLanguage(string const & language) const
507 if (language == "reset")
508 return reset_language;
509 if (language == "ignore")
510 return ignore_language;
511 const_iterator it = languagelist_.find(language);
512 return it == languagelist_.end() ? reset_language : &it->second;