~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Jelmer Vernooij
  • Date: 2011-10-05 12:45:41 UTC
  • mfrom: (6190 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6216.
  • Revision ID: jelmer@samba.org-20111005124541-chv6scmle72z72gq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import os
27
27
import sys
28
28
 
 
29
 
29
30
_translations = None
30
31
 
31
32
 
82
83
 
83
84
 
84
85
def install(lang=None):
85
 
    """Enables gettext translations."""
 
86
    """Enables gettext translations in bzr."""
86
87
    global _translations
87
88
    if installed():
88
89
        return
 
90
    _translations = install_translations(lang)
 
91
 
 
92
 
 
93
def install_translations(lang=None, domain='bzr', locale_base=None):
 
94
    """Create a gettext translation object.
 
95
    
 
96
    :param lang: language to install.
 
97
    :param domain: translation domain to install.
 
98
    :param locale_base: plugins can specify their own directory.
 
99
 
 
100
    :returns: a gettext translations object to use
 
101
    """
89
102
    if lang is None:
90
103
        lang = _get_current_locale()
91
104
    if lang is not None:
92
105
        languages = lang.split(':')
93
106
    else:
94
107
        languages = None
95
 
    _translations = _gettext.translation(
96
 
            'bzr',
97
 
            localedir=_get_locale_dir(),
 
108
    translation = _gettext.translation(
 
109
            domain,
 
110
            localedir=_get_locale_dir(locale_base),
98
111
            languages=languages,
99
112
            fallback=True)
 
113
    return translation
 
114
 
 
115
 
 
116
def add_fallback(fallback):
 
117
    """
 
118
    Add a fallback translations object.  Typically used by plugins.
 
119
 
 
120
    :param fallback: gettext.GNUTranslations object
 
121
    """
 
122
    install()
 
123
    _translations.add_fallback(fallback)
100
124
 
101
125
 
102
126
def uninstall():
105
129
    _translations = None
106
130
 
107
131
 
108
 
def _get_locale_dir():
 
132
def _get_locale_dir(base):
 
133
    """Returns directory to find .mo translations file in, either local or system
 
134
 
 
135
    :param base: plugins can specify their own local directory
 
136
    """
109
137
    if hasattr(sys, 'frozen'):
110
 
        base = os.path.dirname(
 
138
        if base is None:
 
139
            base = os.path.dirname(
111
140
                unicode(sys.executable, sys.getfilesystemencoding()))
112
141
        return os.path.join(base, u'locale')
113
142
    else:
114
 
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
143
        if base is None:
 
144
            base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
115
145
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
116
146
        if os.path.exists(dirpath):
117
147
            return dirpath
160
190
        if lang:
161
191
            return lang
162
192
    return None
 
193
 
 
194
 
 
195
def load_plugin_translations(domain):
 
196
    """Load the translations for a specific plugin.
 
197
 
 
198
    :param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
 
199
    """
 
200
    locale_base = os.path.dirname(
 
201
        unicode(__file__, sys.getfilesystemencoding()))
 
202
    translation = install_translations(domain=domain,
 
203
        locale_base=locale_base)
 
204
    add_fallback(translation)
 
205
    return translation