Skip to content

Commit

Permalink
Uniform handling of locale initialization.
Browse files Browse the repository at this point in the history
Provide uniform translation for both GObject / Glade strings
(initialized by module locale) and Python gettext strings to
prevent a mix of languages in UI strings.

If a locale is provided with sonata but not generated for the C
library, Python strings will be translated, but strings in Glade
dialogs will not be translated. So if the initial locale.setlocale
call fails, don't point Python gettext to the .mo files -- this way
all strings will be left untranslated.
  • Loading branch information
Peter Helbing committed Mar 3, 2015
1 parent 7994c82 commit 6fdcce4
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions sonata/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,31 @@ def run():
# correct localization for these functions with the locale
# module. See:
# https://docs.python.org/3/library/locale.html#access-to-message-catalogs
locale.setlocale(locale.LC_ALL, '')

# bindtextdomain() is GNU libc specific and may not be available
# on other systems (e.g. OSX)
if hasattr(locale, 'bindtextdomain'):
locale.bindtextdomain('sonata', locales_path)

gettext.install('sonata', locales_path, names=["ngettext"])
gettext.textdomain('sonata')
gettext.bindtextdomain('sonata', locales_path)
try:
locale.setlocale(locale.LC_ALL, '')
gettext.install('sonata', locales_path, names=["ngettext"])

# bindtextdomain() is GNU libc specific and may not be available
# on other systems (e.g. OSX)
if hasattr(locale, 'bindtextdomain'):
locale.bindtextdomain('sonata', locales_path)

gettext.textdomain('sonata')
gettext.bindtextdomain('sonata', locales_path)
except locale.Error:
# If locale is not supported by C library, the initial call to
# locale.setlocale will fail and raise an exception. Any Glade
# strings would not be translated. But native python strings
# would still be translated if the .mo files are included in
# sonata!
#
# To prevent a mix of languages, disable translation for both:
# 1. explicitly set locale to 'C' (default strings)
# 2. don't provide python gettext with any translatable
# strings (localedir=None), but still install the required
# _() function
locale.setlocale(locale.LC_ALL, 'C')
gettext.install(True, localedir=None, names=["ngettext"])


## Check initial dependencies:
Expand Down

0 comments on commit 6fdcce4

Please sign in to comment.