From c02531fc55b9bc998de8bf825507bf0d3e53b74e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 22 Apr 2018 22:03:31 +0300 Subject: [PATCH] Feat: Get translations by language Rename 'ru_RU' translations to just 'ru'. Force language 'ru' for test_translations.py. --- m_librarian/translations.py | 15 ++++++++++++--- m_librarian/translations/{ru_RU.mo => ru.mo} | Bin m_librarian/translations/{ru_RU.po => ru.po} | 0 tests/test_translations.py | 10 ++-------- 4 files changed, 14 insertions(+), 11 deletions(-) rename m_librarian/translations/{ru_RU.mo => ru.mo} (100%) rename m_librarian/translations/{ru_RU.po => ru.po} (100%) diff --git a/m_librarian/translations.py b/m_librarian/translations.py index ba5f44c..e3cbce4 100644 --- a/m_librarian/translations.py +++ b/m_librarian/translations.py @@ -3,16 +3,25 @@ import gettext import locale import os -language = locale.getdefaultlocale()[0] -translations = None -if language: +def get_translations(language): mo_filename = os.path.join( os.path.dirname(__file__), 'translations', language + '.mo') if os.path.exists(mo_filename): mo_file = open(mo_filename, 'rb') translations = gettext.GNUTranslations(mo_file) mo_file.close() + return translations + return None + + +language = locale.getdefaultlocale()[0] +translations = None + +if language: + if language in ('ru_RU', 'Russian_Russia'): + language = 'ru' + translations = get_translations(language) if translations is None: translations = gettext.NullTranslations() diff --git a/m_librarian/translations/ru_RU.mo b/m_librarian/translations/ru.mo similarity index 100% rename from m_librarian/translations/ru_RU.mo rename to m_librarian/translations/ru.mo diff --git a/m_librarian/translations/ru_RU.po b/m_librarian/translations/ru.po similarity index 100% rename from m_librarian/translations/ru_RU.po rename to m_librarian/translations/ru.po diff --git a/tests/test_translations.py b/tests/test_translations.py index 313d6d6..ed45122 100644 --- a/tests/test_translations.py +++ b/tests/test_translations.py @@ -1,14 +1,8 @@ # coding: utf-8 -import os -save_locale = os.environ.get('LC_CTYPE') -os.environ['LC_CTYPE'] = 'ru_RU' - -from m_librarian.translations import translations # noqa - -if save_locale: - os.environ['LC_CTYPE'] = save_locale +from m_librarian.translations import get_translations +translations = get_translations('ru') _ = getattr(translations, 'ugettext', None) or translations.gettext -- 2.39.5