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 LaTeXFont const & lf =
80 theLaTeXFonts().getLaTeXFont(from_ascii(params.fontsRoman()));
81 vector<string> const lfe = lf.fontencs();
82 for (auto & fe : fontenc_) {
83 // ASCII means: support all T* encodings plus OT1
85 for (auto & afe : lfe) {
86 if (afe == "OT1" || prefixIs(afe, "T"))
87 // we found a suitable one; return that.
91 // For other encodings, just check whether the font supports it
92 if (lf.hasFontenc(fe))
95 // We did not find a suitable one; just take the first in the list,
96 // the priorized one (which is "T1" for ASCII).
97 return fontenc_.front() == "ASCII" ? "T1" : fontenc_.front();
101 bool Language::readLanguage(Lexer & lex)
116 LA_POSTBABELPREAMBLE,
124 // Keep these sorted alphabetically!
125 LexerKeyword languageTags[] = {
126 { "asbabeloptions", LA_AS_BABELOPTS },
127 { "babelname", LA_BABELNAME },
128 { "encoding", LA_ENCODING },
130 { "fontencoding", LA_FONTENC },
131 { "guiname", LA_GUINAME },
132 { "hasguisupport", LA_HAS_GUI_SUPPORT },
133 { "internalencoding", LA_INTERNAL_ENC },
134 { "langcode", LA_LANG_CODE },
135 { "langvariety", LA_LANG_VARIETY },
136 { "polyglossianame", LA_POLYGLOSSIANAME },
137 { "polyglossiaopts", LA_POLYGLOSSIAOPTS },
138 { "postbabelpreamble", LA_POSTBABELPREAMBLE },
139 { "prebabelpreamble", LA_PREBABELPREAMBLE },
140 { "provides", LA_PROVIDES },
141 { "quotestyle", LA_QUOTESTYLE },
142 { "requires", LA_REQUIRES },
147 bool finished = false;
148 lex.pushTable(languageTags);
149 // parse style section
150 while (!finished && lex.isOK() && !error) {
152 // See comment in LyXRC.cpp.
154 case Lexer::LEX_FEOF:
157 case Lexer::LEX_UNDEF: // parse error
158 lex.printError("Unknown language tag `$$Token'");
165 switch (static_cast<LanguageTags>(le)) {
166 case LA_END: // end of structure
169 case LA_AS_BABELOPTS:
170 lex >> as_babel_options_;
175 case LA_POLYGLOSSIANAME:
176 lex >> polyglossia_name_;
178 case LA_POLYGLOSSIAOPTS:
179 lex >> polyglossia_opts_;
189 vector<string> const fe =
190 getVectorFromString(lex.getString(true), "|");
191 fontenc_.insert(fontenc_.end(), fe.begin(), fe.end());
197 case LA_HAS_GUI_SUPPORT:
198 lex >> has_gui_support_;
200 case LA_INTERNAL_ENC:
201 lex >> internal_enc_;
206 case LA_LANG_VARIETY:
209 case LA_POSTBABELPREAMBLE:
210 babel_postsettings_ =
211 lex.getLongString(from_ascii("EndPostBabelPreamble"));
213 case LA_PREBABELPREAMBLE:
215 lex.getLongString(from_ascii("EndPreBabelPreamble"));
229 return finished && !error;
233 bool Language::read(Lexer & lex)
235 as_babel_options_ = 0;
241 lex.printError("No name given for language: `$$Token'.");
245 lang_ = lex.getString();
246 LYXERR(Debug::INFO, "Reading language " << lang_);
247 if (!readLanguage(lex)) {
248 LYXERR0("Error parsing language `" << lang_ << '\'');
252 encoding_ = encodings.fromLyXName(encodingStr_);
253 if (!encoding_ && !encodingStr_.empty()) {
254 encoding_ = encodings.fromLyXName("iso8859-1");
255 LYXERR0("Unknown encoding " << encodingStr_);
257 if (fontenc_.empty())
258 fontenc_.push_back("ASCII");
263 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
265 TranslationMap::const_iterator const end = trans.end();
266 for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
268 || layoutTranslations_.find(it->first) == layoutTranslations_.end())
269 layoutTranslations_[it->first] = it->second;
274 void Languages::read(FileName const & filename)
277 lex.setFile(filename);
278 lex.setContext("Languages::read");
282 case Lexer::LEX_FEOF:
288 if (lex.getString() != "Language") {
289 lex.printError("Unknown Language tag `$$Token'");
296 if (l.lang() == "latex") {
297 // Check if latex language was not already defined.
298 LASSERT(latex_language == 0, continue);
299 static const Language latex_lang = l;
300 latex_language = &latex_lang;
301 } else if (l.lang() == "ignore") {
302 // Check if ignore language was not already defined.
303 LASSERT(ignore_language == 0, continue);
304 static const Language ignore_lang = l;
305 ignore_language = &ignore_lang;
307 languagelist[l.lang()] = l;
310 default_language = getLanguage("english");
311 if (!default_language) {
312 LYXERR0("Default language \"english\" not found!");
313 default_language = &(*languagelist.begin()).second;
314 LYXERR0("Using \"" << default_language->lang() << "\" instead!");
317 // Read layout translations
318 FileName const path = libFileSearch(string(), "layouttranslations");
319 readLayoutTranslations(path);
325 bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
328 if (lex.checkFor("End"))
332 string const key = lex.getString();
335 docstring const val = lex.getDocString();
349 Match match(string const & code, Language const & lang)
351 // we need to mimic gettext: code can be a two-letter code, which
352 // should match all variants, e.g. "de" should match "de_DE",
354 // special case for chinese:
355 // simplified => code == "zh_CN", langcode == "zh_CN"
356 // traditional => code == "zh_TW", langcode == "zh_CN"
357 string const variety = lang.variety();
358 string const langcode = variety.empty() ?
359 lang.code() : lang.code() + '_' + variety;
360 string const name = lang.lang();
361 if ((code == langcode && name != "chinese-traditional")
362 || (code == "zh_TW" && name == "chinese-traditional"))
364 if ((code.size() == 2) && (langcode.size() > 2)
365 && (code + '_' == langcode.substr(0, 3)))
366 return ApproximateMatch;
373 void Languages::readLayoutTranslations(support::FileName const & filename)
376 lex.setFile(filename);
377 lex.setContext("Languages::read");
379 // 1) read all translations (exact and approximate matches) into trans
380 typedef std::map<string, Language::TranslationMap> TransMap;
382 LanguageList::iterator const lbeg = languagelist.begin();
383 LanguageList::iterator const lend = languagelist.end();
385 if (!lex.checkFor("Translation")) {
387 lex.printError("Unknown layout translation tag `$$Token'");
392 string const code = lex.getString();
394 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
395 if (match(code, lit->second) != NoMatch) {
401 lex.printError("Unknown language `" + code + "'");
404 if (!readTranslations(lex, trans[code])) {
405 lex.printError("Could not read layout translations for language `"
411 // 2) merge all translations into the languages
412 // exact translations overwrite approximate ones
413 TransMap::const_iterator const tbeg = trans.begin();
414 TransMap::const_iterator const tend = trans.end();
415 for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
416 for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
417 Match const m = match(tit->first, lit->second);
420 lit->second.readLayoutTranslations(tit->second,
428 Language const * Languages::getLanguage(string const & language) const
430 if (language == "reset")
431 return reset_language;
432 if (language == "ignore")
433 return ignore_language;
434 const_iterator it = languagelist.find(language);
435 return it == languagelist.end() ? reset_language : &it->second;