qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
translator.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "translator.h"
22 #include <QApplication>
23 #include <QDebug>
24 #include <QLibraryInfo>
25 #include <QLocale>
26 #include <QMutexLocker>
27 #include <QString>
28 #include <QTranslator>
29 #include <algorithm>
30 
31 QTranslator* Translator::core_translator{nullptr};
32 QTranslator* Translator::app_translator{nullptr};
33 QVector<Translator::Callback> Translator::callbacks;
34 QMutex Translator::lock;
35 
39 void Translator::translate(const QString& localeName)
40 {
41  QMutexLocker locker{&lock};
42 
43  if (!core_translator)
44  core_translator = new QTranslator();
45 
46  if (!app_translator)
47  app_translator = new QTranslator();
48 
49  // Remove old translations
50  QCoreApplication::removeTranslator(core_translator);
51  QApplication::removeTranslator(app_translator);
52 
53  // Load translations
54  QString locale = localeName.isEmpty() ? QLocale::system().name().section('_', 0, 0) : localeName;
55 
56  if (core_translator->load(locale, ":translations/")) {
57  qDebug() << "Loaded translation" << locale;
58 
59  // System menu translation
60  QString s_locale = "qt_" + locale;
61  QString location = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
62  if (app_translator->load(s_locale, location)) {
63  QApplication::installTranslator(app_translator);
64  qDebug() << "System translation loaded" << locale;
65  } else {
66  qDebug() << "System translation not loaded" << locale;
67  }
68 
69  // Application translation
70  QCoreApplication::installTranslator(core_translator);
71  } else {
72  qDebug() << "Error loading translation" << locale;
73  }
74 
75  // After the language is changed from RTL to LTR, the layout direction isn't
76  // always restored
77  const QString direction =
78  QApplication::tr("LTR", "Translate this string to the string 'RTL' in"
79  " right-to-left languages (for example Hebrew and"
80  " Arabic) to get proper widget layout");
81 
82  QGuiApplication::setLayoutDirection(direction == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
83 
84  for (auto pair : callbacks)
85  pair.second();
86 }
87 
93 void Translator::registerHandler(const std::function<void()>& f, void* owner)
94 {
95  QMutexLocker locker{&lock};
96  callbacks.push_back({owner, f});
97 }
98 
103 void Translator::unregister(void* owner)
104 {
105  QMutexLocker locker{&lock};
106  callbacks.erase(std::remove_if(begin(callbacks), end(callbacks),
107  [=](const Callback& c) { return c.first == owner; }),
108  end(callbacks));
109 }
Translator::core_translator
static QTranslator * core_translator
Definition: translator.h:41
Translator::translate
static void translate(const QString &localeName)
Loads the translations according to the settings or locale.
Definition: translator.cpp:39
Translator::unregister
static void unregister(void *owner)
Unregisters all handlers of an owner.
Definition: translator.cpp:103
Translator::Callback
QPair< void *, std::function< void()> > Callback
Definition: translator.h:38
Translator::lock
static QMutex lock
Definition: translator.h:40
Translator::registerHandler
static void registerHandler(const std::function< void()> &, void *owner)
Register a function to be called when the UI needs to be retranslated.
Definition: translator.cpp:93
Translator::app_translator
static QTranslator * app_translator
Definition: translator.h:42
translator.h
Translator::callbacks
static QVector< Callback > callbacks
Definition: translator.h:39