KI18n

main.cpp
1 /* This file is part of the KDE libraries
2  SPDX-FileCopyrightText: 2015 Lukáš Tinkl <[email protected]>
3  SPDX-FileCopyrightText: 2021 Ingo Klöcker <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "ki18n_logging.h"
9 
10 #include <QCoreApplication>
11 #include <QLibraryInfo>
12 #include <QLocale>
13 #include <QTranslator>
14 
15 #include <memory>
16 
17 static bool loadCatalog(const QString &catalog, const QLocale &locale)
18 {
19  auto translator = std::make_unique<QTranslator>(QCoreApplication::instance());
20  if (!translator->load(locale, catalog, QString(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
21  qCDebug(KI18N) << "Loading the" << catalog << "catalog failed for locale" << locale;
22  return false;
23  }
24  QCoreApplication::instance()->installTranslator(translator.release());
25  return true;
26 }
27 
28 static bool loadCatalog(const QString &catalog, const QLocale &locale, const QLocale &fallbackLocale)
29 {
30  // try to load the catalog for locale
31  if (loadCatalog(catalog, locale)) {
32  return true;
33  }
34  // if this fails, then try the fallback locale (if it's different from locale)
35  if (fallbackLocale != locale) {
36  return loadCatalog(catalog, fallbackLocale);
37  }
38  return false;
39 }
40 
41 // load global Qt translation, needed in KDE e.g. by lots of builtin dialogs (QColorDialog, QFontDialog) that we use
42 static void loadTranslation(const QString &localeName, const QString &fallbackLocaleName)
43 {
44  const QLocale locale{localeName};
45  const QLocale fallbackLocale{fallbackLocaleName};
46  // first, try to load the qt_ meta catalog
47  if (loadCatalog(QStringLiteral("qt_"), locale, fallbackLocale)) {
48  return;
49  }
50  // if loading the meta catalog failed, then try loading the four catalogs
51  // it depends on, i.e. qtbase, qtscript, qtmultimedia, qtxmlpatterns, separately
52  const auto catalogs = {
53  QStringLiteral("qtbase_"),
54  QStringLiteral("qtscript_"),
55  QStringLiteral("qtmultimedia_"),
56  QStringLiteral("qtxmlpatterns_"),
57  };
58  for (const auto &catalog : catalogs) {
59  loadCatalog(catalog, locale, fallbackLocale);
60  }
61 }
62 
63 static void load()
64 {
65  // The way Qt translation system handles plural forms makes it necessary to
66  // have a translation file which contains only plural forms for `en`. That's
67  // why we load the `en` translation unconditionally, then load the
68  // translation for the current locale to overload it.
69  loadCatalog(QStringLiteral("qt_"), QLocale{QStringLiteral("en")});
70 
71  const QLocale locale = QLocale::system();
72  if (locale.name() != QStringLiteral("en")) {
73  loadTranslation(locale.name(), locale.bcp47Name());
74  }
75 }
76 
77 Q_COREAPP_STARTUP_FUNCTION(load)
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
QLocale system()
bool installTranslator(QTranslator *translationFile)
QCoreApplication * instance()
LocaleWrapper locale()
QString location(QLibraryInfo::LibraryLocation loc)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 04:07:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.