1
# Miro - an RSS based video player application
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
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.
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.
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
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
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.
29
"""``miro.gtcache`` -- Caching gettext functions.
34
from miro import config
35
from miro import prefs
36
import miro.plat.utils
39
codeset = None # The default codeset of our locale (always lower case)
46
if not miro.plat.utils.locale_initialized():
47
raise Exception, "locale not initialized"
49
# try to set the locale to the platform default, but if that fails
50
# log a message and set it to C.
52
locale.setlocale(locale.LC_ALL, '')
54
logging.warn("gtcache.init: setlocale failed. setting locale to 'C'")
55
locale.setlocale(locale.LC_ALL, 'C')
57
# try to get the locale which might fail despite the above. see bug #11783.
59
codeset = locale.getlocale()[1]
61
logging.warn("gtcache.init: getlocale failed. setting locale to 'C'")
62
locale.setlocale(locale.LC_ALL, "C")
63
codeset = locale.getlocale()[1]
65
if codeset is not None:
66
codeset = codeset.lower()
68
_gt.bindtextdomain("miro", config.get(prefs.GETTEXT_PATHNAME))
69
_gt.textdomain("miro")
70
_gt.bind_textdomain_codeset("miro", "UTF-8")
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
80
>>> declarify("View|All")
83
Returns a unicode string.
86
return text.split("|")[-1]
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.
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.
98
For example, if the string is::
100
"%(countfiles) fichiers analyses"
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
112
Returns a unicode string.
114
text = text.encode('utf-8')
119
s = _gt.gettext(text).decode('utf-8')
123
print "gtcache.gettext: not initialized for string \"%s\"" % text
125
# traceback.print_stack()
135
logging.warn("gtcache.gettext: translation has bad formatting characters. returning english form. '%s'", text)
136
_gtcache[text] = text
139
def ngettext(text1, text2, count, values=None):
140
"""Given two strings and a count.
143
the singular form of the string to be translated
145
the plural form of the string to be translated
147
the number of things involved
149
the dict of values to expand the string with
151
See Python ``gettext.ngettext`` documentation and the GNU gettext
152
documentation for more details.
154
http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
156
Returns a unicode string.
158
text1 = text1.encode('utf-8')
159
text2 = text2.encode('utf-8')
161
s = _gt.ngettext(text1, text2, count).decode('utf-8')
169
logging.warn("gtcache.ngettext: translation has bad formatting characters. returning english form. '%s'", text1)
171
return text1 % values
173
return text2 % values