Mercurial > babel > mirror
annotate babel/support.py @ 599:33c8c68b96c7 trunk
change constructor for babel.support.Translations to __init__(fp=None, domain=None) as its super class gettext.GNUTranslations uses "fp" as well (even if "fileobj" is a better name)
author | fschwarz |
---|---|
date | Mon, 20 Aug 2012 19:34:42 +0000 |
parents | 92e3eb0a317a |
children | 60f716236946 |
rev | line source |
---|---|
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
2 # |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
5 # |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://babel.edgewall.org/wiki/License. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
9 # |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://babel.edgewall.org/log/. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
13 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
14 """Several classes and functions that help with integrating and using Babel |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
15 in applications. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
16 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
17 .. note: the code in this module is not used by Babel itself |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
18 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
19 |
522 | 20 from datetime import date, datetime, timedelta |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
21 import gettext |
420 | 22 import locale |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
23 |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
24 from babel.core import Locale |
401
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
25 from babel.dates import format_date, format_datetime, format_time, \ |
522 | 26 format_timedelta |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
27 from babel.numbers import format_number, format_decimal, format_currency, \ |
522 | 28 format_percent, format_scientific |
29 from babel.util import UTC | |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
30 |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
31 __all__ = ['Format', 'LazyProxy', 'NullTranslations', 'Translations'] |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
32 __docformat__ = 'restructuredtext en' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
33 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
34 |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
35 class Format(object): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
36 """Wrapper class providing the various date and number formatting functions |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
37 bound to a specific locale and time-zone. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
38 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
39 >>> fmt = Format('en_US', UTC) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
40 >>> fmt.date(date(2007, 4, 1)) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
41 u'Apr 1, 2007' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
42 >>> fmt.decimal(1.2345) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
43 u'1.234' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
44 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
45 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
46 def __init__(self, locale, tzinfo=None): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
47 """Initialize the formatter. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
48 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
49 :param locale: the locale identifier or `Locale` instance |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
50 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
51 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
52 self.locale = Locale.parse(locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
53 self.tzinfo = tzinfo |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
54 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
55 def date(self, date=None, format='medium'): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
56 """Return a date formatted according to the given pattern. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
57 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
58 >>> fmt = Format('en_US') |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
59 >>> fmt.date(date(2007, 4, 1)) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
60 u'Apr 1, 2007' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
61 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
62 :see: `babel.dates.format_date` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
63 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
64 return format_date(date, format, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
65 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
66 def datetime(self, datetime=None, format='medium'): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
67 """Return a date and time formatted according to the given pattern. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
68 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
69 >>> from pytz import timezone |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
70 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
71 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
72 u'Apr 1, 2007 11:30:00 AM' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
73 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
74 :see: `babel.dates.format_datetime` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
75 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
76 return format_datetime(datetime, format, tzinfo=self.tzinfo, |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
77 locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
78 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
79 def time(self, time=None, format='medium'): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
80 """Return a time formatted according to the given pattern. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
81 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
82 >>> from pytz import timezone |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
83 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
348
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
336
diff
changeset
|
84 >>> fmt.time(datetime(2007, 4, 1, 15, 30)) |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
85 u'11:30:00 AM' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
86 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
87 :see: `babel.dates.format_time` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
88 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
89 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
90 |
401
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
91 def timedelta(self, delta, granularity='second', threshold=.85): |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
92 """Return a time delta according to the rules of the given locale. |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
93 |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
94 >>> fmt = Format('en_US') |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
95 >>> fmt.timedelta(timedelta(weeks=11)) |
430 | 96 u'3 mths' |
401
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
97 |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
98 :see: `babel.dates.format_timedelta` |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
99 """ |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
100 return format_timedelta(delta, granularity=granularity, |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
101 threshold=threshold, locale=self.locale) |
d1d285768c8c
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
352
diff
changeset
|
102 |
101
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
103 def number(self, number): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
104 """Return an integer number formatted for the locale. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
105 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
106 >>> fmt = Format('en_US') |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
107 >>> fmt.number(1099) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
108 u'1,099' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
109 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
110 :see: `babel.numbers.format_number` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
111 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
112 return format_number(number, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
113 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
114 def decimal(self, number, format=None): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
115 """Return a decimal number formatted for the locale. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
116 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
117 >>> fmt = Format('en_US') |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
118 >>> fmt.decimal(1.2345) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
119 u'1.234' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
120 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
121 :see: `babel.numbers.format_decimal` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
122 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
123 return format_decimal(number, format, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
124 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
125 def currency(self, number, currency): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
126 """Return a number in the given currency formatted for the locale. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
127 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
128 :see: `babel.numbers.format_currency` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
129 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
130 return format_currency(number, currency, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
131 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
132 def percent(self, number, format=None): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
133 """Return a number formatted as percentage for the locale. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
134 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
135 >>> fmt = Format('en_US') |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
136 >>> fmt.percent(0.34) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
137 u'34%' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
138 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
139 :see: `babel.numbers.format_percent` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
140 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
141 return format_percent(number, format, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
142 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
143 def scientific(self, number): |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
144 """Return a number formatted using scientific notation for the locale. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
145 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
146 :see: `babel.numbers.format_scientific` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
147 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
148 return format_scientific(number, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
149 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
150 |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
151 class LazyProxy(object): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
152 """Class for proxy objects that delegate to a specified function to evaluate |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
153 the actual object. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
154 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
155 >>> def greeting(name='world'): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
156 ... return 'Hello, %s!' % name |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
157 >>> lazy_greeting = LazyProxy(greeting, name='Joe') |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
158 >>> print lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
159 Hello, Joe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
160 >>> u' ' + lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
161 u' Hello, Joe!' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
162 >>> u'(%s)' % lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
163 u'(Hello, Joe!)' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
164 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
165 This can be used, for example, to implement lazy translation functions that |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
166 delay the actual translation until the string is actually used. The |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
167 rationale for such behavior is that the locale of the user may not always |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
168 be available. In web applications, you only know the locale when processing |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
169 a request. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
170 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
171 The proxy implementation attempts to be as complete as possible, so that |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
172 the lazy objects should mostly work as expected, for example for sorting: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
173 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
174 >>> greetings = [ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
175 ... LazyProxy(greeting, 'world'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
176 ... LazyProxy(greeting, 'Joe'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
177 ... LazyProxy(greeting, 'universe'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
178 ... ] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
179 >>> greetings.sort() |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
180 >>> for greeting in greetings: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
181 ... print greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
182 Hello, Joe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
183 Hello, universe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
184 Hello, world! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
185 """ |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
186 __slots__ = ['_func', '_args', '_kwargs', '_value', '_is_cache_enabled'] |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
187 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
188 def __init__(self, func, *args, **kwargs): |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
189 is_cache_enabled = kwargs.pop('enable_cache', True) |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
190 # Avoid triggering our own __setattr__ implementation |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
191 object.__setattr__(self, '_func', func) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
192 object.__setattr__(self, '_args', args) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
193 object.__setattr__(self, '_kwargs', kwargs) |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
194 object.__setattr__(self, '_is_cache_enabled', is_cache_enabled) |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
195 object.__setattr__(self, '_value', None) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
196 |
579 | 197 @property |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
198 def value(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
199 if self._value is None: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
200 value = self._func(*self._args, **self._kwargs) |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
201 if not self._is_cache_enabled: |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
202 return value |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
203 object.__setattr__(self, '_value', value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
204 return self._value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
205 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
206 def __contains__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
207 return key in self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
208 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
209 def __nonzero__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
210 return bool(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
211 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
212 def __dir__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
213 return dir(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
214 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
215 def __iter__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
216 return iter(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
217 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
218 def __len__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
219 return len(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
220 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
221 def __str__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
222 return str(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
223 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
224 def __unicode__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
225 return unicode(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
226 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
227 def __add__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
228 return self.value + other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
229 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
230 def __radd__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
231 return other + self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
232 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
233 def __mod__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
234 return self.value % other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
235 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
236 def __rmod__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
237 return other % self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
238 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
239 def __mul__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
240 return self.value * other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
241 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
242 def __rmul__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
243 return other * self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
244 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
245 def __call__(self, *args, **kwargs): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
246 return self.value(*args, **kwargs) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
247 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
248 def __lt__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
249 return self.value < other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
250 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
251 def __le__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
252 return self.value <= other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
253 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
254 def __eq__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
255 return self.value == other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
256 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
257 def __ne__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
258 return self.value != other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
259 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
260 def __gt__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
261 return self.value > other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
262 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
263 def __ge__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
264 return self.value >= other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
265 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
266 def __delattr__(self, name): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
267 delattr(self.value, name) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
268 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
269 def __getattr__(self, name): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
270 return getattr(self.value, name) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
271 |
100 | 272 def __setattr__(self, name, value): |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
273 setattr(self.value, name, value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
274 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
275 def __delitem__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
276 del self.value[key] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
277 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
278 def __getitem__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
279 return self.value[key] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
280 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
281 def __setitem__(self, key, value): |
100 | 282 self.value[key] = value |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
283 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
284 |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
285 class NullTranslations(gettext.NullTranslations, object): |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
286 |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
287 DEFAULT_DOMAIN = None |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
288 |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
289 def __init__(self, fp=None): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
290 """Initialize a simple translations class which is not backed by a |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
291 real catalog. Behaves similar to gettext.NullTranslations but also |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
292 offers Babel's on *gettext methods (e.g. 'dgettext()'). |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
293 |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
294 :param fp: a file-like object (ignored in this class) |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
295 """ |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
296 # These attributes are set by gettext.NullTranslations when a catalog |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
297 # is parsed (fp != None). Ensure that they are always present because |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
298 # some *gettext methods (including '.gettext()') rely on the attributes. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
299 self._catalog = {} |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
300 self.plural = lambda n: int(n != 1) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
301 super(NullTranslations, self).__init__(fp=fp) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
302 self.files = filter(None, [getattr(fp, 'name', None)]) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
303 self.domain = self.DEFAULT_DOMAIN |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
304 self._domains = {} |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
305 |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
306 def dgettext(self, domain, message): |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
307 """Like ``gettext()``, but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
308 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
309 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
310 return self._domains.get(domain, self).gettext(message) |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
311 |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
312 def ldgettext(self, domain, message): |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
313 """Like ``lgettext()``, but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
314 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
315 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
316 return self._domains.get(domain, self).lgettext(message) |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
317 |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
318 def udgettext(self, domain, message): |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
319 """Like ``ugettext()``, but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
320 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
321 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
322 return self._domains.get(domain, self).ugettext(message) |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
323 # backward compatibility with 0.9 |
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
324 dugettext = udgettext |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
325 |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
326 def dngettext(self, domain, singular, plural, num): |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
327 """Like ``ngettext()``, but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
328 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
329 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
330 return self._domains.get(domain, self).ngettext(singular, plural, num) |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
331 |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
332 def ldngettext(self, domain, singular, plural, num): |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
333 """Like ``lngettext()``, but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
334 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
335 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
336 return self._domains.get(domain, self).lngettext(singular, plural, num) |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
337 |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
338 def udngettext(self, domain, singular, plural, num): |
408
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
339 """Like ``ungettext()`` but look the message up in the specified |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
340 domain. |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
341 """ |
62cc40fa04eb
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
402
diff
changeset
|
342 return self._domains.get(domain, self).ungettext(singular, plural, num) |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
343 # backward compatibility with 0.9 |
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
344 dungettext = udngettext |
420 | 345 |
346 # Most of the downwards code, until it get's included in stdlib, from: | |
347 # http://bugs.python.org/file10036/gettext-pgettext.patch | |
348 # | |
349 # The encoding of a msgctxt and a msgid in a .mo file is | |
350 # msgctxt + "\x04" + msgid (gettext version >= 0.15) | |
351 CONTEXT_ENCODING = '%s\x04%s' | |
352 | |
353 def pgettext(self, context, message): | |
354 """Look up the `context` and `message` id in the catalog and return the | |
355 corresponding message string, as an 8-bit string encoded with the | |
356 catalog's charset encoding, if known. If there is no entry in the | |
357 catalog for the `message` id and `context` , and a fallback has been | |
358 set, the look up is forwarded to the fallback's ``pgettext()`` | |
359 method. Otherwise, the `message` id is returned. | |
360 """ | |
361 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) | |
362 missing = object() | |
363 tmsg = self._catalog.get(ctxt_msg_id, missing) | |
364 if tmsg is missing: | |
365 if self._fallback: | |
366 return self._fallback.pgettext(context, message) | |
367 return message | |
368 # Encode the Unicode tmsg back to an 8-bit string, if possible | |
369 if self._output_charset: | |
370 return tmsg.encode(self._output_charset) | |
371 elif self._charset: | |
372 return tmsg.encode(self._charset) | |
373 return tmsg | |
374 | |
375 def lpgettext(self, context, message): | |
376 """Equivalent to ``pgettext()``, but the translation is returned in the | |
377 preferred system encoding, if no other encoding was explicitly set with | |
378 ``bind_textdomain_codeset()``. | |
379 """ | |
380 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) | |
381 missing = object() | |
382 tmsg = self._catalog.get(ctxt_msg_id, missing) | |
383 if tmsg is missing: | |
384 if self._fallback: | |
385 return self._fallback.lpgettext(context, message) | |
386 return message | |
387 if self._output_charset: | |
388 return tmsg.encode(self._output_charset) | |
389 return tmsg.encode(locale.getpreferredencoding()) | |
390 | |
391 def npgettext(self, context, singular, plural, num): | |
392 """Do a plural-forms lookup of a message id. `singular` is used as the | |
393 message id for purposes of lookup in the catalog, while `num` is used to | |
394 determine which plural form to use. The returned message string is an | |
395 8-bit string encoded with the catalog's charset encoding, if known. | |
396 | |
397 If the message id for `context` is not found in the catalog, and a | |
398 fallback is specified, the request is forwarded to the fallback's | |
399 ``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is | |
400 returned, and ``plural`` is returned in all other cases. | |
401 """ | |
402 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular) | |
403 try: | |
404 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))] | |
405 if self._output_charset: | |
406 return tmsg.encode(self._output_charset) | |
407 elif self._charset: | |
408 return tmsg.encode(self._charset) | |
409 return tmsg | |
410 except KeyError: | |
411 if self._fallback: | |
412 return self._fallback.npgettext(context, singular, plural, num) | |
413 if num == 1: | |
414 return singular | |
415 else: | |
416 return plural | |
417 | |
418 def lnpgettext(self, context, singular, plural, num): | |
419 """Equivalent to ``npgettext()``, but the translation is returned in the | |
420 preferred system encoding, if no other encoding was explicitly set with | |
421 ``bind_textdomain_codeset()``. | |
422 """ | |
423 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular) | |
424 try: | |
425 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))] | |
426 if self._output_charset: | |
427 return tmsg.encode(self._output_charset) | |
428 return tmsg.encode(locale.getpreferredencoding()) | |
429 except KeyError: | |
430 if self._fallback: | |
431 return self._fallback.lnpgettext(context, singular, plural, num) | |
432 if num == 1: | |
433 return singular | |
434 else: | |
435 return plural | |
436 | |
437 def upgettext(self, context, message): | |
438 """Look up the `context` and `message` id in the catalog and return the | |
439 corresponding message string, as a Unicode string. If there is no entry | |
440 in the catalog for the `message` id and `context`, and a fallback has | |
441 been set, the look up is forwarded to the fallback's ``upgettext()`` | |
442 method. Otherwise, the `message` id is returned. | |
443 """ | |
444 ctxt_message_id = self.CONTEXT_ENCODING % (context, message) | |
445 missing = object() | |
446 tmsg = self._catalog.get(ctxt_message_id, missing) | |
447 if tmsg is missing: | |
448 if self._fallback: | |
449 return self._fallback.upgettext(context, message) | |
450 return unicode(message) | |
451 return tmsg | |
452 | |
453 def unpgettext(self, context, singular, plural, num): | |
454 """Do a plural-forms lookup of a message id. `singular` is used as the | |
455 message id for purposes of lookup in the catalog, while `num` is used to | |
456 determine which plural form to use. The returned message string is a | |
457 Unicode string. | |
458 | |
459 If the message id for `context` is not found in the catalog, and a | |
460 fallback is specified, the request is forwarded to the fallback's | |
461 ``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is | |
462 returned, and `plural` is returned in all other cases. | |
463 """ | |
464 ctxt_message_id = self.CONTEXT_ENCODING % (context, singular) | |
465 try: | |
466 tmsg = self._catalog[(ctxt_message_id, self.plural(num))] | |
467 except KeyError: | |
468 if self._fallback: | |
469 return self._fallback.unpgettext(context, singular, plural, num) | |
470 if num == 1: | |
471 tmsg = unicode(singular) | |
472 else: | |
473 tmsg = unicode(plural) | |
474 return tmsg | |
475 | |
476 def dpgettext(self, domain, context, message): | |
477 """Like `pgettext()`, but look the message up in the specified | |
478 `domain`. | |
479 """ | |
480 return self._domains.get(domain, self).pgettext(context, message) | |
481 | |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
482 def udpgettext(self, domain, context, message): |
420 | 483 """Like `upgettext()`, but look the message up in the specified |
484 `domain`. | |
485 """ | |
486 return self._domains.get(domain, self).upgettext(context, message) | |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
487 # backward compatibility with 0.9 |
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
488 dupgettext = udpgettext |
420 | 489 |
490 def ldpgettext(self, domain, context, message): | |
491 """Equivalent to ``dpgettext()``, but the translation is returned in the | |
492 preferred system encoding, if no other encoding was explicitly set with | |
493 ``bind_textdomain_codeset()``. | |
494 """ | |
495 return self._domains.get(domain, self).lpgettext(context, message) | |
496 | |
497 def dnpgettext(self, domain, context, singular, plural, num): | |
498 """Like ``npgettext``, but look the message up in the specified | |
499 `domain`. | |
500 """ | |
501 return self._domains.get(domain, self).npgettext(context, singular, | |
502 plural, num) | |
503 | |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
504 def udnpgettext(self, domain, context, singular, plural, num): |
420 | 505 """Like ``unpgettext``, but look the message up in the specified |
506 `domain`. | |
507 """ | |
508 return self._domains.get(domain, self).unpgettext(context, singular, | |
509 plural, num) | |
561
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
510 # backward compatibility with 0.9 |
7c81bb0d32e5
rename several du*gettext methods() to ud*gettext for more consistent naming (fixes #263). The old names are still valid and are not deprecetad as these names are commonly used also in other software.
fschwarz
parents:
559
diff
changeset
|
511 dunpgettext = udnpgettext |
420 | 512 |
513 def ldnpgettext(self, domain, context, singular, plural, num): | |
514 """Equivalent to ``dnpgettext()``, but the translation is returned in | |
515 the preferred system encoding, if no other encoding was explicitly set | |
516 with ``bind_textdomain_codeset()``. | |
517 """ | |
518 return self._domains.get(domain, self).lnpgettext(context, singular, | |
519 plural, num) | |
520 | |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
521 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
522 class Translations(NullTranslations, gettext.GNUTranslations): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
523 """An extended translation catalog class.""" |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
524 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
525 DEFAULT_DOMAIN = 'messages' |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
526 |
599
33c8c68b96c7
change constructor for babel.support.Translations to __init__(fp=None, domain=None) as its super class gettext.GNUTranslations uses "fp" as well (even if "fileobj" is a better name)
fschwarz
parents:
597
diff
changeset
|
527 def __init__(self, fp=None, domain=None): |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
528 """Initialize the translations catalog. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
529 |
599
33c8c68b96c7
change constructor for babel.support.Translations to __init__(fp=None, domain=None) as its super class gettext.GNUTranslations uses "fp" as well (even if "fileobj" is a better name)
fschwarz
parents:
597
diff
changeset
|
530 :param fp: the file-like object the translation should be read from |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
531 :param domain: the message domain (default: 'messages') |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
532 """ |
599
33c8c68b96c7
change constructor for babel.support.Translations to __init__(fp=None, domain=None) as its super class gettext.GNUTranslations uses "fp" as well (even if "fileobj" is a better name)
fschwarz
parents:
597
diff
changeset
|
533 super(Translations, self).__init__(fp=fp) |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
534 self.domain = domain or self.DEFAULT_DOMAIN |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
535 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
536 @classmethod |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
537 def load(cls, dirname=None, locales=None, domain=None): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
538 """Load translations from the given directory. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
539 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
540 :param dirname: the directory containing the ``MO`` files |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
541 :param locales: the list of locales in order of preference (items in |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
542 this list can be either `Locale` objects or locale |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
543 strings) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
544 :param domain: the message domain (default: 'messages') |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
545 :return: the loaded catalog, or a ``NullTranslations`` instance if no |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
546 matching translations were found |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
547 :rtype: `Translations` |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
548 """ |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
549 if locales is not None: |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
550 if not isinstance(locales, (list, tuple)): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
551 locales = [locales] |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
552 locales = [str(locale) for locale in locales] |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
553 if not domain: |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
554 domain = cls.DEFAULT_DOMAIN |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
555 filename = gettext.find(domain, dirname, locales) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
556 if not filename: |
597
92e3eb0a317a
".load()" (babel.support.Translations) now returns Babel's NullTranslation (babel.support.NullTranslation) instead of gettext.NullTranslations
fschwarz
parents:
596
diff
changeset
|
557 return NullTranslations() |
596
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
558 return cls(fileobj=open(filename, 'rb'), domain=domain) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
559 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
560 def __repr__(self): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
561 return '<%s: "%s">' % (type(self).__name__, |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
562 self._info.get('project-id-version')) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
563 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
564 def add(self, translations, merge=True): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
565 """Add the given translations to the catalog. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
566 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
567 If the domain of the translations is different than that of the |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
568 current catalog, they are added as a catalog that is only accessible |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
569 by the various ``d*gettext`` functions. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
570 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
571 :param translations: the `Translations` instance with the messages to |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
572 add |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
573 :param merge: whether translations for message domains that have |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
574 already been added should be merged with the existing |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
575 translations |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
576 :return: the `Translations` instance (``self``) so that `merge` calls |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
577 can be easily chained |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
578 :rtype: `Translations` |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
579 """ |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
580 domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
581 if merge and domain == self.domain: |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
582 return self.merge(translations) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
583 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
584 existing = self._domains.get(domain) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
585 if merge and existing is not None: |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
586 existing.merge(translations) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
587 else: |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
588 translations.add_fallback(self) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
589 self._domains[domain] = translations |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
590 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
591 return self |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
592 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
593 def merge(self, translations): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
594 """Merge the given translations into the catalog. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
595 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
596 Message translations in the specified catalog override any messages |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
597 with the same identifier in the existing catalog. |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
598 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
599 :param translations: the `Translations` instance with the messages to |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
600 merge |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
601 :return: the `Translations` instance (``self``) so that `merge` calls |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
602 can be easily chained |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
603 :rtype: `Translations` |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
604 """ |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
605 if isinstance(translations, gettext.GNUTranslations): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
606 self._catalog.update(translations._catalog) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
607 if isinstance(translations, Translations): |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
608 self.files.extend(translations.files) |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
609 |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
610 return self |
f63a07d648b6
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents:
595
diff
changeset
|
611 |