aboutsummaryrefslogtreecommitdiffstats
path: root/src/m_pinentry/qti18n.cpp
blob: 198e6cc43967098b00f9ad2fe0250399f64fb3bd (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* qti18n.cpp - Load qt translations for pinentry.
 * Copyright 2021 g10 Code GmbH
 * SPDX-FileCopyrightText: 2015 Lukáš Tinkl <[email protected]>
 * SPDX-FileCopyrightText: 2021 Ingo Klöcker <[email protected]>
 *
 * Copied from k18n under the terms of LGPLv2 or later.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <QCoreApplication>
#include <QDebug>
#include <QLibraryInfo>
#include <QLocale>
#include <QTranslator>

static bool loadCatalog(const QString &catalog, const QLocale &locale) {
  auto translator = new QTranslator(QCoreApplication::instance());

  if (!translator->load(locale, catalog, QString(),
                        QLatin1String(":/i18n_qt"))) {
    qDebug() << "Loading the" << catalog << "catalog failed for locale"
             << locale;
    delete translator;
    return false;
  }
  QCoreApplication::instance()->installTranslator(translator);
  return true;
}

static bool loadCatalog(const QString &catalog, const QLocale &locale,
                        const QLocale &fallbackLocale) {
  // try to load the catalog for locale
  if (loadCatalog(catalog, locale)) {
    return true;
  }
  // if this fails, then try the fallback locale (if it's different from locale)
  if (fallbackLocale != locale) {
    return loadCatalog(catalog, fallbackLocale);
  }
  return false;
}

// load global Qt translation, needed in KDE e.g. by lots of builtin dialogs
// (QColorDialog, QFontDialog) that we use
static void loadTranslation(const QString &localeName,
                            const QString &fallbackLocaleName) {
  const QLocale locale{localeName};
  const QLocale fallbackLocale{fallbackLocaleName};
  // first, try to load the qt_ meta catalog
  if (loadCatalog(QStringLiteral("qt_"), locale, fallbackLocale)) {
    return;
  }
  // if loading the meta catalog failed, then try loading the four catalogs
  // it depends on, i.e. qtbase, qtscript, qtmultimedia, qtxmlpatterns,
  // separately
  const auto catalogs = {
      QStringLiteral("qtbase_"),
      /* QStringLiteral("qtscript_"),
      QStringLiteral("qtmultimedia_"),
      QStringLiteral("qtxmlpatterns_"), */
  };
  for (const auto &catalog : catalogs) {
    loadCatalog(catalog, locale, fallbackLocale);
  }
}

static void load() {
  // The way Qt translation system handles plural forms makes it necessary to
  // have a translation file which contains only plural forms for `en`. That's
  // why we load the `en` translation unconditionally, then load the
  // translation for the current locale to overload it.
  loadCatalog(QStringLiteral("qt_"), QLocale{QStringLiteral("en")});

  const QLocale locale = QLocale::system();
  if (locale.name() != QStringLiteral("en")) {
    loadTranslation(locale.name(), locale.bcp47Name());
  }
}

Q_COREAPP_STARTUP_FUNCTION(load)