Translate language names.
[yandex-traffic] / settings.cpp
index a08f07f..fd78526 100644 (file)
@@ -1,8 +1,34 @@
 #include <QtCore>
 #include <settings.hpp>
 
+#include "globals.hpp"
+
+
+// --------------------------------------------------
+// Language
+// --------------------------------------------------
+QString Language::title () const
+{
+    return Settings::tr (_title.toUtf8 ());
+}
+
+
+// --------------------------------------------------
+// Settings
+// --------------------------------------------------
+static Settings* _settings;
+
+
+Settings* Settings::instance ()
+{
+    if (!_settings)
+        _settings = new Settings;
+    return _settings;
+}
+
 
 Settings::Settings ()
+    : _ts (NULL)
 {
     load ();
 }
@@ -26,9 +52,7 @@ void Settings::load ()
 
     _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ());
 
-    _langIndex = settings.value ("langIndex", _langIndex).toInt ();
-    if (_langIndex < 0 || _langIndex >= _langs.count ())
-        _langIndex = 0;
+    setLanguageIndex (settings.value ("langIndex", _langIndex).toInt ());
 }
 
 
@@ -52,6 +76,7 @@ void Settings::save ()
 }
 
 
+// This routine called before any translations loaded, so strings must be translated explicitly.
 void Settings::makeDefault ()
 {
     _regionID = "1";            // Default city
@@ -72,10 +97,10 @@ void Settings::makeDefault ()
     _updateIntervalIndex = 3;
 
     // languages
-    _langIndex = 0;
     _langs.append (Language (QString (""),   tr ("System")));
     _langs.append (Language (QString ("en"), tr ("English")));
     _langs.append (Language (QString ("ru"), tr ("Russian")));
+    setLanguageIndex (0);
 }
 
 
@@ -124,3 +149,43 @@ int Settings::minutes2IntervalIndex (int minutes) const
             return 0;
     }
 }
+
+
+void Settings::setLanguageIndex (int index)
+{
+    if (index < 0 || index >= _langs.count ())
+        _langIndex = 0;
+    else
+        _langIndex = index;
+
+    // load settings
+    if (_ts) {
+        QCoreApplication::removeTranslator (_ts);
+        _ts = NULL;
+    }
+
+    QString alias = _langs[_langIndex].alias ();
+    QString fileName = QString (APPLICATION_NAME) + "_";
+
+    _ts = new QTranslator;
+
+    if (alias.isEmpty ())
+        fileName += QLocale::system ().name ();
+    else
+        fileName += alias;
+
+    if (_ts->load (fileName, TRANSLATION_PATH)) {
+        QCoreApplication::installTranslator (_ts);
+        translationsUpdated ();
+    }
+    else {
+        delete _ts;
+        _ts = NULL;
+    }
+}
+
+
+QString Settings::regionName (const QString &id) const
+{
+    return Settings::tr (_cities[id].toUtf8 ());
+}