1 """i18n and l10n support for git-cola"""
8 from . import resources
11 class NullTranslation
:
12 """This is a pass-through object that does nothing"""
14 def gettext(self
, value
):
19 """The application-wide current translation state"""
21 translation
= NullTranslation()
25 cls
.translation
= NullTranslation()
28 def update(cls
, lang
):
29 cls
.translation
= Translation(lang
)
32 def gettext(cls
, value
):
33 """Return a translated value"""
34 return cls
.translation
.gettext(value
)
38 def __init__(self
, lang
):
41 self
.filename
= get_filename_for_locale(lang
)
46 """Read the pofile content into memory"""
47 po
= polib
.pofile(self
.filename
, encoding
='utf-8')
48 messages
= self
.messages
49 for entry
in po
.translated_entries():
50 messages
[entry
.msgid
] = entry
.msgstr
52 def gettext(self
, value
):
53 return self
.messages
.get(value
, value
)
57 """Translate a string"""
58 txt
= State
.gettext(value
)
59 # handle @@verb / @@noun
60 if txt
[-6:-4] == '@@':
61 txt
= txt
.replace('@@verb', '').replace('@@noun', '')
66 """Marker function for translated values
68 N_("Some string value") is used to mark strings for translation.
73 def get_filename_for_locale(name
):
74 """Return the .po file for the specified locale"""
75 # When <name> is "foo_BAR.UTF-8", the name is truncated to "foo_BAR".
76 # When <name> is "foo_BAR", the <short_name> is "foo"
77 # Try the following locations:
79 # cola/i18n/<short_name>.po
80 if not name
: # If no locale was specified then try the current locale.
81 name
= locale
.getdefaultlocale()[0]
86 name
= name
.split('.', 1)[0] # foo_BAR.UTF-8 -> foo_BAR
88 filename
= resources
.i18n('%s.po' % name
)
89 if os
.path
.exists(filename
):
92 short_name
= name
.split('_', 1)[0]
93 filename
= resources
.i18n('%s.po' % short_name
)
94 if os
.path
.exists(filename
):
100 # pylint: disable=global-statement
101 if sys
.platform
== 'win32' and not lang
:
102 lang
= _get_win32_default_locale()
103 lang
= _install_custom_language(lang
)
108 # pylint: disable=global-statement
112 def _install_custom_language(lang
):
113 """Allow a custom language to be set in ~/.config/git-cola/language"""
114 lang_file
= resources
.config_home('language')
115 if not core
.exists(lang_file
):
118 lang
= core
.read(lang_file
).strip()
124 def _get_win32_default_locale():
125 """Get the default locale on Windows"""
126 for name
in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
127 lang
= os
.environ
.get(name
)
131 import ctypes
# pylint: disable=all
133 # use only user's default locale
134 return locale
.getdefaultlocale()[0]
135 # using ctypes to determine all locales
136 lcid_user
= ctypes
.windll
.kernel32
.GetUserDefaultLCID()
137 lcid_system
= ctypes
.windll
.kernel32
.GetSystemDefaultLCID()
138 lang_user
= locale
.windows_locale
.get(lcid_user
)
139 lang_system
= locale
.windows_locale
.get(lcid_system
)