1# Orca
2#
3# Copyright 2004-2008 Sun Microsystems Inc.
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# This library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this library; if not, write to the
17# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
18# Boston MA  02110-1301 USA.
19
20"""Provides i18n support for orca using the gettext module.  Tells
21gettext where to find localized strings and creates an alias, _, that
22maps to the gettext.gettext function.  This function will accept a
23string and return a localized string for that string.
24"""
25
26import gettext
27import imp
28import os
29import sys
30
31# Alias gettext.gettext to _ and gettext.ngettext to ngettext
32#
33_ = gettext.gettext
34ngettext = gettext.ngettext
35cgettext = gettext.gettext
36
37# Tell gettext where to find localized strings.
38#
39localedir = "@localedir@".replace('${prefix}', '@prefix@')
40gettext.bindtextdomain ("@GETTEXT_PACKAGE@", localedir)
41gettext.textdomain("orca")
42
43########################################################################
44#                                                                      #
45# Utility methods to facilitate easier translation                     #
46#                                                                      #
47########################################################################
48
49def C_(ctx, s):
50    """Provide qualified translatable strings via context."""
51    translated = cgettext('%s\x04%s' % (ctx, s))
52    if '\x04' in translated:
53        # no translation found, return input string
54        return s
55    return translated
56
57def setModuleLocale(moduleName, newLocale=None):
58    global _, ngettext, cgettext
59
60    try:
61        translation = gettext.translation('orca', localedir, languages=[newLocale])
62        _ = translation.gettext
63        ngettext = translation.ngettext
64        cgettext = translation.gettext
65    except:
66        _ = gettext.gettext
67        ngettext = gettext.ngettext
68        cgettext = gettext.gettext
69    module = sys.modules.get(moduleName)
70    if module:
71        imp.reload(module)
72
73def setLocaleForMessages(newLocale=None):
74    modules = ['orca.tutorialgenerator', 'orca.messages']
75    for module in modules:
76        setModuleLocale(module, newLocale)
77
78def setLocaleForNames(newLocale=None):
79    modules = ['orca.chnames', 'orca.keynames', 'orca.phonnames',
80               'orca.text_attribute_names', 'orca.object_properties',
81               'orca.cmdnames', 'orca.keybindings', 'orca.colornames',
82               'orca.mathsymbols']
83    for module in modules:
84        setModuleLocale(module, newLocale)
85
86def setLocaleForGUI(newLocale=None):
87    modules = ['orca.orca_gtkbuilder',
88               'orca.guilabels',
89               'orca.brltablenames']
90    for module in modules:
91        setModuleLocale(module, newLocale)
92