~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-27 15:42:09 UTC
  • mfrom: (5993 +trunk)
  • mto: (5993.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5994.
  • Revision ID: v.ladeuil+lp@free.fr-20110627154209-azubuhbuxsz109hq
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import os
27
27
import sys
28
28
 
29
 
 
30
29
_translations = None
31
30
 
32
31
 
35
34
    
36
35
    :returns: translated message as unicode.
37
36
    """
38
 
    install()
39
37
    return _translations.ugettext(message)
40
38
 
41
39
 
42
 
def ngettext(singular, plural, number):
43
 
    """Translate message with plural forms based on `number`.
44
 
 
45
 
    :param singular: English language message in singular form
46
 
    :param plural: English language message in plural form
47
 
    :param number: the number this message should be translated for
 
40
def ngettext(s, p, n):
 
41
    """Translate message based on `n`.
48
42
 
49
43
    :returns: translated message as unicode.
50
44
    """
51
 
    install()
52
 
    return _translations.ungettext(singular, plural, number)
 
45
    return _translations.ungettext(s, p, n)
53
46
 
54
47
 
55
48
def N_(msg):
62
55
 
63
56
    :returns: concatenated translated message as unicode.
64
57
    """
65
 
    install()
66
58
    paragraphs = message.split(u'\n\n')
67
59
    ugettext = _translations.ugettext
68
60
    # Be careful not to translate the empty string -- it holds the
70
62
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
71
63
 
72
64
 
73
 
def disable_i18n():
74
 
    """Do not allow i18n to be enabled.  Useful for third party users
75
 
    of bzrlib."""
76
 
    global _translations
77
 
    _translations = _gettext.NullTranslations()
78
 
 
79
 
 
80
65
def installed():
81
 
    """Returns whether translations are in use or not."""
82
66
    return _translations is not None
83
67
 
84
68
 
85
69
def install(lang=None):
86
 
    """Enables gettext translations in bzr."""
87
70
    global _translations
88
 
    if installed():
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
 
    """
102
71
    if lang is None:
103
72
        lang = _get_current_locale()
104
 
    if lang is not None:
105
 
        languages = lang.split(':')
106
 
    else:
107
 
        languages = None
108
 
    translation = _gettext.translation(
109
 
            domain,
110
 
            localedir=_get_locale_dir(locale_base),
111
 
            languages=languages,
 
73
    _translations = _gettext.translation(
 
74
            'bzr',
 
75
            localedir=_get_locale_dir(),
 
76
            languages=lang.split(':'),
112
77
            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)
124
78
 
125
79
 
126
80
def uninstall():
127
 
    """Disables gettext translations."""
128
81
    global _translations
129
82
    _translations = None
130
83
 
131
84
 
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
 
    """
 
85
def _get_locale_dir():
137
86
    if hasattr(sys, 'frozen'):
138
 
        if base is None:
139
 
            base = os.path.dirname(
 
87
        base = os.path.dirname(
140
88
                unicode(sys.executable, sys.getfilesystemencoding()))
141
89
        return os.path.join(base, u'locale')
142
90
    else:
143
 
        if base is None:
144
 
            base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
91
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
145
92
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
146
93
        if os.path.exists(dirpath):
147
94
            return dirpath
179
126
def _get_current_locale():
180
127
    if not os.environ.get('LANGUAGE'):
181
128
        from bzrlib import config
182
 
        lang = config.GlobalStack().get('language')
 
129
        lang = config.GlobalConfig().get_user_option('language')
183
130
        if lang:
184
131
            os.environ['LANGUAGE'] = lang
185
132
            return lang
190
137
        if lang:
191
138
            return lang
192
139
    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