]> git.phdru.name Git - m_librarian.git/blob - m_librarian/translations.py
Feat: Get translations by language
[m_librarian.git] / m_librarian / translations.py
1
2 import gettext
3 import locale
4 import os
5
6
7 def get_translations(language):
8     mo_filename = os.path.join(
9         os.path.dirname(__file__), 'translations', language + '.mo')
10     if os.path.exists(mo_filename):
11         mo_file = open(mo_filename, 'rb')
12         translations = gettext.GNUTranslations(mo_file)
13         mo_file.close()
14         return translations
15     return None
16
17
18 language = locale.getdefaultlocale()[0]
19 translations = None
20
21 if language:
22     if language in ('ru_RU', 'Russian_Russia'):
23         language = 'ru'
24     translations = get_translations(language)
25
26 if translations is None:
27     translations = gettext.NullTranslations()