Mercurial > babel > old > babel-test
annotate babel/support.py @ 508:dbb70d75718a stable-0.9.x
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
author | fschwarz |
---|---|
date | Fri, 04 Mar 2011 14:16:15 +0000 |
parents | d1a27b087ea4 |
children |
rev | line source |
---|---|
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 -*- |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 # |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 # |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 # |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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/. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 """ |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
20 from datetime import date, datetime, time |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
22 |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
23 from babel.core import Locale |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
24 from babel.dates import format_date, format_datetime, format_time, LC_TIME |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
25 from babel.numbers import format_number, format_decimal, format_currency, \ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
26 format_percent, format_scientific, LC_NUMERIC |
508
dbb70d75718a
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
413
diff
changeset
|
27 from babel.util import set, UTC |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
28 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
29 __all__ = ['Format', 'LazyProxy', 'Translations'] |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
30 __docformat__ = 'restructuredtext en' |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
31 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
33 class Format(object): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
34 """Wrapper class providing the various date and number formatting functions |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
35 bound to a specific locale and time-zone. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
36 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
37 >>> fmt = Format('en_US', UTC) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
38 >>> fmt.date(date(2007, 4, 1)) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
39 u'Apr 1, 2007' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
40 >>> fmt.decimal(1.2345) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
41 u'1.234' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
42 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
43 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
44 def __init__(self, locale, tzinfo=None): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
45 """Initialize the formatter. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
46 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
47 :param locale: the locale identifier or `Locale` instance |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
48 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
49 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
50 self.locale = Locale.parse(locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
51 self.tzinfo = tzinfo |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
52 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
53 def date(self, date=None, format='medium'): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
54 """Return a date formatted according to the given pattern. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
55 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
56 >>> fmt = Format('en_US') |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
57 >>> fmt.date(date(2007, 4, 1)) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
58 u'Apr 1, 2007' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
59 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
60 :see: `babel.dates.format_date` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
61 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
62 return format_date(date, format, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
63 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
64 def datetime(self, datetime=None, format='medium'): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
65 """Return a date and time formatted according to the given pattern. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
66 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
67 >>> from pytz import timezone |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
68 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
69 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
70 u'Apr 1, 2007 11:30:00 AM' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
71 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
72 :see: `babel.dates.format_datetime` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
73 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
74 return format_datetime(datetime, format, tzinfo=self.tzinfo, |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
75 locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
76 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
77 def time(self, time=None, format='medium'): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
78 """Return a time formatted according to the given pattern. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
79 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
80 >>> from pytz import timezone |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
81 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
349 | 82 >>> fmt.time(datetime(2007, 4, 1, 15, 30)) |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
83 u'11:30:00 AM' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
84 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
85 :see: `babel.dates.format_time` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
86 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
87 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
88 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
89 def number(self, number): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
90 """Return an integer number formatted for the locale. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
91 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
92 >>> fmt = Format('en_US') |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
93 >>> fmt.number(1099) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
94 u'1,099' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
95 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
96 :see: `babel.numbers.format_number` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
97 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
98 return format_number(number, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
99 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
100 def decimal(self, number, format=None): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
101 """Return a decimal number formatted for the locale. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
102 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
103 >>> fmt = Format('en_US') |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
104 >>> fmt.decimal(1.2345) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
105 u'1.234' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
106 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
107 :see: `babel.numbers.format_decimal` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
108 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
109 return format_decimal(number, format, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
110 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
111 def currency(self, number, currency): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
112 """Return a number in the given currency formatted for the locale. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
113 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
114 :see: `babel.numbers.format_currency` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
115 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
116 return format_currency(number, currency, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
117 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
118 def percent(self, number, format=None): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
119 """Return a number formatted as percentage for the locale. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
120 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
121 >>> fmt = Format('en_US') |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
122 >>> fmt.percent(0.34) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
123 u'34%' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
124 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
125 :see: `babel.numbers.format_percent` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
126 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
127 return format_percent(number, format, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
128 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
129 def scientific(self, number): |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
130 """Return a number formatted using scientific notation for the locale. |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
131 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
132 :see: `babel.numbers.format_scientific` |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
133 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
134 return format_scientific(number, locale=self.locale) |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
135 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
136 |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
137 class LazyProxy(object): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
138 """Class for proxy objects that delegate to a specified function to evaluate |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
139 the actual object. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
140 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
141 >>> def greeting(name='world'): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
142 ... return 'Hello, %s!' % name |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
143 >>> lazy_greeting = LazyProxy(greeting, name='Joe') |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
144 >>> print lazy_greeting |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
145 Hello, Joe! |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
146 >>> u' ' + lazy_greeting |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
147 u' Hello, Joe!' |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
148 >>> u'(%s)' % lazy_greeting |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
149 u'(Hello, Joe!)' |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
150 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 This can be used, for example, to implement lazy translation functions that |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 delay the actual translation until the string is actually used. The |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 rationale for such behavior is that the locale of the user may not always |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 be available. In web applications, you only know the locale when processing |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 a request. |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 The proxy implementation attempts to be as complete as possible, so that |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 the lazy objects should mostly work as expected, for example for sorting: |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 >>> greetings = [ |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 ... LazyProxy(greeting, 'world'), |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 ... LazyProxy(greeting, 'Joe'), |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 ... LazyProxy(greeting, 'universe'), |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 ... ] |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 >>> greetings.sort() |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 >>> for greeting in greetings: |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 ... print greeting |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 Hello, Joe! |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 Hello, universe! |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 Hello, world! |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 """ |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 __slots__ = ['_func', '_args', '_kwargs', '_value'] |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __init__(self, func, *args, **kwargs): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 # Avoid triggering our own __setattr__ implementation |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 object.__setattr__(self, '_func', func) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 object.__setattr__(self, '_args', args) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 object.__setattr__(self, '_kwargs', kwargs) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 object.__setattr__(self, '_value', None) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def value(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 if self._value is None: |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 value = self._func(*self._args, **self._kwargs) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 object.__setattr__(self, '_value', value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return self._value |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 value = property(value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 __contains__(self, key): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return key in self.value |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __nonzero__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return bool(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __dir__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return dir(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __iter__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return iter(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __len__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return len(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __str__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
204 return str(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
206 def __unicode__(self): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
207 return unicode(self.value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
209 def __add__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
210 return self.value + other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
212 def __radd__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
213 return other + self.value |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
215 def __mod__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
216 return self.value % other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
218 def __rmod__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
219 return other % self.value |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
221 def __mul__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
222 return self.value * other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
224 def __rmul__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
225 return other * self.value |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
227 def __call__(self, *args, **kwargs): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
228 return self.value(*args, **kwargs) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
230 def __lt__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
231 return self.value < other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
233 def __le__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
234 return self.value <= other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
236 def __eq__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
237 return self.value == other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
239 def __ne__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
240 return self.value != other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
242 def __gt__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
243 return self.value > other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
245 def __ge__(self, other): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
246 return self.value >= other |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
248 def __delattr__(self, name): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 delattr(self.value, name) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
251 def __getattr__(self, name): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
252 return getattr(self.value, name) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
100 | 254 def __setattr__(self, name, value): |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 setattr(self.value, name, value) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
257 def __delitem__(self, key): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 del self.value[key] |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
260 def __getitem__(self, key): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
261 return self.value[key] |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
263 def __setitem__(self, key, value): |
100 | 264 self.value[key] = value |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
407 | 266 |
267 class Translations(gettext.GNUTranslations, object): | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
268 """An extended translation catalog class.""" |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 DEFAULT_DOMAIN = 'messages' |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
413 | 272 def __init__(self, fileobj=None, domain=DEFAULT_DOMAIN): |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 """Initialize the translations catalog. |
413 | 274 |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 :param fileobj: the file-like object the translation should be read |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 from |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 """ |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
278 gettext.GNUTranslations.__init__(self, fp=fileobj) |
346
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
279 self.files = filter(None, [getattr(fileobj, 'name', None)]) |
413 | 280 self.domain = domain |
281 self._domains = {} | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 """Load translations from the given directory. |
413 | 285 |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
286 :param dirname: the directory containing the ``MO`` files |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 :param locales: the list of locales in order of preference (items in |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
288 this list can be either `Locale` objects or locale |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 strings) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 :param domain: the message domain |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 :return: the loaded catalog, or a ``NullTranslations`` instance if no |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 matching translations were found |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
293 :rtype: `Translations` |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
294 """ |
346
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
295 if locales is not None: |
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
296 if not isinstance(locales, (list, tuple)): |
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
297 locales = [locales] |
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
298 locales = [str(locale) for locale in locales] |
413 | 299 if not domain: |
300 domain = cls.DEFAULT_DOMAIN | |
301 filename = gettext.find(domain, dirname, locales) | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 if not filename: |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 return gettext.NullTranslations() |
413 | 304 return cls(fileobj=open(filename, 'rb'), domain=domain) |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 load = classmethod(load) |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, 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 |
413 | 307 def __repr__(self): |
308 return '<%s: "%s">' % (type(self).__name__, | |
309 self._info.get('project-id-version')) | |
310 | |
311 def add(self, translations, merge=True): | |
312 """Add the given translations to the catalog. | |
313 | |
314 If the domain of the translations is different than that of the | |
315 current catalog, they are added as a catalog that is only accessible | |
316 by the various ``d*gettext`` functions. | |
317 | |
318 :param translations: the `Translations` instance with the messages to | |
319 add | |
320 :param merge: whether translations for message domains that have | |
321 already been added should be merged with the existing | |
322 translations | |
323 :return: the `Translations` instance (``self``) so that `merge` calls | |
324 can be easily chained | |
325 :rtype: `Translations` | |
326 """ | |
327 domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN) | |
328 if merge and domain == self.domain: | |
329 return self.merge(translations) | |
330 | |
331 existing = self._domains.get(domain) | |
332 if merge and existing is not None: | |
333 existing.merge(translations) | |
334 else: | |
335 translations.add_fallback(self) | |
336 self._domains[domain] = translations | |
337 | |
338 return self | |
339 | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
340 def merge(self, translations): |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
341 """Merge the given translations into the catalog. |
413 | 342 |
343 Message translations in the specified catalog override any messages | |
344 with the same identifier in the existing catalog. | |
345 | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
346 :param translations: the `Translations` instance with the messages to |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
347 merge |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
348 :return: the `Translations` instance (``self``) so that `merge` calls |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
349 can be easily chained |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
350 :rtype: `Translations` |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
351 """ |
413 | 352 if isinstance(translations, gettext.GNUTranslations): |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
353 self._catalog.update(translations._catalog) |
413 | 354 if isinstance(translations, Translations): |
355 self.files.extend(translations.files) | |
356 | |
61
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
357 return self |
da7efa40a9e2
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
358 |
413 | 359 def dgettext(self, domain, message): |
360 """Like ``gettext()``, but look the message up in the specified | |
361 domain. | |
362 """ | |
363 return self._domains.get(domain, self).gettext(message) | |
364 | |
365 def ldgettext(self, domain, message): | |
366 """Like ``lgettext()``, but look the message up in the specified | |
367 domain. | |
368 """ | |
369 return self._domains.get(domain, self).lgettext(message) | |
370 | |
371 def dugettext(self, domain, message): | |
372 """Like ``ugettext()``, but look the message up in the specified | |
373 domain. | |
374 """ | |
375 return self._domains.get(domain, self).ugettext(message) | |
376 | |
377 def dngettext(self, domain, singular, plural, num): | |
378 """Like ``ngettext()``, but look the message up in the specified | |
379 domain. | |
380 """ | |
381 return self._domains.get(domain, self).ngettext(singular, plural, num) | |
382 | |
383 def ldngettext(self, domain, singular, plural, num): | |
384 """Like ``lngettext()``, but look the message up in the specified | |
385 domain. | |
386 """ | |
387 return self._domains.get(domain, self).lngettext(singular, plural, num) | |
388 | |
389 def dungettext(self, domain, singular, plural, num): | |
390 """Like ``ungettext()`` but look the message up in the specified | |
391 domain. | |
392 """ | |
393 return self._domains.get(domain, self).ungettext(singular, plural, num) |