Mercurial > babel > old > mirror
annotate babel/support.py @ 524:335d0284d903
Get rid of some unused imports.
author | jruigrok |
---|---|
date | Sat, 05 Mar 2011 14:50:16 +0000 |
parents | b4f01db57a9e |
children | e93f68837913 |
rev | line source |
---|---|
63
a60ecd4a4954
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 -*- |
a60ecd4a4954
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 # |
a60ecd4a4954
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
|
3 # Copyright (C) 2007 Edgewall Software |
a60ecd4a4954
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. |
a60ecd4a4954
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 # |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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. |
a60ecd4a4954
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 # |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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/. |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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. |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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 """ |
a60ecd4a4954
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 |
524 | 20 from datetime import date, datetime, timedelta |
63
a60ecd4a4954
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 |
422 | 22 import locale |
63
a60ecd4a4954
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 |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
24 from babel.core import Locale |
403
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
25 from babel.dates import format_date, format_datetime, format_time, \ |
524 | 26 format_timedelta |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
27 from babel.numbers import format_number, format_decimal, format_currency, \ |
524 | 28 format_percent, format_scientific |
29 from babel.util import UTC | |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
30 |
354
249aab27c4b3
The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents:
353
diff
changeset
|
31 __all__ = ['Format', 'LazyProxy', 'Translations'] |
63
a60ecd4a4954
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' |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
35 class Format(object): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
36 """Wrapper class providing the various date and number formatting functions |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
37 bound to a specific locale and time-zone. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
38 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
39 >>> fmt = Format('en_US', UTC) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
40 >>> fmt.date(date(2007, 4, 1)) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
41 u'Apr 1, 2007' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
42 >>> fmt.decimal(1.2345) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
43 u'1.234' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
44 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
45 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
46 def __init__(self, locale, tzinfo=None): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
47 """Initialize the formatter. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
48 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
49 :param locale: the locale identifier or `Locale` instance |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
50 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
51 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
52 self.locale = Locale.parse(locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
53 self.tzinfo = tzinfo |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
54 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
55 def date(self, date=None, format='medium'): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
56 """Return a date formatted according to the given pattern. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
57 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
58 >>> fmt = Format('en_US') |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
59 >>> fmt.date(date(2007, 4, 1)) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
60 u'Apr 1, 2007' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
61 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
62 :see: `babel.dates.format_date` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
63 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
64 return format_date(date, format, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
65 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
66 def datetime(self, datetime=None, format='medium'): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
67 """Return a date and time formatted according to the given pattern. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
68 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
69 >>> from pytz import timezone |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
70 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
71 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
72 u'Apr 1, 2007 11:30:00 AM' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
73 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
74 :see: `babel.dates.format_datetime` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
75 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
76 return format_datetime(datetime, format, tzinfo=self.tzinfo, |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
77 locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
78 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
79 def time(self, time=None, format='medium'): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
80 """Return a time formatted according to the given pattern. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
81 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
82 >>> from pytz import timezone |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
83 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
350
e5db561bf70e
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
338
diff
changeset
|
84 >>> fmt.time(datetime(2007, 4, 1, 15, 30)) |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
85 u'11:30:00 AM' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
86 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
87 :see: `babel.dates.format_time` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
88 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
89 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
90 |
403
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
91 def timedelta(self, delta, granularity='second', threshold=.85): |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
92 """Return a time delta according to the rules of the given locale. |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
93 |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
94 >>> fmt = Format('en_US') |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
95 >>> fmt.timedelta(timedelta(weeks=11)) |
432 | 96 u'3 mths' |
403
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
97 |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
98 :see: `babel.dates.format_timedelta` |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
99 """ |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
100 return format_timedelta(delta, granularity=granularity, |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
101 threshold=threshold, locale=self.locale) |
3a0cea7b8050
Add `timedelta` function to the `Format` support class.
cmlenz
parents:
354
diff
changeset
|
102 |
103
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
103 def number(self, number): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
104 """Return an integer number formatted for the locale. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
105 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
106 >>> fmt = Format('en_US') |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
107 >>> fmt.number(1099) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
108 u'1,099' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
109 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
110 :see: `babel.numbers.format_number` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
111 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
112 return format_number(number, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
113 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
114 def decimal(self, number, format=None): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
115 """Return a decimal number formatted for the locale. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
116 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
117 >>> fmt = Format('en_US') |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
118 >>> fmt.decimal(1.2345) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
119 u'1.234' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
120 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
121 :see: `babel.numbers.format_decimal` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
122 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
123 return format_decimal(number, format, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
124 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
125 def currency(self, number, currency): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
126 """Return a number in the given currency formatted for the locale. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
127 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
128 :see: `babel.numbers.format_currency` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
129 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
130 return format_currency(number, currency, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
131 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
132 def percent(self, number, format=None): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
133 """Return a number formatted as percentage for the locale. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
134 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
135 >>> fmt = Format('en_US') |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
136 >>> fmt.percent(0.34) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
137 u'34%' |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
138 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
139 :see: `babel.numbers.format_percent` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
140 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
141 return format_percent(number, format, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
142 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
143 def scientific(self, number): |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
144 """Return a number formatted using scientific notation for the locale. |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
145 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
146 :see: `babel.numbers.format_scientific` |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
147 """ |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
148 return format_scientific(number, locale=self.locale) |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
149 |
1ba215a5774d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
102
diff
changeset
|
150 |
63
a60ecd4a4954
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): |
a60ecd4a4954
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 |
a60ecd4a4954
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. |
a60ecd4a4954
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 |
a60ecd4a4954
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'): |
a60ecd4a4954
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 |
a60ecd4a4954
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') |
a60ecd4a4954
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 |
a60ecd4a4954
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! |
a60ecd4a4954
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 |
a60ecd4a4954
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!' |
a60ecd4a4954
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 |
a60ecd4a4954
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!)' |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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. |
a60ecd4a4954
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 |
a60ecd4a4954
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 |
a60ecd4a4954
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: |
a60ecd4a4954
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 |
a60ecd4a4954
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 = [ |
a60ecd4a4954
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'), |
a60ecd4a4954
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'), |
a60ecd4a4954
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'), |
a60ecd4a4954
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 ... ] |
a60ecd4a4954
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() |
a60ecd4a4954
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: |
a60ecd4a4954
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 |
a60ecd4a4954
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! |
a60ecd4a4954
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! |
a60ecd4a4954
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! |
a60ecd4a4954
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 """ |
a60ecd4a4954
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
|
186 __slots__ = ['_func', '_args', '_kwargs', '_value'] |
a60ecd4a4954
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 |
a60ecd4a4954
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): |
a60ecd4a4954
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
|
189 # Avoid triggering our own __setattr__ implementation |
a60ecd4a4954
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 object.__setattr__(self, '_func', func) |
a60ecd4a4954
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, '_args', args) |
a60ecd4a4954
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, '_kwargs', kwargs) |
a60ecd4a4954
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, '_value', None) |
a60ecd4a4954
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
|
194 |
a60ecd4a4954
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 def value(self): |
a60ecd4a4954
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 if self._value is None: |
a60ecd4a4954
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
|
197 value = self._func(*self._args, **self._kwargs) |
a60ecd4a4954
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 object.__setattr__(self, '_value', value) |
a60ecd4a4954
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 return self._value |
a60ecd4a4954
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 = property(value) |
a60ecd4a4954
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
|
201 |
a60ecd4a4954
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
|
202 def __contains__(self, key): |
a60ecd4a4954
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 return key in self.value |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __nonzero__(self): |
a60ecd4a4954
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 return bool(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __dir__(self): |
a60ecd4a4954
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 return dir(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __iter__(self): |
a60ecd4a4954
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 return iter(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __len__(self): |
a60ecd4a4954
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 return len(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __str__(self): |
a60ecd4a4954
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 return str(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __unicode__(self): |
a60ecd4a4954
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 return unicode(self.value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __add__(self, other): |
a60ecd4a4954
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 return self.value + other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __radd__(self, other): |
a60ecd4a4954
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 return other + self.value |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __mod__(self, other): |
a60ecd4a4954
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 return self.value % other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __rmod__(self, other): |
a60ecd4a4954
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 return other % self.value |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __mul__(self, other): |
a60ecd4a4954
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 return self.value * other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __rmul__(self, other): |
a60ecd4a4954
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 return other * self.value |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __call__(self, *args, **kwargs): |
a60ecd4a4954
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 return self.value(*args, **kwargs) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __lt__(self, other): |
a60ecd4a4954
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 return self.value < other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __le__(self, other): |
a60ecd4a4954
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 return self.value <= other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __eq__(self, other): |
a60ecd4a4954
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 return self.value == other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __ne__(self, other): |
a60ecd4a4954
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 return self.value != other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __gt__(self, other): |
a60ecd4a4954
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 return self.value > other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __ge__(self, other): |
a60ecd4a4954
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 return self.value >= other |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __delattr__(self, name): |
a60ecd4a4954
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 delattr(self.value, name) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __getattr__(self, name): |
a60ecd4a4954
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 return getattr(self.value, name) |
a60ecd4a4954
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 |
102 | 268 def __setattr__(self, name, value): |
63
a60ecd4a4954
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 setattr(self.value, name, value) |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __delitem__(self, key): |
a60ecd4a4954
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
|
272 del self.value[key] |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __getitem__(self, key): |
a60ecd4a4954
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 return self.value[key] |
a60ecd4a4954
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 |
a60ecd4a4954
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 def __setitem__(self, key, value): |
102 | 278 self.value[key] = value |
63
a60ecd4a4954
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 |
404
7a3ab3e0dcb0
Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents:
403
diff
changeset
|
280 |
7a3ab3e0dcb0
Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents:
403
diff
changeset
|
281 class Translations(gettext.GNUTranslations, object): |
63
a60ecd4a4954
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
|
282 """An extended translation catalog class.""" |
a60ecd4a4954
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 |
a60ecd4a4954
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 DEFAULT_DOMAIN = 'messages' |
a60ecd4a4954
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
|
285 |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
286 def __init__(self, fileobj=None, domain=DEFAULT_DOMAIN): |
63
a60ecd4a4954
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
|
287 """Initialize the translations catalog. |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
288 |
63
a60ecd4a4954
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
|
289 :param fileobj: the file-like object the translation should be read |
a60ecd4a4954
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
|
290 from |
a60ecd4a4954
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
|
291 """ |
a60ecd4a4954
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
|
292 gettext.GNUTranslations.__init__(self, fp=fileobj) |
338
398e7c377165
Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents:
285
diff
changeset
|
293 self.files = filter(None, [getattr(fileobj, 'name', None)]) |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
294 self.domain = domain |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
295 self._domains = {} |
63
a60ecd4a4954
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
|
296 |
a60ecd4a4954
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
|
297 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): |
a60ecd4a4954
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
|
298 """Load translations from the given directory. |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
299 |
63
a60ecd4a4954
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
|
300 :param dirname: the directory containing the ``MO`` files |
a60ecd4a4954
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
|
301 :param locales: the list of locales in order of preference (items in |
a60ecd4a4954
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
|
302 this list can be either `Locale` objects or locale |
a60ecd4a4954
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
|
303 strings) |
a60ecd4a4954
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
|
304 :param domain: the message domain |
a60ecd4a4954
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 :return: the loaded catalog, or a ``NullTranslations`` instance if no |
a60ecd4a4954
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
|
306 matching translations were found |
a60ecd4a4954
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
|
307 :rtype: `Translations` |
a60ecd4a4954
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
|
308 """ |
338
398e7c377165
Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents:
285
diff
changeset
|
309 if locales is not None: |
398e7c377165
Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents:
285
diff
changeset
|
310 if not isinstance(locales, (list, tuple)): |
398e7c377165
Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents:
285
diff
changeset
|
311 locales = [locales] |
398e7c377165
Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents:
285
diff
changeset
|
312 locales = [str(locale) for locale in locales] |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
313 if not domain: |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
314 domain = cls.DEFAULT_DOMAIN |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
315 filename = gettext.find(domain, dirname, locales) |
63
a60ecd4a4954
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
|
316 if not filename: |
a60ecd4a4954
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
|
317 return gettext.NullTranslations() |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
318 return cls(fileobj=open(filename, 'rb'), domain=domain) |
63
a60ecd4a4954
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
|
319 load = classmethod(load) |
a60ecd4a4954
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
|
320 |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
321 def __repr__(self): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
322 return '<%s: "%s">' % (type(self).__name__, |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
323 self._info.get('project-id-version')) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
324 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
325 def add(self, translations, merge=True): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
326 """Add the given translations to the catalog. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
327 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
328 If the domain of the translations is different than that of the |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
329 current catalog, they are added as a catalog that is only accessible |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
330 by the various ``d*gettext`` functions. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
331 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
332 :param translations: the `Translations` instance with the messages to |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
333 add |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
334 :param merge: whether translations for message domains that have |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
335 already been added should be merged with the existing |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
336 translations |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
337 :return: the `Translations` instance (``self``) so that `merge` calls |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
338 can be easily chained |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
339 :rtype: `Translations` |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
340 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
341 domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
342 if merge and domain == self.domain: |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
343 return self.merge(translations) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
344 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
345 existing = self._domains.get(domain) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
346 if merge and existing is not None: |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
347 existing.merge(translations) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
348 else: |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
349 translations.add_fallback(self) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
350 self._domains[domain] = translations |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
351 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
352 return self |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
353 |
63
a60ecd4a4954
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
|
354 def merge(self, translations): |
a60ecd4a4954
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
|
355 """Merge the given translations into the catalog. |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
356 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
357 Message translations in the specified catalog override any messages |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
358 with the same identifier in the existing catalog. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
359 |
63
a60ecd4a4954
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
|
360 :param translations: the `Translations` instance with the messages to |
a60ecd4a4954
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
|
361 merge |
a60ecd4a4954
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
|
362 :return: the `Translations` instance (``self``) so that `merge` calls |
a60ecd4a4954
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
|
363 can be easily chained |
a60ecd4a4954
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
|
364 :rtype: `Translations` |
a60ecd4a4954
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
|
365 """ |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
366 if isinstance(translations, gettext.GNUTranslations): |
63
a60ecd4a4954
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
|
367 self._catalog.update(translations._catalog) |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
368 if isinstance(translations, Translations): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
369 self.files.extend(translations.files) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
370 |
63
a60ecd4a4954
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
|
371 return self |
a60ecd4a4954
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
|
372 |
410
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
373 def dgettext(self, domain, message): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
374 """Like ``gettext()``, but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
375 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
376 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
377 return self._domains.get(domain, self).gettext(message) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
378 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
379 def ldgettext(self, domain, message): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
380 """Like ``lgettext()``, but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
381 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
382 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
383 return self._domains.get(domain, self).lgettext(message) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
384 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
385 def dugettext(self, domain, message): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
386 """Like ``ugettext()``, but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
387 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
388 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
389 return self._domains.get(domain, self).ugettext(message) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
390 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
391 def dngettext(self, domain, singular, plural, num): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
392 """Like ``ngettext()``, but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
393 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
394 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
395 return self._domains.get(domain, self).ngettext(singular, plural, num) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
396 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
397 def ldngettext(self, domain, singular, plural, num): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
398 """Like ``lngettext()``, but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
399 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
400 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
401 return self._domains.get(domain, self).lngettext(singular, plural, num) |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
402 |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
403 def dungettext(self, domain, singular, plural, num): |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
404 """Like ``ungettext()`` but look the message up in the specified |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
405 domain. |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
406 """ |
dc5b9feeab62
Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents:
404
diff
changeset
|
407 return self._domains.get(domain, self).ungettext(singular, plural, num) |
422 | 408 |
409 # Most of the downwards code, until it get's included in stdlib, from: | |
410 # http://bugs.python.org/file10036/gettext-pgettext.patch | |
411 # | |
412 # The encoding of a msgctxt and a msgid in a .mo file is | |
413 # msgctxt + "\x04" + msgid (gettext version >= 0.15) | |
414 CONTEXT_ENCODING = '%s\x04%s' | |
415 | |
416 def pgettext(self, context, message): | |
417 """Look up the `context` and `message` id in the catalog and return the | |
418 corresponding message string, as an 8-bit string encoded with the | |
419 catalog's charset encoding, if known. If there is no entry in the | |
420 catalog for the `message` id and `context` , and a fallback has been | |
421 set, the look up is forwarded to the fallback's ``pgettext()`` | |
422 method. Otherwise, the `message` id is returned. | |
423 """ | |
424 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) | |
425 missing = object() | |
426 tmsg = self._catalog.get(ctxt_msg_id, missing) | |
427 if tmsg is missing: | |
428 if self._fallback: | |
429 return self._fallback.pgettext(context, message) | |
430 return message | |
431 # Encode the Unicode tmsg back to an 8-bit string, if possible | |
432 if self._output_charset: | |
433 return tmsg.encode(self._output_charset) | |
434 elif self._charset: | |
435 return tmsg.encode(self._charset) | |
436 return tmsg | |
437 | |
438 def lpgettext(self, context, message): | |
439 """Equivalent to ``pgettext()``, but the translation is returned in the | |
440 preferred system encoding, if no other encoding was explicitly set with | |
441 ``bind_textdomain_codeset()``. | |
442 """ | |
443 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) | |
444 missing = object() | |
445 tmsg = self._catalog.get(ctxt_msg_id, missing) | |
446 if tmsg is missing: | |
447 if self._fallback: | |
448 return self._fallback.lpgettext(context, message) | |
449 return message | |
450 if self._output_charset: | |
451 return tmsg.encode(self._output_charset) | |
452 return tmsg.encode(locale.getpreferredencoding()) | |
453 | |
454 def npgettext(self, context, singular, plural, num): | |
455 """Do a plural-forms lookup of a message id. `singular` is used as the | |
456 message id for purposes of lookup in the catalog, while `num` is used to | |
457 determine which plural form to use. The returned message string is an | |
458 8-bit string encoded with the catalog's charset encoding, if known. | |
459 | |
460 If the message id for `context` is not found in the catalog, and a | |
461 fallback is specified, the request is forwarded to the fallback's | |
462 ``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is | |
463 returned, and ``plural`` is returned in all other cases. | |
464 """ | |
465 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular) | |
466 try: | |
467 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))] | |
468 if self._output_charset: | |
469 return tmsg.encode(self._output_charset) | |
470 elif self._charset: | |
471 return tmsg.encode(self._charset) | |
472 return tmsg | |
473 except KeyError: | |
474 if self._fallback: | |
475 return self._fallback.npgettext(context, singular, plural, num) | |
476 if num == 1: | |
477 return singular | |
478 else: | |
479 return plural | |
480 | |
481 def lnpgettext(self, context, singular, plural, num): | |
482 """Equivalent to ``npgettext()``, but the translation is returned in the | |
483 preferred system encoding, if no other encoding was explicitly set with | |
484 ``bind_textdomain_codeset()``. | |
485 """ | |
486 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular) | |
487 try: | |
488 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))] | |
489 if self._output_charset: | |
490 return tmsg.encode(self._output_charset) | |
491 return tmsg.encode(locale.getpreferredencoding()) | |
492 except KeyError: | |
493 if self._fallback: | |
494 return self._fallback.lnpgettext(context, singular, plural, num) | |
495 if num == 1: | |
496 return singular | |
497 else: | |
498 return plural | |
499 | |
500 def upgettext(self, context, message): | |
501 """Look up the `context` and `message` id in the catalog and return the | |
502 corresponding message string, as a Unicode string. If there is no entry | |
503 in the catalog for the `message` id and `context`, and a fallback has | |
504 been set, the look up is forwarded to the fallback's ``upgettext()`` | |
505 method. Otherwise, the `message` id is returned. | |
506 """ | |
507 ctxt_message_id = self.CONTEXT_ENCODING % (context, message) | |
508 missing = object() | |
509 tmsg = self._catalog.get(ctxt_message_id, missing) | |
510 if tmsg is missing: | |
511 if self._fallback: | |
512 return self._fallback.upgettext(context, message) | |
513 return unicode(message) | |
514 return tmsg | |
515 | |
516 def unpgettext(self, context, singular, plural, num): | |
517 """Do a plural-forms lookup of a message id. `singular` is used as the | |
518 message id for purposes of lookup in the catalog, while `num` is used to | |
519 determine which plural form to use. The returned message string is a | |
520 Unicode string. | |
521 | |
522 If the message id for `context` is not found in the catalog, and a | |
523 fallback is specified, the request is forwarded to the fallback's | |
524 ``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is | |
525 returned, and `plural` is returned in all other cases. | |
526 """ | |
527 ctxt_message_id = self.CONTEXT_ENCODING % (context, singular) | |
528 try: | |
529 tmsg = self._catalog[(ctxt_message_id, self.plural(num))] | |
530 except KeyError: | |
531 if self._fallback: | |
532 return self._fallback.unpgettext(context, singular, plural, num) | |
533 if num == 1: | |
534 tmsg = unicode(singular) | |
535 else: | |
536 tmsg = unicode(plural) | |
537 return tmsg | |
538 | |
539 def dpgettext(self, domain, context, message): | |
540 """Like `pgettext()`, but look the message up in the specified | |
541 `domain`. | |
542 """ | |
543 return self._domains.get(domain, self).pgettext(context, message) | |
544 | |
545 def dupgettext(self, domain, context, message): | |
546 """Like `upgettext()`, but look the message up in the specified | |
547 `domain`. | |
548 """ | |
549 return self._domains.get(domain, self).upgettext(context, message) | |
550 | |
551 def ldpgettext(self, domain, context, message): | |
552 """Equivalent to ``dpgettext()``, but the translation is returned in the | |
553 preferred system encoding, if no other encoding was explicitly set with | |
554 ``bind_textdomain_codeset()``. | |
555 """ | |
556 return self._domains.get(domain, self).lpgettext(context, message) | |
557 | |
558 def dnpgettext(self, domain, context, singular, plural, num): | |
559 """Like ``npgettext``, but look the message up in the specified | |
560 `domain`. | |
561 """ | |
562 return self._domains.get(domain, self).npgettext(context, singular, | |
563 plural, num) | |
564 | |
565 def dunpgettext(self, domain, context, singular, plural, num): | |
566 """Like ``unpgettext``, but look the message up in the specified | |
567 `domain`. | |
568 """ | |
569 return self._domains.get(domain, self).unpgettext(context, singular, | |
570 plural, num) | |
571 | |
572 def ldnpgettext(self, domain, context, singular, plural, num): | |
573 """Equivalent to ``dnpgettext()``, but the translation is returned in | |
574 the preferred system encoding, if no other encoding was explicitly set | |
575 with ``bind_textdomain_codeset()``. | |
576 """ | |
577 return self._domains.get(domain, self).lnpgettext(context, singular, | |
578 plural, num) | |
579 |