Mercurial > babel > mirror
annotate babel/support.py @ 284:0ee9dc258c2d stable-0.9.x
Ported [311] to 0.9.x branch.
author | cmlenz |
---|---|
date | Wed, 05 Sep 2007 17:18:10 +0000 |
parents | 0f641136aa6b |
children | d6d91f1fb5df |
rev | line source |
---|---|
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
2 # |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
5 # |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://babel.edgewall.org/wiki/License. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
9 # |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://babel.edgewall.org/log/. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
13 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
14 """Several classes and functions that help with integrating and using Babel |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
15 in applications. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
16 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
17 .. note: the code in this module is not used by Babel itself |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
18 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
19 |
101
0f641136aa6b
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
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
21 import gettext |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
22 |
101
0f641136aa6b
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 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
24 from babel.dates import format_date, format_datetime, format_time, LC_TIME |
0f641136aa6b
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, \ |
0f641136aa6b
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 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
27 from babel.util import UTC |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
28 |
0f641136aa6b
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
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
30 __docformat__ = 'restructuredtext en' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
31 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
32 |
101
0f641136aa6b
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): |
0f641136aa6b
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 |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
36 |
0f641136aa6b
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) |
0f641136aa6b
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)) |
0f641136aa6b
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' |
0f641136aa6b
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) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
42 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
43 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
46 |
0f641136aa6b
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 |
0f641136aa6b
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`) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
49 """ |
0f641136aa6b
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) |
0f641136aa6b
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 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
52 |
0f641136aa6b
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'): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
55 |
0f641136aa6b
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') |
0f641136aa6b
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)) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
59 |
0f641136aa6b
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` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
61 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
62 return format_date(date, format, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
63 |
0f641136aa6b
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'): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
66 |
0f641136aa6b
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 |
0f641136aa6b
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')) |
0f641136aa6b
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)) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
71 |
0f641136aa6b
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` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
73 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
74 return format_datetime(datetime, format, tzinfo=self.tzinfo, |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
75 locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
76 |
0f641136aa6b
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'): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
79 |
0f641136aa6b
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 |
0f641136aa6b
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')) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
82 >>> fmt.time(time(15, 30)) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
84 |
0f641136aa6b
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` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
86 """ |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
87 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
88 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
91 |
0f641136aa6b
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') |
0f641136aa6b
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) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
95 |
0f641136aa6b
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` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
97 """ |
0f641136aa6b
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) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
99 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
102 |
0f641136aa6b
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') |
0f641136aa6b
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) |
0f641136aa6b
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' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
106 |
0f641136aa6b
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` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
108 """ |
0f641136aa6b
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) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
110 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
113 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
114 :see: `babel.numbers.format_currency` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
115 """ |
0f641136aa6b
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) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
117 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
120 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
121 >>> fmt = Format('en_US') |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
122 >>> fmt.percent(0.34) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
123 u'34%' |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
124 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
125 :see: `babel.numbers.format_percent` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
126 """ |
0f641136aa6b
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) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
128 |
0f641136aa6b
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): |
0f641136aa6b
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. |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
131 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
132 :see: `babel.numbers.format_scientific` |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
133 """ |
0f641136aa6b
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) |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
135 |
0f641136aa6b
Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents:
100
diff
changeset
|
136 |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
137 class LazyProxy(object): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
138 """Class for proxy objects that delegate to a specified function to evaluate |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
139 the actual object. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
140 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
141 >>> def greeting(name='world'): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
142 ... return 'Hello, %s!' % name |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
143 >>> lazy_greeting = LazyProxy(greeting, name='Joe') |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
144 >>> print lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
145 Hello, Joe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
146 >>> u' ' + lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
147 u' Hello, Joe!' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
148 >>> u'(%s)' % lazy_greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
149 u'(Hello, Joe!)' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
150 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
151 This can be used, for example, to implement lazy translation functions that |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
152 delay the actual translation until the string is actually used. The |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
153 rationale for such behavior is that the locale of the user may not always |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
154 be available. In web applications, you only know the locale when processing |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
155 a request. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
156 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
157 The proxy implementation attempts to be as complete as possible, so that |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
158 the lazy objects should mostly work as expected, for example for sorting: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
159 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
160 >>> greetings = [ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
161 ... LazyProxy(greeting, 'world'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
162 ... LazyProxy(greeting, 'Joe'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
163 ... LazyProxy(greeting, 'universe'), |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
164 ... ] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
165 >>> greetings.sort() |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
166 >>> for greeting in greetings: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
167 ... print greeting |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
168 Hello, Joe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
169 Hello, universe! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
170 Hello, world! |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
171 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
172 __slots__ = ['_func', '_args', '_kwargs', '_value'] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
173 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
174 def __init__(self, func, *args, **kwargs): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
175 # Avoid triggering our own __setattr__ implementation |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
176 object.__setattr__(self, '_func', func) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
177 object.__setattr__(self, '_args', args) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
178 object.__setattr__(self, '_kwargs', kwargs) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
179 object.__setattr__(self, '_value', None) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
180 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
181 def value(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
182 if self._value is None: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
183 value = self._func(*self._args, **self._kwargs) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
184 object.__setattr__(self, '_value', value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
185 return self._value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
186 value = property(value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
187 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
188 def __contains__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
189 return key in self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
190 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
191 def __nonzero__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
192 return bool(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
193 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
194 def __dir__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
195 return dir(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
196 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
197 def __iter__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
198 return iter(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
199 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
200 def __len__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
201 return len(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
202 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
203 def __str__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
204 return str(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
205 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
206 def __unicode__(self): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
207 return unicode(self.value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
208 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
209 def __add__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
210 return self.value + other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
211 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
212 def __radd__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
213 return other + self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
214 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
215 def __mod__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
216 return self.value % other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
217 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
218 def __rmod__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
219 return other % self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
220 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
221 def __mul__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
222 return self.value * other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
223 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
224 def __rmul__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
225 return other * self.value |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
226 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
227 def __call__(self, *args, **kwargs): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
228 return self.value(*args, **kwargs) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
229 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
230 def __lt__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
231 return self.value < other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
232 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
233 def __le__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
234 return self.value <= other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
235 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
236 def __eq__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
237 return self.value == other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
238 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
239 def __ne__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
240 return self.value != other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
241 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
242 def __gt__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
243 return self.value > other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
244 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
245 def __ge__(self, other): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
246 return self.value >= other |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
247 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
248 def __delattr__(self, name): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
249 delattr(self.value, name) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
250 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
251 def __getattr__(self, name): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
252 return getattr(self.value, name) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
253 |
100 | 254 def __setattr__(self, name, value): |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
255 setattr(self.value, name, value) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
256 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
257 def __delitem__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
258 del self.value[key] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
259 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
260 def __getitem__(self, key): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
261 return self.value[key] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
262 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
263 def __setitem__(self, key, value): |
100 | 264 self.value[key] = value |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
265 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
266 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
267 class Translations(gettext.GNUTranslations): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
268 """An extended translation catalog class.""" |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
269 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
270 DEFAULT_DOMAIN = 'messages' |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
271 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
272 def __init__(self, fileobj=None): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
273 """Initialize the translations catalog. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
274 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
275 :param fileobj: the file-like object the translation should be read |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
276 from |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
277 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
278 gettext.GNUTranslations.__init__(self, fp=fileobj) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
279 self.files = [getattr(fileobj, 'name')] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
280 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
281 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
282 """Load translations from the given directory. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
283 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
284 :param dirname: the directory containing the ``MO`` files |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
285 :param locales: the list of locales in order of preference (items in |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
286 this list can be either `Locale` objects or locale |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
287 strings) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
288 :param domain: the message domain |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
289 :return: the loaded catalog, or a ``NullTranslations`` instance if no |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
290 matching translations were found |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
291 :rtype: `Translations` |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
292 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
293 if not isinstance(locales, (list, tuple)): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
294 locales = [locales] |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
295 locales = [str(locale) for locale in locales] |
100 | 296 filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales) |
61
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
297 if not filename: |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
298 return gettext.NullTranslations() |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
299 return cls(fileobj=open(filename, 'rb')) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
300 load = classmethod(load) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
301 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
302 def merge(self, translations): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
303 """Merge the given translations into the catalog. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
304 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
305 Message translations in the specfied catalog override any messages with |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
306 the same identifier in the existing catalog. |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
307 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
308 :param translations: the `Translations` instance with the messages to |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
309 merge |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
310 :return: the `Translations` instance (``self``) so that `merge` calls |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
311 can be easily chained |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
312 :rtype: `Translations` |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
313 """ |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
314 if isinstance(translations, Translations): |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
315 self._catalog.update(translations._catalog) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
316 self.files.extend(translations.files) |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
317 return self |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
318 |
9d13b9a5d727
Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff
changeset
|
319 def __repr__(self): |
284 | 320 return "<%s>" % (type(self).__name__) |