1 """i18n and l10n support for git-cola"""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
9 from . import resources
12 class NullTranslation(object):
13 """This is a pass-through object that does nothing"""
15 def gettext(self
, value
):
20 """The application-wide current translation state"""
22 translation
= NullTranslation()
26 cls
.translation
= NullTranslation()
29 def update(cls
, lang
):
30 cls
.translation
= Translation(lang
)
33 def gettext(cls
, value
):
34 """Return a translated value"""
35 return cls
.translation
.gettext(value
)
38 class Translation(object):
39 def __init__(self
, lang
):
42 self
.filename
= get_filename_for_locale(lang
)
47 """Read the pofile content into memory"""
48 po
= polib
.pofile(self
.filename
, encoding
='utf-8')
49 messages
= self
.messages
50 for entry
in po
.translated_entries():
51 messages
[entry
.msgid
] = entry
.msgstr
53 def gettext(self
, value
):
54 return self
.messages
.get(value
, value
)
58 """Translate a string"""
59 txt
= State
.gettext(value
)
60 # handle @@verb / @@noun
61 if txt
[-6:-4] == '@@':
62 txt
= txt
.replace('@@verb', '').replace('@@noun', '')
67 """Marker function for translated values
69 N_("Some string value") is used to mark strings for translation.
74 def get_filename_for_locale(name
):
75 """Return the .po file for the specified locale"""
76 # When <name> is "foo_BAR.UTF-8", the name is truncated to "foo_BAR".
77 # When <name> is "foo_BAR", the <short_name> is "foo"
78 # Try the following locations:
80 # cola/i18n/<short_name>.po
81 if not name
: # If no locale was specified then try the current locale.
82 name
= locale
.getdefaultlocale()[0]
87 name
= name
.split('.', 1)[0] # foo_BAR.UTF-8 -> foo_BAR
89 filename
= resources
.i18n('%s.po' % name
)
90 if os
.path
.exists(filename
):
93 short_name
= name
.split('_', 1)[0]
94 filename
= resources
.i18n('%s.po' % short_name
)
95 if os
.path
.exists(filename
):
101 # pylint: disable=global-statement
102 if sys
.platform
== 'win32' and not lang
:
103 lang
= _get_win32_default_locale()
104 lang
= _install_custom_language(lang
)
109 # pylint: disable=global-statement
113 def _install_custom_language(lang
):
114 """Allow a custom language to be set in ~/.config/git-cola/language"""
115 lang_file
= resources
.config_home('language')
116 if not core
.exists(lang_file
):
119 lang
= core
.read(lang_file
).strip()
120 except (OSError, IOError):
125 def _get_win32_default_locale():
126 """Get the default locale on Windows"""
127 for name
in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
128 lang
= os
.environ
.get(name
)
132 import ctypes
# pylint: disable=all
134 # use only user's default locale
135 return locale
.getdefaultlocale()[0]
136 # using ctypes to determine all locales
137 lcid_user
= ctypes
.windll
.kernel32
.GetUserDefaultLCID()
138 lcid_system
= ctypes
.windll
.kernel32
.GetSystemDefaultLCID()
139 lang_user
= locale
.windows_locale
.get(lcid_user
)
140 lang_system
= locale
.windows_locale
.get(lcid_system
)