Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable gettext translation for glade dialogs. #75

Merged
merged 4 commits into from
Mar 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions sonata/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
sys.exit(1)

import gettext
import locale
import logging
import os
import platform
Expand Down Expand Up @@ -118,9 +119,36 @@ def run():
# files.
locales_path = None

gettext.install('sonata', locales_path, names=["ngettext"])
gettext.textdomain('sonata')
gettext.bindtextdomain('sonata', locales_path)
# Gtk.Builder uses gettext functions from C library. Enable
# correct localization for these functions with the locale
# module. See:
# https://docs.python.org/3/library/locale.html#access-to-message-catalogs
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 as e:
# 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
print("setlocale() failed: %s, falling back to default locale." % e)
locale.setlocale(locale.LC_ALL, 'C')
gettext.install(True, localedir=None, names=["ngettext"])


## Check initial dependencies:
Expand Down