summaryrefslogtreecommitdiff
path: root/src/main-view/LanguageMenu.cpp
blob: 84ffd469a7b185332a12184aca00ba0296447270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "LanguageMenu.h"
#include "../main.h"
#include "../strings.h"

LanguageMenu::LanguageMenu(QMenu* parent):
    QMenu(tr("&Language"), parent),
    locale(QSettings().value("lang").toString()),
    systemLocale(QLocale::system().name().split("_").first())
{
    initUi(parent);
    initLangs();
    createActionsGroup();
    foreach(QString key, langs.keys())
        createAction(key);
}

void LanguageMenu::initUi(QMenu* parent)
{
    parent->addMenu(this);
    setIcon(QIcon(":/images/language.png"));
}

void LanguageMenu::initLangs()
{
    langs["cs"] = "Čeština (Czech)";
    langs["fi"] = "Suomi (Finnish)";
    langs["fr"] = "Français (French)";
    langs["de"] = "Deutsch (German)";
    langs["en"] = "English";
    langs["ru"] = "Русский (Russian)";
    langs["es"] = "Español (Spanish)";
    langs["uk"] = "Українська (Ukrainian)";
    langs[""] = tr("System") + ": " + langs[systemLocale];
}

void LanguageMenu::createActionsGroup()
{
    actionsGroup = new QActionGroup(this);
    actionsGroup->setExclusive(true);
}

void LanguageMenu::createAction(const QString& key)
{
    QAction* action = addAction(langs[key]);
    action->setCheckable(true);
    if(key == locale)
        action->setChecked(true);
    action->setData(key);
    actionsGroup->addAction(action);
    connect(action, SIGNAL(triggered()), SLOT(saveLanguage()));
}

void LanguageMenu::saveLanguage()
{
    QAction *action = qobject_cast<QAction*>(sender());
    QString newLocale = action->data().toString();
    QSettings().setValue("lang", newLocale);
    if(newLocale.isEmpty())
        newLocale = systemLocale;

    installTranslator("freshmemory_" + newLocale, getResourcePath() + "/tr");
    QMessageBox::information(this, Strings::tr(Strings::s_appTitle),
        tr("The application must be restarted to use the selected language"));
}