annotate babel/support.py @ 404:7a3ab3e0dcb0

Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
author palgarvio
date Tue, 05 Aug 2008 23:30:50 +0000
parents 3a0cea7b8050
children dc5b9feeab62
rev   line source
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
2 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
4 # All rights reserved.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
5 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
9 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
13
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
14 """Several classes and functions that help with integrating and using Babel
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
15 in applications.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
16
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
17 .. note: the code in this module is not used by Babel itself
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
18 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
19
403
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
20 from datetime import date, datetime, time, timedelta
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
21 import gettext
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
22
353
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
23 try:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
24 set
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
25 except NameError:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
26 from sets import set
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
27
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
28 from babel.core import Locale
403
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
29 from babel.dates import format_date, format_datetime, format_time, \
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
30 format_timedelta, LC_TIME
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
31 from babel.numbers import format_number, format_decimal, format_currency, \
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
32 format_percent, format_scientific, LC_NUMERIC
354
249aab27c4b3 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 353
diff changeset
33 from babel.util import UTC
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
34
354
249aab27c4b3 The builtin checkers don't require setuptools any longer, validate_format and python_format from the checkers module are merged into one now.
aronacher
parents: 353
diff changeset
35 __all__ = ['Format', 'LazyProxy', 'Translations']
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
36 __docformat__ = 'restructuredtext en'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
37
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
38
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
39 class Format(object):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
40 """Wrapper class providing the various date and number formatting functions
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
41 bound to a specific locale and time-zone.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
42
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
43 >>> fmt = Format('en_US', UTC)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
44 >>> fmt.date(date(2007, 4, 1))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
45 u'Apr 1, 2007'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
46 >>> fmt.decimal(1.2345)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
47 u'1.234'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
48 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
49
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
50 def __init__(self, locale, tzinfo=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
51 """Initialize the formatter.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
52
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
53 :param locale: the locale identifier or `Locale` instance
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
54 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
55 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
56 self.locale = Locale.parse(locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
57 self.tzinfo = tzinfo
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
58
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
59 def date(self, date=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
60 """Return a date formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
61
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
62 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
63 >>> fmt.date(date(2007, 4, 1))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
64 u'Apr 1, 2007'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
65
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
66 :see: `babel.dates.format_date`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
67 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
68 return format_date(date, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
69
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
70 def datetime(self, datetime=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
71 """Return a date and time formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
72
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
73 >>> from pytz import timezone
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
74 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
75 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
76 u'Apr 1, 2007 11:30:00 AM'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
77
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
78 :see: `babel.dates.format_datetime`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
79 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
80 return format_datetime(datetime, format, tzinfo=self.tzinfo,
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
81 locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
82
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
83 def time(self, time=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
84 """Return a time formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
85
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
86 >>> from pytz import timezone
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
87 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
350
e5db561bf70e Fixes for timezone calculations in time formatting (#83).
cmlenz
parents: 338
diff changeset
88 >>> fmt.time(datetime(2007, 4, 1, 15, 30))
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
89 u'11:30:00 AM'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
90
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
91 :see: `babel.dates.format_time`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
92 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
93 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
94
403
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
95 def timedelta(self, delta, granularity='second', threshold=.85):
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
96 """Return a time delta according to the rules of the given locale.
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
97
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
98 >>> fmt = Format('en_US')
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
99 >>> fmt.timedelta(timedelta(weeks=11))
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
100 u'3 months'
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
101
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
102 :see: `babel.dates.format_timedelta`
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
103 """
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
104 return format_timedelta(delta, granularity=granularity,
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
105 threshold=threshold, locale=self.locale)
3a0cea7b8050 Add `timedelta` function to the `Format` support class.
cmlenz
parents: 354
diff changeset
106
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
107 def number(self, number):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
108 """Return an integer number formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
109
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
110 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
111 >>> fmt.number(1099)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
112 u'1,099'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
113
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
114 :see: `babel.numbers.format_number`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
115 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
116 return format_number(number, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
117
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
118 def decimal(self, number, format=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
119 """Return a decimal number formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
120
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
121 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
122 >>> fmt.decimal(1.2345)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
123 u'1.234'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
124
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
125 :see: `babel.numbers.format_decimal`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
126 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
127 return format_decimal(number, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
128
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
129 def currency(self, number, currency):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
130 """Return a number in the given currency formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
131
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
132 :see: `babel.numbers.format_currency`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
133 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
134 return format_currency(number, currency, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
135
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
136 def percent(self, number, format=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
137 """Return a number formatted as percentage for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
138
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
139 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
140 >>> fmt.percent(0.34)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
141 u'34%'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
142
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
143 :see: `babel.numbers.format_percent`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
144 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
145 return format_percent(number, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
146
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
147 def scientific(self, number):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
148 """Return a number formatted using scientific notation for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
149
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
150 :see: `babel.numbers.format_scientific`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
151 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
152 return format_scientific(number, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
153
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
154
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
155 class LazyProxy(object):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
156 """Class for proxy objects that delegate to a specified function to evaluate
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
157 the actual object.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
158
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
159 >>> def greeting(name='world'):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
160 ... return 'Hello, %s!' % name
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
161 >>> lazy_greeting = LazyProxy(greeting, name='Joe')
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
162 >>> print lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
163 Hello, Joe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
164 >>> u' ' + lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
165 u' Hello, Joe!'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
166 >>> u'(%s)' % lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
167 u'(Hello, Joe!)'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
168
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
169 This can be used, for example, to implement lazy translation functions that
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
170 delay the actual translation until the string is actually used. The
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
171 rationale for such behavior is that the locale of the user may not always
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
172 be available. In web applications, you only know the locale when processing
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
173 a request.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
174
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
175 The proxy implementation attempts to be as complete as possible, so that
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
176 the lazy objects should mostly work as expected, for example for sorting:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
177
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
178 >>> greetings = [
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
179 ... LazyProxy(greeting, 'world'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
180 ... LazyProxy(greeting, 'Joe'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
181 ... LazyProxy(greeting, 'universe'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
182 ... ]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
183 >>> greetings.sort()
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
184 >>> for greeting in greetings:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
185 ... print greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
186 Hello, Joe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
187 Hello, universe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
188 Hello, world!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
189 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
190 __slots__ = ['_func', '_args', '_kwargs', '_value']
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
191
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
192 def __init__(self, func, *args, **kwargs):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
193 # Avoid triggering our own __setattr__ implementation
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
194 object.__setattr__(self, '_func', func)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
195 object.__setattr__(self, '_args', args)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
196 object.__setattr__(self, '_kwargs', kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
197 object.__setattr__(self, '_value', None)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
198
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
199 def value(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
200 if self._value is None:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
201 value = self._func(*self._args, **self._kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
202 object.__setattr__(self, '_value', value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
203 return self._value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
204 value = property(value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
205
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
206 def __contains__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
207 return key in self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
208
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
209 def __nonzero__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
210 return bool(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
211
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
212 def __dir__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
213 return dir(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
214
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
215 def __iter__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
216 return iter(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
217
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
218 def __len__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
219 return len(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
220
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
221 def __str__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
222 return str(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
223
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
224 def __unicode__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
225 return unicode(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
226
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
227 def __add__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
228 return self.value + other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
229
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
230 def __radd__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
231 return other + self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
232
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
233 def __mod__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
234 return self.value % other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
235
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
236 def __rmod__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
237 return other % self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
238
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
239 def __mul__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
240 return self.value * other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
241
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
242 def __rmul__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
243 return other * self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
244
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
245 def __call__(self, *args, **kwargs):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
246 return self.value(*args, **kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
247
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
248 def __lt__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
249 return self.value < other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
250
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
251 def __le__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
252 return self.value <= other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
253
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
254 def __eq__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
255 return self.value == other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
256
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
257 def __ne__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
258 return self.value != other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
259
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
260 def __gt__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
261 return self.value > other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
262
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
263 def __ge__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
264 return self.value >= other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
265
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
266 def __delattr__(self, name):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
267 delattr(self.value, name)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
268
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
269 def __getattr__(self, name):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
270 return getattr(self.value, name)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
271
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
272 def __setattr__(self, name, value):
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
273 setattr(self.value, name, value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
274
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
275 def __delitem__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
276 del self.value[key]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
277
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
278 def __getitem__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
279 return self.value[key]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
280
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
281 def __setitem__(self, key, value):
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
282 self.value[key] = value
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
283
404
7a3ab3e0dcb0 Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents: 403
diff changeset
284
7a3ab3e0dcb0 Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents: 403
diff changeset
285 class Translations(gettext.GNUTranslations, object):
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
286 """An extended translation catalog class."""
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
287
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
288 DEFAULT_DOMAIN = 'messages'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
289
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
290 def __init__(self, fileobj=None):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
291 """Initialize the translations catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
292
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
293 :param fileobj: the file-like object the translation should be read
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
294 from
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
295 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
296 gettext.GNUTranslations.__init__(self, fp=fileobj)
338
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
297 self.files = filter(None, [getattr(fileobj, 'name', None)])
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
298
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
299 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
300 """Load translations from the given directory.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
301
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
302 :param dirname: the directory containing the ``MO`` files
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
303 :param locales: the list of locales in order of preference (items in
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
304 this list can be either `Locale` objects or locale
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
305 strings)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
306 :param domain: the message domain
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
307 :return: the loaded catalog, or a ``NullTranslations`` instance if no
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
308 matching translations were found
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
309 :rtype: `Translations`
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
310 """
338
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
311 if locales is not None:
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
312 if not isinstance(locales, (list, tuple)):
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
313 locales = [locales]
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
314 locales = [str(locale) for locale in locales]
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
315 filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales)
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
316 if not filename:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
317 return gettext.NullTranslations()
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
318 return cls(fileobj=open(filename, 'rb'))
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
319 load = classmethod(load)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
320
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
321 def merge(self, translations):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
322 """Merge the given translations into the catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
323
404
7a3ab3e0dcb0 Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents: 403
diff changeset
324 Message translations in the specified catalog override any messages with
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
325 the same identifier in the existing catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
326
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
327 :param translations: the `Translations` instance with the messages to
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
328 merge
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
329 :return: the `Translations` instance (``self``) so that `merge` calls
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
330 can be easily chained
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
331 :rtype: `Translations`
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
332 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
333 if isinstance(translations, Translations):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
334 self._catalog.update(translations._catalog)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
335 self.files.extend(translations.files)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
336 return self
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
337
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
338 def __repr__(self):
404
7a3ab3e0dcb0 Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents: 403
diff changeset
339 return '<%s: "%s">' % (type(self).__name__,
7a3ab3e0dcb0 Fix `babel.support.Translations.__repr__` and make it less stupid; Include the project name and version defined on the catalog loaded.
palgarvio
parents: 403
diff changeset
340 self._info.get('project-id-version'))
Copyright (C) 2012-2017 Edgewall Software