]> git.lyx.org Git - lyx.git/blobdiff - src/Language.cpp
some less language hardcoding
[lyx.git] / src / Language.cpp
index 8f984325d8448e4c252e2a0efde5f0e8f90ba79b..ddb63e3b28f2983e10bd3392c84e04d46878ab3c 100644 (file)
 #include "support/debug.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/Messages.h"
 
 using namespace std;
 using namespace lyx::support;
 
-
 namespace lyx {
 
 Languages languages;
-Language ignore_lang;
-Language latex_lang;
-Language const * default_language;
-Language const * ignore_language = &ignore_lang;
-Language const * latex_language = &latex_lang;
+Language const * ignore_language = 0;
+Language const * default_language = 0;
+Language const * latex_language = 0;
 Language const * reset_language = 0;
 
 
@@ -55,7 +53,9 @@ docstring const Language::translateLayout(string const & m) const
        if (it != layoutTranslations_.end())
                return it->second;
 
-       return from_ascii(m);
+       docstring t = from_ascii(m);
+       cleanTranslation(t);
+       return t;
 }
 
 
@@ -74,6 +74,7 @@ bool Language::readLanguage(Lexer & lex)
                LA_POLYGLOSSIAOPTS,
                LA_POSTBABELPREAMBLE,
                LA_PREBABELPREAMBLE,
+               LA_REQUIRES,
                LA_RTL
        };
 
@@ -91,6 +92,7 @@ bool Language::readLanguage(Lexer & lex)
                { "polyglossiaopts",      LA_POLYGLOSSIAOPTS },
                { "postbabelpreamble",    LA_POSTBABELPREAMBLE },
                { "prebabelpreamble",     LA_PREBABELPREAMBLE },
+               { "requires",             LA_REQUIRES },
                { "rtl",                  LA_RTL }
        };
 
@@ -152,6 +154,9 @@ bool Language::readLanguage(Lexer & lex)
                        babel_presettings_ =
                                lex.getLongString("EndPreBabelPreamble");
                        break;
+               case LA_REQUIRES:
+                       lex >> requires_;
+                       break;
                case LA_RTL:
                        lex >> rightToLeft_;
                        break;
@@ -194,57 +199,14 @@ bool Language::read(Lexer & lex)
 }
 
 
-namespace {
-
-bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
-{
-       while (lex.isOK()) {
-               if (lex.checkFor("End"))
-                       break;
-               if (!lex.next(true))
-                       return false;
-               string const key = lex.getString();
-               if (!lex.next(true))
-                       return false;
-               docstring const val = lex.getDocString();
-               trans[key] = val;
-       }
-       return true;
-}
-
-enum Match{NoMatch, ApproximateMatch, ExactMatch};
-
-Match match(string const & code, Language const & lang)
-{
-       // we need to mimic gettext: code can be a two-letter code, which
-       // should match all variants, e.g. "de" should match "de_DE",
-       // "de_AT" etc.
-       // special case for chinese:
-       // simplified  => code == "zh_CN", langcode == "zh_CN"
-       // traditional => code == "zh_TW", langcode == "zh_CN"
-       string const variety = lang.variety();
-       string const langcode = variety.empty() ?
-                               lang.code() : lang.code() + '_' + variety;
-       string const name = lang.lang();
-       if ((code == langcode && name != "chinese-traditional") ||
-           (code == "zh_TW"  && name == "chinese-traditional"))
-               return ExactMatch;
-       if ((code.size() == 2 && langcode.size() > 2 &&
-            code + '_' == langcode.substr(0, 3)))
-               return ApproximateMatch;
-       return NoMatch;
-}
-
-}
-
-
 void Language::readLayoutTranslations(Language::TranslationMap const & trans, bool replace)
 {
        TranslationMap::const_iterator const end = trans.end();
-       for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it)
-               if (replace ||
-                   layoutTranslations_.find(it->first) == layoutTranslations_.end())
+       for (TranslationMap::const_iterator it = trans.begin(); it != end; ++it) {
+               if (replace
+                       || layoutTranslations_.find(it->first) == layoutTranslations_.end())
                        layoutTranslations_[it->first] = it->second;
+       }
 }
 
 
@@ -270,11 +232,17 @@ void Languages::read(FileName const & filename)
                l.read(lex);
                if (!lex)
                        break;
-               if (l.lang() == "latex")
-                       latex_lang = l;
-               else if (l.lang() == "ignore")
-                       ignore_lang = l;
-               else
+               if (l.lang() == "latex") {
+                       // Check if latex language was not already defined.
+                       LASSERT(latex_language == 0, continue);
+                       static const Language latex_lang = l;
+                       latex_language = &latex_lang;
+               } else if (l.lang() == "ignore") {
+                       // Check if ignore language was not already defined.
+                       LASSERT(ignore_language == 0, continue);
+                       static const Language ignore_lang = l;
+                       ignore_language = &ignore_lang;
+               } else
                        languagelist[l.lang()] = l;
        }
 
@@ -294,6 +262,56 @@ void Languages::read(FileName const & filename)
 }
 
 
+namespace {
+
+bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
+{
+       while (lex.isOK()) {
+               if (lex.checkFor("End"))
+                       break;
+               if (!lex.next(true))
+                       return false;
+               string const key = lex.getString();
+               if (!lex.next(true))
+                       return false;
+               docstring const val = lex.getDocString();
+               trans[key] = val;
+       }
+       return true;
+}
+
+
+enum Match {
+       NoMatch,
+       ApproximateMatch,
+       ExactMatch
+};
+
+
+Match match(string const & code, Language const & lang)
+{
+       // we need to mimic gettext: code can be a two-letter code, which
+       // should match all variants, e.g. "de" should match "de_DE",
+       // "de_AT" etc.
+       // special case for chinese:
+       // simplified  => code == "zh_CN", langcode == "zh_CN"
+       // traditional => code == "zh_TW", langcode == "zh_CN"
+       string const variety = lang.variety();
+       string const langcode = variety.empty() ?
+                               lang.code() : lang.code() + '_' + variety;
+       string const name = lang.lang();
+       if ((code == langcode && name != "chinese-traditional")
+               || (code == "zh_TW"  && name == "chinese-traditional"))
+               return ExactMatch;
+       if ((code.size() == 2) && (langcode.size() > 2)
+               && (code + '_' == langcode.substr(0, 3)))
+               return ApproximateMatch;
+       return NoMatch;
+}
+
+}
+
+
 void Languages::readLayoutTranslations(support::FileName const & filename)
 {
        Lexer lex;
@@ -314,22 +332,22 @@ void Languages::readLayoutTranslations(support::FileName const & filename)
                if (!lex.next(true))
                        break;
                string const code = lex.getString();
-               bool readit = false;
+               bool found = false;
                for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
                        if (match(code, lit->second) != NoMatch) {
-                               if (readTranslations(lex, trans[code]))
-                                       readit = true;
-                               else
-                                       lex.printError("Could not read layout "
-                                                      "translations for language "
-                                                      "`" + code + "'");
+                               found = true;
                                break;
                        }
                }
-               if (!readit) {
+               if (!found) {
                        lex.printError("Unknown language `" + code + "'");
                        break;
                }
+               if (!readTranslations(lex, trans[code])) {
+                       lex.printError("Could not read layout translations for language `"
+                               + code + "'");
+                       break;
+               }
        }
 
        // 2) merge all translations into the languages
@@ -338,7 +356,7 @@ void Languages::readLayoutTranslations(support::FileName const & filename)
        TransMap::const_iterator const tend = trans.end();
        for (TransMap::const_iterator tit = tbeg; tit != tend; ++tit) {
                for (LanguageList::iterator lit = lbeg; lit != lend; ++lit) {
-                       Match m = match(tit->first, lit->second);
+                       Match const m = match(tit->first, lit->second);
                        if (m == NoMatch)
                                continue;
                        lit->second.readLayoutTranslations(tit->second,