Mercurial > babel > old > babel-test
annotate babel/support.py @ 480:863bb2ae4adb stable-0.9.x
Block more msgctxt related work.
author | jruigrok |
---|---|
date | Sun, 11 Apr 2010 08:50:34 +0000 |
parents | d1a27b087ea4 |
children | dbb70d75718a |
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 |
369 | 23 try: |
24 set | |
25 except NameError: | |
26 from sets import set | |
27 | |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 format_percent, format_scientific, LC_NUMERIC |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
32 from babel.util import UTC |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
33 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
34 __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
|
35 __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
|
36 |
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
|
37 |
101
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
38 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
|
39 """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
|
40 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
|
41 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
42 >>> 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
|
43 >>> 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
|
44 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
|
45 >>> 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
|
46 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
|
47 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
48 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
49 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
|
50 """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
|
51 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
52 :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
|
53 :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
|
54 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
55 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
|
56 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
|
57 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
58 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
|
59 """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
|
60 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
61 >>> 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
|
62 >>> 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
|
63 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
|
64 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
65 :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
|
66 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
67 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
|
68 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
69 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
|
70 """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
|
71 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
72 >>> 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
|
73 >>> 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
|
74 >>> 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
|
75 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
|
76 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
77 :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
|
78 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
79 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
|
80 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
|
81 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
82 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
|
83 """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
|
84 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
85 >>> 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
|
86 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) |
349 | 87 >>> 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
|
88 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
|
89 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
90 :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
|
91 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
92 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
|
93 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
94 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
|
95 """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
|
96 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
97 >>> 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
|
98 >>> 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
|
99 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
|
100 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
101 :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
|
102 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
103 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
|
104 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
105 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
|
106 """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
|
107 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
108 >>> 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
|
109 >>> 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
|
110 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
|
111 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
112 :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
|
113 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
114 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
|
115 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
116 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
|
117 """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
|
118 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
119 :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
|
120 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
121 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
|
122 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
123 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
|
124 """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
|
125 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
126 >>> 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
|
127 >>> 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
|
128 u'34%' |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
129 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
130 :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
|
131 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
132 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
|
133 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
134 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
|
135 """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
|
136 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
137 :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
|
138 """ |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
139 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
|
140 |
1312ad6b624d
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
141 |
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
|
142 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
|
143 """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
|
144 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
|
145 |
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 >>> 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
|
147 ... 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
|
148 >>> 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
|
149 >>> 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
|
150 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
|
151 >>> 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
|
152 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
|
153 >>> 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
|
154 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
|
155 |
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 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 |
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 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
|
163 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
|
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 = [ |
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 ... 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
|
167 ... 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
|
168 ... 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
|
169 ... ] |
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 >>> 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
|
171 >>> 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
|
172 ... 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
|
173 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
|
174 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
|
175 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
|
176 """ |
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 __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
|
178 |
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 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
|
180 # 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 |
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 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 |
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 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
|
194 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
|
195 |
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 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
|
197 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
|
198 |
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 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
|
200 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
|
201 |
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 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
|
203 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
|
204 |
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 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
|
206 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
|
207 |
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 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
|
209 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
|
210 |
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 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
|
212 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
|
213 |
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 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
|
215 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
|
216 |
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 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
|
218 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
|
219 |
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 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
|
221 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
|
222 |
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 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
|
224 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
|
225 |
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 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
|
227 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
|
228 |
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 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
|
230 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
|
231 |
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 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
|
233 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
|
234 |
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 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
|
236 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
|
237 |
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 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
|
239 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
|
240 |
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 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
|
242 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
|
243 |
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 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
|
245 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
|
246 |
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 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
|
248 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
|
249 |
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 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
|
251 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
|
252 |
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 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
|
254 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
|
255 |
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 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
|
257 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
|
258 |
100 | 259 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
|
260 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
|
261 |
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 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
|
263 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
|
264 |
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 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
|
266 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
|
267 |
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 def __setitem__(self, key, value): |
100 | 269 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
|
270 |
407 | 271 |
272 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
|
273 """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
|
274 |
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 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
|
276 |
413 | 277 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
|
278 """Initialize the translations catalog. |
413 | 279 |
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
|
280 :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
|
281 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
|
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 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
|
284 self.files = filter(None, [getattr(fileobj, 'name', None)]) |
413 | 285 self.domain = domain |
286 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
|
287 |
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 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
|
289 """Load translations from the given directory. |
413 | 290 |
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
|
291 :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
|
292 :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
|
293 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
|
294 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
|
295 :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
|
296 :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
|
297 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
|
298 :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
|
299 """ |
346
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
300 if locales is not None: |
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
301 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
|
302 locales = [locales] |
faf0ead3a132
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
284
diff
changeset
|
303 locales = [str(locale) for locale in locales] |
413 | 304 if not domain: |
305 domain = cls.DEFAULT_DOMAIN | |
306 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
|
307 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
|
308 return gettext.NullTranslations() |
413 | 309 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
|
310 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
|
311 |
413 | 312 def __repr__(self): |
313 return '<%s: "%s">' % (type(self).__name__, | |
314 self._info.get('project-id-version')) | |
315 | |
316 def add(self, translations, merge=True): | |
317 """Add the given translations to the catalog. | |
318 | |
319 If the domain of the translations is different than that of the | |
320 current catalog, they are added as a catalog that is only accessible | |
321 by the various ``d*gettext`` functions. | |
322 | |
323 :param translations: the `Translations` instance with the messages to | |
324 add | |
325 :param merge: whether translations for message domains that have | |
326 already been added should be merged with the existing | |
327 translations | |
328 :return: the `Translations` instance (``self``) so that `merge` calls | |
329 can be easily chained | |
330 :rtype: `Translations` | |
331 """ | |
332 domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN) | |
333 if merge and domain == self.domain: | |
334 return self.merge(translations) | |
335 | |
336 existing = self._domains.get(domain) | |
337 if merge and existing is not None: | |
338 existing.merge(translations) | |
339 else: | |
340 translations.add_fallback(self) | |
341 self._domains[domain] = translations | |
342 | |
343 return self | |
344 | |
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
|
345 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
|
346 """Merge the given translations into the catalog. |
413 | 347 |
348 Message translations in the specified catalog override any messages | |
349 with the same identifier in the existing catalog. | |
350 | |
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
|
351 :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
|
352 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
|
353 :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
|
354 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
|
355 :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
|
356 """ |
413 | 357 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
|
358 self._catalog.update(translations._catalog) |
413 | 359 if isinstance(translations, Translations): |
360 self.files.extend(translations.files) | |
361 | |
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
|
362 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
|
363 |
413 | 364 def dgettext(self, domain, message): |
365 """Like ``gettext()``, but look the message up in the specified | |
366 domain. | |
367 """ | |
368 return self._domains.get(domain, self).gettext(message) | |
369 | |
370 def ldgettext(self, domain, message): | |
371 """Like ``lgettext()``, but look the message up in the specified | |
372 domain. | |
373 """ | |
374 return self._domains.get(domain, self).lgettext(message) | |
375 | |
376 def dugettext(self, domain, message): | |
377 """Like ``ugettext()``, but look the message up in the specified | |
378 domain. | |
379 """ | |
380 return self._domains.get(domain, self).ugettext(message) | |
381 | |
382 def dngettext(self, domain, singular, plural, num): | |
383 """Like ``ngettext()``, but look the message up in the specified | |
384 domain. | |
385 """ | |
386 return self._domains.get(domain, self).ngettext(singular, plural, num) | |
387 | |
388 def ldngettext(self, domain, singular, plural, num): | |
389 """Like ``lngettext()``, but look the message up in the specified | |
390 domain. | |
391 """ | |
392 return self._domains.get(domain, self).lngettext(singular, plural, num) | |
393 | |
394 def dungettext(self, domain, singular, plural, num): | |
395 """Like ``ungettext()`` but look the message up in the specified | |
396 domain. | |
397 """ | |
398 return self._domains.get(domain, self).ungettext(singular, plural, num) |