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 // Don't use LaTeX fonts, so just return the language's preferred
78 // (although this is not used with nonTeXFonts anyway).
79 if (params.useNonTeXFonts)
80 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
82 // Determine optimal font encoding
83 // We check whether the used rm font supports an encoding our language supports
84 LaTeXFont const & lf =
85 theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
86 vector<string> const lfe = lf.fontencs();
87 for (auto & fe : fontenc_) {
88 // ASCII means: support all T* encodings plus OT1
90 for (auto & afe : lfe) {
91 if (afe == "OT1" || prefixIs(afe, "T"))
92 // we found a suitable one; return that.
96 // For other encodings, just check whether the font supports it
97 if (lf.hasFontenc(fe))
100 // We did not find a suitable one; just take the first in the list,
101 // the priorized one (which is "T1" for ASCII).
102 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
106 string Language::dateFormat(size_t i) const
108 if (i > dateformats_.size())
110 return dateformats_.at(i);
114 bool Language::readLanguage(Lexer & lex)
129 LA_POSTBABELPREAMBLE,
137 // Keep these sorted alphabetically!
138 LexerKeyword languageTags[] = {
139 { "babelname", LA_BABELNAME },
140 { "dateformats", LA_DATEFORMATS },
141 { "encoding", LA_ENCODING },
143 { "fontencoding", LA_FONTENC },
144 { "guiname", LA_GUINAME },
145 { "hasguisupport", LA_HAS_GUI_SUPPORT },
146 { "internalencoding", LA_INTERNAL_ENC },
147 { "langcode", LA_LANG_CODE },
148 { "langvariety", LA_LANG_VARIETY },
149 { "polyglossianame", LA_POLYGLOSSIANAME },
150 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
151 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
152 { "prebabelpreamble", LA_PREBABELPREAMBLE },
153 { "provides", LA_PROVIDES },
154 { "quotestyle", LA_QUOTESTYLE },
155 { "requires", LA_REQUIRES },
160 bool finished = false;
161 lex.pushTable(languageTags);
162 // parse style section
163 while (!finished && lex.isOK() && !error) {
165 // See comment in LyXRC.cpp.
167 case Lexer::LEX_FEOF:
170 case Lexer::LEX_UNDEF: // parse error
171 lex.printError("Unknown language tag `$$Token'");
178 switch (static_cast<LanguageTags>(le)) {
179 case LA_END: // end of structure
185 case LA_POLYGLOSSIANAME:
186 lex >> polyglossia_name_;
188 case LA_POLYGLOSSIAOPTS:
189 lex >> polyglossia_opts_;
199 vector<string> const fe =
200 getVectorFromString(lex.getString(true), "|");
201 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
204 case LA_DATEFORMATS: {
206 vector<string> const df =
207 getVectorFromString(trim(lex.getString(true), "\""), "|");
208 dateformats_.insert(dateformats_.end(), df.begin(), df.end());
214 case LA_HAS_GUI_SUPPORT:
215 lex >> has_gui_support_;
217 case LA_INTERNAL_ENC:
218 lex >> internal_enc_;
223 case LA_LANG_VARIETY:
226 case LA_POSTBABELPREAMBLE:
227 babel_postsettings_ =
228 lex.getLongString(from_ascii("EndPostBabelPreamble"));
230 case LA_PREBABELPREAMBLE:
232 lex.getLongString(from_ascii("EndPreBabelPreamble"));
246 return finished && !error;
250 bool Language::read(Lexer & lex)
257 lex.printError("No name given for language: `$$Token'.");
261 lang_ = lex.getString();
262 LYXERR(Debug::INFO, "Reading language " << lang_);
263 if (!readLanguage(lex)) {
264 LYXERR0("Error parsing language `" << lang_ << '\'');
268 encoding_ = encodings.fromLyXName(encodingStr_);
269 if (!encoding_ && !encodingStr_.empty()) {
270 encoding_ = encodings.fromLyXName("iso8859-1");
271 LYXERR0("Unknown encoding " << encodingStr_);
273 if (fontenc_.empty())
274 fontenc_.push_back("ASCII");
275 if (dateformats_.empty()) {
276 dateformats_.push_back("MMMM dd, yyyy");
277 dateformats_.push_back("MMM dd, yyyy");
278 dateformats_.push_back("M/d/yyyy");
284 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
286 TranslationMap::const_iterator const end = trans.end();
287 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
289 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
290 layoutTranslations_[it->first] = it->second;
295 void Languages::read(FileName const & filename)
298 lex.setFile(filename);
299 lex.setContext("Languages::read");
303 case Lexer::LEX_FEOF:
309 if (lex.getString() != "Language") {
310 lex.printError("Unknown Language tag `$$Token'");
317 if (l.lang() == "latex") {
318 // Check if latex language was not already defined.
319 LASSERT(latex_language == 0, continue);
320 static const Language latex_lang = l;
321 latex_language = &latex_lang;
322 } else if (l.lang() == "ignore") {
323 // Check if ignore language was not already defined.
324 LASSERT(ignore_language == 0, continue);
325 static const Language ignore_lang = l;
326 ignore_language = &ignore_lang;
328 languagelist[l.lang()] = l;
331 default_language = getLanguage("english");
332 if (!default_language) {
333 LYXERR0("Default language \"english\" not found!");
334 default_language = &(*languagelist.begin()).second;
335 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
338 // Read layout translations
339 FileName const path = libFileSearch(string(), "layouttranslations");
340 readLayoutTranslations(path);
346 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
349 if (lex.checkFor("End"))
353 string const key = lex.getString();
356 docstring const val = lex.getDocString();
370 Match match(string const & code, Language const & lang)
372 // we need to mimic gettext: code can be a two-letter code, which
373 // should match all variants, e.g. "de" should match "de_DE",
375 // special case for chinese:
376 // simplified => code == "zh_CN", langcode == "zh_CN"
377 // traditional => code == "zh_TW", langcode == "zh_CN"
378 string const variety = lang.variety();
379 string const langcode = variety.empty() ?
380 lang.code() : lang.code() + '_' + variety;
381 string const name = lang.lang();
382 if ((code == langcode && name != "chinese-traditional")
383 || (code == "zh_TW" && name == "chinese-traditional"))
385 if ((code.size() == 2) && (langcode.size() > 2)
386 && (code + '_' == langcode.substr(0, 3)))
387 return ApproximateMatch;
395 Language const * Languages::getFromCode(string const & code) const
397 LanguageList::const_iterator const lbeg = languagelist.begin();
398 LanguageList::const_iterator const lend = languagelist.end();
399 // Try for exact match first
400 for (LanguageList::const_iterator lit = lbeg; lit != lend; ++lit) {
401 if (match(code, lit->second) == ExactMatch)
404 // If not found, look for lang prefix (without country) instead
405 for (LanguageList::const_iterator lit = lbeg; lit != lend; ++lit) {
406 if (match(code, lit->second) == ApproximateMatch)
409 LYXERR0("Unknown language `" + code + "'");
414 void Languages::readLayoutTranslations(support::FileName const & filename)
417 lex.setFile(filename);
418 lex.setContext("Languages::read");
420 // 1) read all translations (exact and approximate matches) into trans
421 typedef std::map<string, Language::TranslationMap> TransMap;
423 LanguageList::iterator const lbeg = languagelist.begin();
424 LanguageList::iterator const lend = languagelist.end();
426 if (!lex.checkFor("Translation")) {
428 lex.printError("Unknown layout translation tag `$$Token'");
433 string const code = lex.getString();
434 bool found = getFromCode(code);
436 lex.printError("Unknown language `" + code + "'");
439 if (!readTranslations(lex, trans[code])) {
440 lex.printError("Could not read layout translations for language `"
446 // 2) merge all translations into the languages
447 // exact translations overwrite approximate ones
448 TransMap::const_iterator const tbeg = trans.begin();
449 TransMap::const_iterator const tend = trans.end();
450 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
451 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
452 Match const m = match(tit->first, lit->second);
455 lit->second.readLayoutTranslations(tit->second,
463 Language const * Languages::getLanguage(string const & language) const
465 if (language == "reset")
466 return reset_language;
467 if (language == "ignore")
468 return ignore_language;
469 const_iterator it = languagelist.find(language);
470 return it == languagelist.end() ? reset_language : &it->second;