]> git.lyx.org Git - lyx.git/blobdiff - src/Language.cpp
Refactoring based on cppcheck suggestions
[lyx.git] / src / Language.cpp
index 52c1c4fcfc2324e93a74c2af15bb0577cc2aee85..4f625b55b9e18bb97e8b6d971669809a7dd388fb 100644 (file)
 #include "support/filetools.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
+#include "support/qstring_helpers.h"
 #include "support/Messages.h"
 
+#include <QLocale>
+#include <QString>
+
 using namespace std;
 using namespace lyx::support;
 
@@ -41,13 +45,13 @@ Language const * reset_language = 0;
 
 bool Language::isPolyglossiaExclusive() const
 {
-       return babel().empty() && !polyglossia().empty() && requires().empty();
+       return babel().empty() && !polyglossia().empty() && required().empty();
 }
 
 
 bool Language::isBabelExclusive() const
 {
-       return !babel().empty() && polyglossia().empty() && requires().empty();
+       return !babel().empty() && polyglossia().empty() && required().empty();
 }
 
 
@@ -111,6 +115,16 @@ string Language::dateFormat(size_t i) const
 }
 
 
+docstring Language::decimalSeparator() const
+{
+       if (lyxrc.default_decimal_sep == "locale") {
+               QLocale loc = QLocale(toqstr(code()));
+               return qstring_to_ucs4(QString(loc.decimalPoint()));
+       }
+       return from_utf8(lyxrc.default_decimal_sep);
+}
+
+
 bool Language::readLanguage(Lexer & lex)
 {
        enum LanguageTags {
@@ -126,17 +140,20 @@ bool Language::readLanguage(Lexer & lex)
                LA_LANG_VARIETY,
                LA_POLYGLOSSIANAME,
                LA_POLYGLOSSIAOPTS,
+               LA_XINDYNAME,
                LA_POSTBABELPREAMBLE,
                LA_PREBABELPREAMBLE,
                LA_PROVIDES,
                LA_REQUIRES,
                LA_QUOTESTYLE,
                LA_RTL,
-               LA_WORDWRAP
+               LA_WORDWRAP,
+               LA_ACTIVECHARS
        };
 
        // Keep these sorted alphabetically!
        LexerKeyword languageTags[] = {
+               { "activechars",          LA_ACTIVECHARS },
                { "babelname",            LA_BABELNAME },
                { "dateformats",          LA_DATEFORMATS },
                { "encoding",             LA_ENCODING },
@@ -155,7 +172,8 @@ bool Language::readLanguage(Lexer & lex)
                { "quotestyle",           LA_QUOTESTYLE },
                { "requires",             LA_REQUIRES },
                { "rtl",                  LA_RTL },
-               {"wordwrap",              LA_WORDWRAP }
+               { "wordwrap",             LA_WORDWRAP },
+               { "xindyname",            LA_XINDYNAME }
        };
 
        bool error = false;
@@ -190,9 +208,15 @@ bool Language::readLanguage(Lexer & lex)
                case LA_POLYGLOSSIAOPTS:
                        lex >> polyglossia_opts_;
                        break;
+               case LA_XINDYNAME:
+                       lex >> xindy_;
+                       break;
                case LA_QUOTESTYLE:
                        lex >> quote_style_;
                        break;
+               case LA_ACTIVECHARS:
+                       lex >> active_chars_;
+                       break;
                case LA_ENCODING:
                        lex >> encodingStr_;
                        break;
@@ -234,7 +258,7 @@ bool Language::readLanguage(Lexer & lex)
                                lex.getLongString(from_ascii("EndPreBabelPreamble"));
                        break;
                case LA_REQUIRES:
-                       lex >> requires_;
+                       lex >> required_;
                        break;
                case LA_PROVIDES:
                        lex >> provides_;
@@ -254,9 +278,9 @@ bool Language::readLanguage(Lexer & lex)
 
 bool Language::read(Lexer & lex)
 {
-       encoding_ = 0;
-       internal_enc_ = 0;
-       rightToLeft_ = 0;
+       encoding_ = nullptr;
+       internal_enc_ = false;
+       rightToLeft_ = false;
 
        if (!lex.next()) {
                lex.printError("No name given for language: `$$Token'.");
@@ -367,6 +391,7 @@ bool readTranslations(Lexer & lex, Language::TranslationMap & trans)
 enum Match {
        NoMatch,
        ApproximateMatch,
+       VeryApproximateMatch,
        ExactMatch
 };
 
@@ -389,6 +414,8 @@ Match match(string const & code, Language const & lang)
        if ((code.size() == 2) && (langcode.size() > 2)
                && (code + '_' == langcode.substr(0, 3)))
                return ApproximateMatch;
+       if (code.substr(0,2) == langcode.substr(0,2))
+               return VeryApproximateMatch;
        return NoMatch;
 }
 
@@ -398,17 +425,41 @@ Match match(string const & code, Language const & lang)
 
 Language const * Languages::getFromCode(string const & code) const
 {
-       // Try for exact match first
+       // 1/ exact match with any known language
        for (auto const & l : languagelist_) {
                if (match(code, l.second) == ExactMatch)
                        return &l.second;
        }
-       // If not found, look for lang prefix (without country) instead
+
+       // 2/ approximate with any known language
        for (auto const & l : languagelist_) {
                if (match(code, l.second) == ApproximateMatch)
                        return &l.second;
        }
-       LYXERR0("Unknown language `" + code + "'");
+       return 0;
+}
+
+
+Language const * Languages::getFromCode(string const & code,
+                       set<Language const *> const & tryfirst) const
+{
+       // 1/ exact match with tryfirst list
+       for (auto const * lptr : tryfirst) {
+               if (match(code, *lptr) == ExactMatch)
+                       return lptr;
+       }
+
+       // 2/ approximate match with tryfirst list
+       for (auto const * lptr : tryfirst) {
+               Match const m = match(code, *lptr);
+               if (m == ApproximateMatch || m == VeryApproximateMatch)
+                       return lptr;
+       }
+
+       // 3/ stricter match in all languages
+       return getFromCode(code);
+
+       LYXERR0("Unknown language `" << code << "'");
        return 0;
 }