diff --git a/qt/Makefile.am b/qt/Makefile.am index 82b6f7d..1175466 100644 --- a/qt/Makefile.am +++ b/qt/Makefile.am @@ -1,71 +1,71 @@ # Makefile.am # Copyright (C) 2002 g10 Code GmbH, Klarälvdalens Datakonsult AB # Copyright (C) 2008, 2015 g10 Code GmbH # # This file is part of PINENTRY. # # PINENTRY 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. # # PINENTRY 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 . # SPDX-License-Identifier: GPL-2.0+ ## Process this file with automake to produce Makefile.in bin_PROGRAMS = pinentry-qt EXTRA_DIST = document-encrypt.png pinentry.qrc if FALLBACK_CURSES ncurses_include = $(NCURSES_INCLUDE) libcurses = ../pinentry/libpinentry-curses.a $(LIBCURSES) $(LIBICONV) else ncurses_include = libcurses = endif AM_CPPFLAGS = $(COMMON_CFLAGS) \ -I$(top_srcdir) -I$(top_srcdir)/secmem \ $(ncurses_include) -I$(top_srcdir)/pinentry AM_CXXFLAGS = $(PINENTRY_QT_CFLAGS) pinentry_qt_LDADD = \ ../pinentry/libpinentry.a $(top_builddir)/secmem/libsecmem.a \ $(COMMON_LIBS) $(PINENTRY_QT_LIBS) $(libcurses) $(LIBCAP) pinentry_qt_LDFLAGS = $(PINENTRY_QT_LDFLAGS) if BUILD_PINENTRY_QT BUILT_SOURCES = \ pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc capslock.moc endif CLEANFILES = \ pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc capslock.moc if HAVE_W32_SYSTEM pinentry_qt_platform_SOURCES = capslock_win.cpp else pinentry_qt_platform_SOURCES = capslock_unix.cpp endif pinentry_qt_SOURCES = pinentrydialog.h pinentrydialog.cpp \ main.cpp qrc_pinentry.cpp pinentryconfirm.cpp pinentryconfirm.h \ pinlineedit.h pinlineedit.cpp capslock.cpp capslock.h capslock_p.h \ pinentry_debug.cpp pinentry_debug.h util.h accessibility.cpp \ - accessibility.h \ + accessibility.h qti18n.cpp \ $(pinentry_qt_platform_SOURCES) nodist_pinentry_qt_SOURCES = \ pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc capslock.moc .h.moc: $(MOC) `test -f '$<' || echo '$(srcdir)/'`$< -o $@ diff --git a/qt/qti18n.cpp b/qt/qti18n.cpp new file mode 100644 index 0000000..8e6bb59 --- /dev/null +++ b/qt/qti18n.cpp @@ -0,0 +1,93 @@ +/* qti18n.cpp - Load qt translations for pinentry. + * Copyright 2021 g10 Code GmbH + * SPDX-FileCopyrightText: 2015 Lukáš Tinkl + * SPDX-FileCopyrightText: 2021 Ingo Klöcker + * + * 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 . + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include +#include +#include +#include + +#include + +static bool loadCatalog(const QString &catalog, const QLocale &locale) +{ + auto translator = new QTranslator(QCoreApplication::instance()); + + if (!translator->load(locale, catalog, QString(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { + 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)