Skip to content

Commit

Permalink
fix translation fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelmberg committed Jul 22, 2019
1 parent b72455d commit 5cdb8c3
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions CKAN-Browser/ckan_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,46 @@ def __init__(self, iface):
'CKANBrowser_{}.qm'.format(locale))

QgsMessageLog.logMessage(u'locale_path: {}'.format(locale_path), 'CKAN-Browser', Qgis.Info)


locale_path_en = os.path.join(
self.plugin_dir,
'i18n',
'CKANBrowser_en.qm'
)

# if we don't have translation for current locale, completely switch to English
if not os.path.exists(locale_path):
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'CKANBrowser_en.qm'
)

locale = 'en'
locale_path = locale_path_en

# if locale is not 'en' then additionally load 'en' as fallback for untranslated elements.
# !!! this has to be done before(!) adding the actual locale:
# https://doc.qt.io/qt-5/qcoreapplication.html#installTranslator
# "Translations are searched for in the reverse order in which they were installed, so the most recently
# installed translation file is searched first and the first translation file installed is searched last."
if locale != 'en':
QgsMessageLog.logMessage(u'loading "en" fallback: {}'.format(locale_path_en), 'CKAN-Browser', Qgis.Info)
self.translator_en = QTranslator()
self.translator_en.load(locale_path_en)
if not QCoreApplication.installTranslator(self.translator_en):
QgsMessageLog.logMessage(u'could not install translator: {}'.format(locale_path_en), 'CKAN-Browser', Qgis.Critical)
else:
QgsMessageLog.logMessage(u'locale "en" installed', 'CKAN-Browser', Qgis.Info)

if os.path.exists(locale_path):
self.translator = QTranslator()

# load translations according to locale
self.translator.load(locale_path)

if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
if not QCoreApplication.installTranslator(self.translator):
QgsMessageLog.logMessage(u'could not install translator: {}'.format(locale_path), 'CKAN-Browser', Qgis.Critical)
else:
QgsMessageLog.logMessage(u'locale "{}" installed'.format(locale), 'CKAN-Browser', Qgis.Info)

self.settings = Settings()
self.settings.load()
self.util = Util(self.settings, self.iface.mainWindow())
self.util.msg_log_debug('__init__')

# TODO ping API

Expand Down

0 comments on commit 5cdb8c3

Please sign in to comment.