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