~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to portable/gtcache.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17
 
#
18
 
# In addition, as a special exception, the copyright holders give
19
 
# permission to link the code of portions of this program with the OpenSSL
20
 
# library.
21
 
#
22
 
# You must obey the GNU General Public License in all respects for all of
23
 
# the code used other than OpenSSL. If you modify file(s) with this
24
 
# exception, you may extend this exception to your version of the file(s),
25
 
# but you are not obligated to do so. If you do not wish to do so, delete
26
 
# this exception statement from your version. If you delete this exception
27
 
# statement from all source files in the program, then also delete it here.
28
 
 
29
 
"""``miro.gtcache`` -- Caching gettext functions.
30
 
"""
31
 
 
32
 
import gettext as _gt
33
 
import locale
34
 
from miro import config
35
 
from miro import prefs
36
 
import miro.plat.utils
37
 
 
38
 
_gtcache = None
39
 
codeset = None # The default codeset of our locale (always lower case)
40
 
 
41
 
def init():
42
 
    import logging
43
 
    global _gtcache
44
 
    global codeset
45
 
    _gtcache = {}
46
 
    if not miro.plat.utils.locale_initialized():
47
 
        raise Exception, "locale not initialized"
48
 
 
49
 
    # try to set the locale to the platform default, but if that fails
50
 
    # log a message and set it to C.
51
 
    try:
52
 
        locale.setlocale(locale.LC_ALL, '')
53
 
    except locale.Error:
54
 
        logging.warn("gtcache.init: setlocale failed.  setting locale to 'C'")
55
 
        locale.setlocale(locale.LC_ALL, 'C')
56
 
 
57
 
    # try to get the locale which might fail despite the above.  see bug #11783.
58
 
    try:
59
 
        codeset = locale.getlocale()[1]
60
 
    except ValueError:
61
 
        logging.warn("gtcache.init: getlocale failed.  setting locale to 'C'")
62
 
        locale.setlocale(locale.LC_ALL, "C")
63
 
        codeset = locale.getlocale()[1]
64
 
 
65
 
    if codeset is not None:
66
 
        codeset = codeset.lower()
67
 
 
68
 
    _gt.bindtextdomain("miro", config.get(prefs.GETTEXT_PATHNAME))
69
 
    _gt.textdomain("miro")
70
 
    _gt.bind_textdomain_codeset("miro", "UTF-8")
71
 
 
72
 
def declarify(text):
73
 
    """Takes the return from a gettext call, and "declarifies" it.  If the
74
 
    item has | symbols in it, then this splits the string on | and returns
75
 
    the last string.  If the item has no | symbols in it, then it returns
76
 
    the string.
77
 
 
78
 
    >>> declarify("foo")
79
 
    'foo'
80
 
    >>> declarify("View|All")
81
 
    'All'
82
 
 
83
 
    Returns a unicode string.
84
 
    """
85
 
    if "|" in text:
86
 
        return text.split("|")[-1]
87
 
    return text
88
 
 
89
 
def gettext(text, values=None):
90
 
    """Returns the translated form of the given text.  If values are provided,
91
 
    expands the string with the given values.
92
 
 
93
 
    In the case where the translated string is improperly formed and throws
94
 
    a ValueError when expanded, this caches and returns the original text.
95
 
    This reduces the likelihood that Miro will throw an error and stop
96
 
    functioning with bad translated strings.
97
 
 
98
 
    For example, if the string is::
99
 
 
100
 
        "%(countfiles) fichiers analyses"
101
 
                     ^^^
102
 
 
103
 
    the d is missing.
104
 
 
105
 
    .. Note::
106
 
 
107
 
       This converts unicode strings to strings in utf-8 encoding
108
 
       before translating.  This definitely slows things down, so if
109
 
       you don't need unicode characters, use a string and not a
110
 
       unicode.
111
 
 
112
 
    Returns a unicode string.
113
 
    """
114
 
    text = text.encode('utf-8')
115
 
    try:
116
 
        s = _gtcache[text]
117
 
 
118
 
    except KeyError:
119
 
        s = _gt.gettext(text).decode('utf-8')
120
 
        _gtcache[text] = s
121
 
 
122
 
    except TypeError:
123
 
        print "gtcache.gettext: not initialized for string \"%s\"" % text
124
 
        # import traceback
125
 
        # traceback.print_stack()
126
 
        return text
127
 
 
128
 
    try:
129
 
        if values:
130
 
            s = s % values
131
 
        return s
132
 
 
133
 
    except ValueError:
134
 
        import logging
135
 
        logging.warn("gtcache.gettext: translation has bad formatting characters.  returning english form.  '%s'", text)
136
 
        _gtcache[text] = text
137
 
        return text % values
138
 
 
139
 
def ngettext(text1, text2, count, values=None):
140
 
    """Given two strings and a count.
141
 
 
142
 
    text1
143
 
        the singular form of the string to be translated
144
 
    text2
145
 
        the plural form of the string to be translated
146
 
    count
147
 
        the number of things involved
148
 
    values
149
 
        the dict of values to expand the string with
150
 
 
151
 
    See Python ``gettext.ngettext`` documentation and the GNU gettext
152
 
    documentation for more details.
153
 
 
154
 
    http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
155
 
 
156
 
    Returns a unicode string.
157
 
    """
158
 
    text1 = text1.encode('utf-8')
159
 
    text2 = text2.encode('utf-8')
160
 
 
161
 
    s = _gt.ngettext(text1, text2, count).decode('utf-8')
162
 
    try:
163
 
        if values:
164
 
            s = s % values
165
 
        return s
166
 
 
167
 
    except ValueError:
168
 
        import logging
169
 
        logging.warn("gtcache.ngettext: translation has bad formatting characters.  returning english form.  '%s'", text1)
170
 
        if count == 1:
171
 
            return text1 % values
172
 
        else:
173
 
            return text2 % values