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 bool Language::readLanguage(Lexer & lex)
121 LA_POSTBABELPREAMBLE,
129 // Keep these sorted alphabetically!
130 LexerKeyword languageTags[] = {
131 { "asbabeloptions", LA_AS_BABELOPTS },
132 { "babelname", LA_BABELNAME },
133 { "encoding", LA_ENCODING },
135 { "fontencoding", LA_FONTENC },
136 { "guiname", LA_GUINAME },
137 { "hasguisupport", LA_HAS_GUI_SUPPORT },
138 { "internalencoding", LA_INTERNAL_ENC },
139 { "langcode", LA_LANG_CODE },
140 { "langvariety", LA_LANG_VARIETY },
141 { "polyglossianame", LA_POLYGLOSSIANAME },
142 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
143 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
144 { "prebabelpreamble", LA_PREBABELPREAMBLE },
145 { "provides", LA_PROVIDES },
146 { "quotestyle", LA_QUOTESTYLE },
147 { "requires", LA_REQUIRES },
152 bool finished = false;
153 lex.pushTable(languageTags);
154 // parse style section
155 while (!finished && lex.isOK() && !error) {
157 // See comment in LyXRC.cpp.
159 case Lexer::LEX_FEOF:
162 case Lexer::LEX_UNDEF: // parse error
163 lex.printError("Unknown language tag `$$Token'");
170 switch (static_cast<LanguageTags>(le)) {
171 case LA_END: // end of structure
174 case LA_AS_BABELOPTS:
175 lex >> as_babel_options_;
180 case LA_POLYGLOSSIANAME:
181 lex >> polyglossia_name_;
183 case LA_POLYGLOSSIAOPTS:
184 lex >> polyglossia_opts_;
194 vector<string> const fe =
195 getVectorFromString(lex.getString(true), "|");
196 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
202 case LA_HAS_GUI_SUPPORT:
203 lex >> has_gui_support_;
205 case LA_INTERNAL_ENC:
206 lex >> internal_enc_;
211 case LA_LANG_VARIETY:
214 case LA_POSTBABELPREAMBLE:
215 babel_postsettings_ =
216 lex.getLongString(from_ascii("EndPostBabelPreamble"));
218 case LA_PREBABELPREAMBLE:
220 lex.getLongString(from_ascii("EndPreBabelPreamble"));
234 return finished && !error;
238 bool Language::read(Lexer & lex)
240 as_babel_options_ = 0;
246 lex.printError("No name given for language: `$$Token'.");
250 lang_ = lex.getString();
251 LYXERR(Debug::INFO, "Reading language " << lang_);
252 if (!readLanguage(lex)) {
253 LYXERR0("Error parsing language `" << lang_ << '\'');
257 encoding_ = encodings.fromLyXName(encodingStr_);
258 if (!encoding_ && !encodingStr_.empty()) {
259 encoding_ = encodings.fromLyXName("iso8859-1");
260 LYXERR0("Unknown encoding " << encodingStr_);
262 if (fontenc_.empty())
263 fontenc_.push_back("ASCII");
268 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
270 TranslationMap::const_iterator const end = trans.end();
271 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
273 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
274 layoutTranslations_[it->first] = it->second;
279 void Languages::read(FileName const & filename)
282 lex.setFile(filename);
283 lex.setContext("Languages::read");
287 case Lexer::LEX_FEOF:
293 if (lex.getString() != "Language") {
294 lex.printError("Unknown Language tag `$$Token'");
301 if (l.lang() == "latex") {
302 // Check if latex language was not already defined.
303 LASSERT(latex_language == 0, continue);
304 static const Language latex_lang = l;
305 latex_language = &latex_lang;
306 } else if (l.lang() == "ignore") {
307 // Check if ignore language was not already defined.
308 LASSERT(ignore_language == 0, continue);
309 static const Language ignore_lang = l;
310 ignore_language = &ignore_lang;
312 languagelist[l.lang()] = l;
315 default_language = getLanguage("english");
316 if (!default_language) {
317 LYXERR0("Default language \"english\" not found!");
318 default_language = &(*languagelist.begin()).second;
319 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
322 // Read layout translations
323 FileName const path = libFileSearch(string(), "layouttranslations");
324 readLayoutTranslations(path);
330 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
333 if (lex.checkFor("End"))
337 string const key = lex.getString();
340 docstring const val = lex.getDocString();
354 Match match(string const & code, Language const & lang)
356 // we need to mimic gettext: code can be a two-letter code, which
357 // should match all variants, e.g. "de" should match "de_DE",
359 // special case for chinese:
360 // simplified => code == "zh_CN", langcode == "zh_CN"
361 // traditional => code == "zh_TW", langcode == "zh_CN"
362 string const variety = lang.variety();
363 string const langcode = variety.empty() ?
364 lang.code() : lang.code() + '_' + variety;
365 string const name = lang.lang();
366 if ((code == langcode && name != "chinese-traditional")
367 || (code == "zh_TW" && name == "chinese-traditional"))
369 if ((code.size() == 2) && (langcode.size() > 2)
370 && (code + '_' == langcode.substr(0, 3)))
371 return ApproximateMatch;
378 void Languages::readLayoutTranslations(support::FileName const & filename)
381 lex.setFile(filename);
382 lex.setContext("Languages::read");
384 // 1) read all translations (exact and approximate matches) into trans
385 typedef std::map<string, Language::TranslationMap> TransMap;
387 LanguageList::iterator const lbeg = languagelist.begin();
388 LanguageList::iterator const lend = languagelist.end();
390 if (!lex.checkFor("Translation")) {
392 lex.printError("Unknown layout translation tag `$$Token'");
397 string const code = lex.getString();
399 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
400 if (match(code, lit->second) != NoMatch) {
406 lex.printError("Unknown language `" + code + "'");
409 if (!readTranslations(lex, trans[code])) {
410 lex.printError("Could not read layout translations for language `"
416 // 2) merge all translations into the languages
417 // exact translations overwrite approximate ones
418 TransMap::const_iterator const tbeg = trans.begin();
419 TransMap::const_iterator const tend = trans.end();
420 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
421 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
422 Match const m = match(tit->first, lit->second);
425 lit->second.readLayoutTranslations(tit->second,
433 Language const * Languages::getLanguage(string const & language) const
435 if (language == "reset")
436 return reset_language;
437 if (language == "ignore")
438 return ignore_language;
439 const_iterator it = languagelist.find(language);
440 return it == languagelist.end() ? reset_language : &it->second;