~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-27 15:45:28 UTC
  • mfrom: (6162.4.10 i18n-plugins)
  • Revision ID: pqm@pqm.ubuntu.com-20110927154528-2mxat4qopuupi9kq
(jr) Changes to i18n.py to help support plugins with translations (Jonathan
 Riddell)

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