annotate babel/support.py @ 530:85e1beadacb0

Update the copyright line.
author jruigrok
date Sat, 05 Mar 2011 15:22:28 +0000
parents 918569a5a855
children
rev   line source
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
2 #
530
85e1beadacb0 Update the copyright line.
jruigrok
parents: 522
diff changeset
3 # Copyright (C) 2007-2011 Edgewall Software
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
4 # All rights reserved.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
5 #
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
9 #
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
13
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
14 """Several classes and functions that help with integrating and using Babel
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
15 in applications.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
16
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
17 .. note: the code in this module is not used by Babel itself
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
18 """
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
19
522
918569a5a855 Get rid of some unused imports.
jruigrok
parents: 430
diff changeset
20 from datetime import date, datetime, timedelta
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
21 import gettext
420
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
22 import locale
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
23
101
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
24 from babel.core import Locale
401
7826d8e9998f 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
918569a5a855 Get rid of some unused imports.
jruigrok
parents: 430
diff changeset
26 format_timedelta
101
1312ad6b624d 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
918569a5a855 Get rid of some unused imports.
jruigrok
parents: 430
diff changeset
28 format_percent, format_scientific
918569a5a855 Get rid of some unused imports.
jruigrok
parents: 430
diff changeset
29 from babel.util import UTC
101
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
30
352
20d10066a42a 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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'
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
1312ad6b624d 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):
1312ad6b624d 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
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
38
1312ad6b624d 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)
1312ad6b624d 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))
1312ad6b624d 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'
1312ad6b624d 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)
1312ad6b624d 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'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
44 """
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
45
1312ad6b624d 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):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
48
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
49 :param locale: the locale identifier or `Locale` instance
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
50 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
51 """
1312ad6b624d 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)
1312ad6b624d 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
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
54
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
55 def date(self, date=None, format='medium'):
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
56 """Return a date formatted according to the given pattern.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
57
1312ad6b624d 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')
1312ad6b624d 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))
1312ad6b624d 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'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
61
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
62 :see: `babel.dates.format_date`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
63 """
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
65
1312ad6b624d 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'):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
68
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
69 >>> from pytz import timezone
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
70 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
71 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
72 u'Apr 1, 2007 11:30:00 AM'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
73
1312ad6b624d 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`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
75 """
1312ad6b624d 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,
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
78
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
79 def time(self, time=None, format='medium'):
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
80 """Return a time formatted according to the given pattern.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
81
1312ad6b624d 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
1312ad6b624d 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
6482dc682455 Fixes for timezone calculations in time formatting (#83).
cmlenz
parents: 336
diff changeset
84 >>> fmt.time(datetime(2007, 4, 1, 15, 30))
101
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
85 u'11:30:00 AM'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
86
1312ad6b624d 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`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
88 """
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
89 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
90
401
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
91 def timedelta(self, delta, granularity='second', threshold=.85):
7826d8e9998f 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.
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
93
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
94 >>> fmt = Format('en_US')
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
95 >>> fmt.timedelta(timedelta(weeks=11))
430
c92473b51411 First changes to accomodate CLDR 1.7's changes.
jruigrok
parents: 420
diff changeset
96 u'3 mths'
401
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
97
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
98 :see: `babel.dates.format_timedelta`
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
99 """
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
100 return format_timedelta(delta, granularity=granularity,
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
101 threshold=threshold, locale=self.locale)
7826d8e9998f Add `timedelta` function to the `Format` support class.
cmlenz
parents: 352
diff changeset
102
101
1312ad6b624d 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):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
105
1312ad6b624d 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')
1312ad6b624d 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)
1312ad6b624d 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'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
109
1312ad6b624d 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`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
111 """
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
112 return format_number(number, locale=self.locale)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
113
1312ad6b624d 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):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
116
1312ad6b624d 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')
1312ad6b624d 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)
1312ad6b624d 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'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
120
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
121 :see: `babel.numbers.format_decimal`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
122 """
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
124
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
125 def currency(self, number, currency):
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
126 """Return a number in the given currency formatted for the locale.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
127
1312ad6b624d 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`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
129 """
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
130 return format_currency(number, currency, locale=self.locale)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
131
1312ad6b624d 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):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
134
1312ad6b624d 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')
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
137 u'34%'
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
138
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
139 :see: `babel.numbers.format_percent`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
140 """
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
142
1312ad6b624d 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):
1312ad6b624d 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.
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
145
1312ad6b624d 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`
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
147 """
1312ad6b624d 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)
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
149
1312ad6b624d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 100
diff changeset
150
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
151 class LazyProxy(object):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
152 """Class for proxy objects that delegate to a specified function to evaluate
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
153 the actual object.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
154
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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'):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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')
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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!
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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!'
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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!)'
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
164
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
165 This can be used, for example, to implement lazy translation functions that
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
166 delay the actual translation until the string is actually used. The
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
167 rationale for such behavior is that the locale of the user may not always
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
168 be available. In web applications, you only know the locale when processing
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
169 a request.
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
170
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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:
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
173
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
174 >>> greetings = [
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
175 ... LazyProxy(greeting, 'world'),
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
176 ... LazyProxy(greeting, 'Joe'),
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
177 ... LazyProxy(greeting, 'universe'),
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
178 ... ]
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
179 >>> greetings.sort()
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
180 >>> for greeting in greetings:
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
181 ... print greeting
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
182 Hello, Joe!
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
183 Hello, universe!
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
184 Hello, world!
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
185 """
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
186 __slots__ = ['_func', '_args', '_kwargs', '_value']
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
187
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
188 def __init__(self, func, *args, **kwargs):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
189 # Avoid triggering our own __setattr__ implementation
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
190 object.__setattr__(self, '_func', func)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
191 object.__setattr__(self, '_args', args)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
192 object.__setattr__(self, '_kwargs', kwargs)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
193 object.__setattr__(self, '_value', None)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
194
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def value(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
196 if self._value is None:
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
197 value = self._func(*self._args, **self._kwargs)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
198 object.__setattr__(self, '_value', value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
199 return self._value
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
200 value = property(value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
201
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
202 def __contains__(self, key):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
203 return key in self.value
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
204
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
205 def __nonzero__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
206 return bool(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
207
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
208 def __dir__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
209 return dir(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
210
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
211 def __iter__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
212 return iter(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
213
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
214 def __len__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
215 return len(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
216
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
217 def __str__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
218 return str(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
219
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
220 def __unicode__(self):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
221 return unicode(self.value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
222
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
223 def __add__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
224 return self.value + other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
225
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
226 def __radd__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
227 return other + self.value
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
228
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
229 def __mod__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
230 return self.value % other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
231
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
232 def __rmod__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
233 return other % self.value
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
234
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
235 def __mul__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
236 return self.value * other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
237
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
238 def __rmul__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
239 return other * self.value
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
240
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
241 def __call__(self, *args, **kwargs):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
242 return self.value(*args, **kwargs)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
243
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
244 def __lt__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
245 return self.value < other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
246
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
247 def __le__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
248 return self.value <= other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
249
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
250 def __eq__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
251 return self.value == other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
252
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
253 def __ne__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
254 return self.value != other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
255
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
256 def __gt__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
257 return self.value > other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
258
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __ge__(self, other):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
260 return self.value >= other
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
261
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
262 def __delattr__(self, name):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
263 delattr(self.value, name)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
264
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
265 def __getattr__(self, name):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
266 return getattr(self.value, name)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
267
100
a19dff75d93b o fix misnamed vars in LazyProxy
pjenvey
parents: 61
diff changeset
268 def __setattr__(self, name, value):
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
269 setattr(self.value, name, value)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
270
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __delitem__(self, key):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
272 del self.value[key]
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
273
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __getitem__(self, key):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
275 return self.value[key]
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
276
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 def __setitem__(self, key, value):
100
a19dff75d93b o fix misnamed vars in LazyProxy
pjenvey
parents: 61
diff changeset
278 self.value[key] = value
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
279
402
307fabac5d71 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
280
307fabac5d71 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
281 class Translations(gettext.GNUTranslations, object):
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
282 """An extended translation catalog class."""
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
283
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
284 DEFAULT_DOMAIN = 'messages'
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
285
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
286 def __init__(self, fileobj=None, domain=DEFAULT_DOMAIN):
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
287 """Initialize the translations catalog.
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
288
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
289 :param fileobj: the file-like object the translation should be read
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
290 from
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
291 """
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, 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 gettext.GNUTranslations.__init__(self, fp=fileobj)
336
fb7c7aae2905 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
293 self.files = filter(None, [getattr(fileobj, 'name', None)])
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
294 self.domain = domain
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
295 self._domains = {}
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
296
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
297 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
298 """Load translations from the given directory.
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
299
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
300 :param dirname: the directory containing the ``MO`` files
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
301 :param locales: the list of locales in order of preference (items in
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
302 this list can be either `Locale` objects or locale
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
303 strings)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
304 :param domain: the message domain
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
305 :return: the loaded catalog, or a ``NullTranslations`` instance if no
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
306 matching translations were found
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
307 :rtype: `Translations`
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
308 """
336
fb7c7aae2905 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
309 if locales is not None:
fb7c7aae2905 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
310 if not isinstance(locales, (list, tuple)):
fb7c7aae2905 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
311 locales = [locales]
fb7c7aae2905 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
312 locales = [str(locale) for locale in locales]
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
313 if not domain:
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
314 domain = cls.DEFAULT_DOMAIN
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
315 filename = gettext.find(domain, dirname, locales)
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
316 if not filename:
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
317 return gettext.NullTranslations()
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
318 return cls(fileobj=open(filename, 'rb'), domain=domain)
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
319 load = classmethod(load)
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
320
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
321 def __repr__(self):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
322 return '<%s: "%s">' % (type(self).__name__,
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
323 self._info.get('project-id-version'))
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
324
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
325 def add(self, translations, merge=True):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
326 """Add the given translations to the catalog.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
327
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
328 If the domain of the translations is different than that of the
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
329 current catalog, they are added as a catalog that is only accessible
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
330 by the various ``d*gettext`` functions.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
331
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
332 :param translations: the `Translations` instance with the messages to
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
333 add
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
334 :param merge: whether translations for message domains that have
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
335 already been added should be merged with the existing
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
336 translations
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
337 :return: the `Translations` instance (``self``) so that `merge` calls
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
338 can be easily chained
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
339 :rtype: `Translations`
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
340 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
341 domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
342 if merge and domain == self.domain:
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
343 return self.merge(translations)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
344
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
345 existing = self._domains.get(domain)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
346 if merge and existing is not None:
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
347 existing.merge(translations)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
348 else:
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
349 translations.add_fallback(self)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
350 self._domains[domain] = translations
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
351
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
352 return self
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
353
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
354 def merge(self, translations):
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
355 """Merge the given translations into the catalog.
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
356
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
357 Message translations in the specified catalog override any messages
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
358 with the same identifier in the existing catalog.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
359
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
360 :param translations: the `Translations` instance with the messages to
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
361 merge
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
362 :return: the `Translations` instance (``self``) so that `merge` calls
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
363 can be easily chained
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
364 :rtype: `Translations`
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
365 """
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
366 if isinstance(translations, gettext.GNUTranslations):
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
367 self._catalog.update(translations._catalog)
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
368 if isinstance(translations, Translations):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
369 self.files.extend(translations.files)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
370
61
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
371 return self
da7efa40a9e2 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
372
408
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
373 def dgettext(self, domain, message):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
374 """Like ``gettext()``, but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
375 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
376 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
377 return self._domains.get(domain, self).gettext(message)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
378
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
379 def ldgettext(self, domain, message):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
380 """Like ``lgettext()``, but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
381 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
382 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
383 return self._domains.get(domain, self).lgettext(message)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
384
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
385 def dugettext(self, domain, message):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
386 """Like ``ugettext()``, but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
387 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
388 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
389 return self._domains.get(domain, self).ugettext(message)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
390
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
391 def dngettext(self, domain, singular, plural, num):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
392 """Like ``ngettext()``, but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
393 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
394 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
395 return self._domains.get(domain, self).ngettext(singular, plural, num)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
396
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
397 def ldngettext(self, domain, singular, plural, num):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
398 """Like ``lngettext()``, but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
399 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
400 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
401 return self._domains.get(domain, self).lngettext(singular, plural, num)
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
402
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
403 def dungettext(self, domain, singular, plural, num):
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
404 """Like ``ungettext()`` but look the message up in the specified
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
405 domain.
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
406 """
94d992d409c7 Add message domain support to the `Translations` class. Closes #137.
cmlenz
parents: 402
diff changeset
407 return self._domains.get(domain, self).ungettext(singular, plural, num)
420
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
408
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
409 # Most of the downwards code, until it get's included in stdlib, from:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
410 # http://bugs.python.org/file10036/gettext-pgettext.patch
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
411 #
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
412 # The encoding of a msgctxt and a msgid in a .mo file is
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
413 # msgctxt + "\x04" + msgid (gettext version >= 0.15)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
414 CONTEXT_ENCODING = '%s\x04%s'
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
415
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
416 def pgettext(self, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
417 """Look up the `context` and `message` id in the catalog and return the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
418 corresponding message string, as an 8-bit string encoded with the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
419 catalog's charset encoding, if known. If there is no entry in the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
420 catalog for the `message` id and `context` , and a fallback has been
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
421 set, the look up is forwarded to the fallback's ``pgettext()``
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
422 method. Otherwise, the `message` id is returned.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
423 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
424 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
425 missing = object()
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
426 tmsg = self._catalog.get(ctxt_msg_id, missing)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
427 if tmsg is missing:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
428 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
429 return self._fallback.pgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
430 return message
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
431 # Encode the Unicode tmsg back to an 8-bit string, if possible
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
432 if self._output_charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
433 return tmsg.encode(self._output_charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
434 elif self._charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
435 return tmsg.encode(self._charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
436 return tmsg
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
437
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
438 def lpgettext(self, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
439 """Equivalent to ``pgettext()``, but the translation is returned in the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
440 preferred system encoding, if no other encoding was explicitly set with
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
441 ``bind_textdomain_codeset()``.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
442 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
443 ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
444 missing = object()
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
445 tmsg = self._catalog.get(ctxt_msg_id, missing)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
446 if tmsg is missing:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
447 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
448 return self._fallback.lpgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
449 return message
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
450 if self._output_charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
451 return tmsg.encode(self._output_charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
452 return tmsg.encode(locale.getpreferredencoding())
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
453
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
454 def npgettext(self, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
455 """Do a plural-forms lookup of a message id. `singular` is used as the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
456 message id for purposes of lookup in the catalog, while `num` is used to
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
457 determine which plural form to use. The returned message string is an
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
458 8-bit string encoded with the catalog's charset encoding, if known.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
459
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
460 If the message id for `context` is not found in the catalog, and a
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
461 fallback is specified, the request is forwarded to the fallback's
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
462 ``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
463 returned, and ``plural`` is returned in all other cases.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
464 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
465 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
466 try:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
467 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
468 if self._output_charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
469 return tmsg.encode(self._output_charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
470 elif self._charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
471 return tmsg.encode(self._charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
472 return tmsg
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
473 except KeyError:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
474 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
475 return self._fallback.npgettext(context, singular, plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
476 if num == 1:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
477 return singular
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
478 else:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
479 return plural
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
480
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
481 def lnpgettext(self, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
482 """Equivalent to ``npgettext()``, but the translation is returned in the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
483 preferred system encoding, if no other encoding was explicitly set with
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
484 ``bind_textdomain_codeset()``.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
485 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
486 ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
487 try:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
488 tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
489 if self._output_charset:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
490 return tmsg.encode(self._output_charset)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
491 return tmsg.encode(locale.getpreferredencoding())
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
492 except KeyError:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
493 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
494 return self._fallback.lnpgettext(context, singular, plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
495 if num == 1:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
496 return singular
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
497 else:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
498 return plural
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
499
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
500 def upgettext(self, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
501 """Look up the `context` and `message` id in the catalog and return the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
502 corresponding message string, as a Unicode string. If there is no entry
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
503 in the catalog for the `message` id and `context`, and a fallback has
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
504 been set, the look up is forwarded to the fallback's ``upgettext()``
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
505 method. Otherwise, the `message` id is returned.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
506 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
507 ctxt_message_id = self.CONTEXT_ENCODING % (context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
508 missing = object()
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
509 tmsg = self._catalog.get(ctxt_message_id, missing)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
510 if tmsg is missing:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
511 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
512 return self._fallback.upgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
513 return unicode(message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
514 return tmsg
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
515
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
516 def unpgettext(self, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
517 """Do a plural-forms lookup of a message id. `singular` is used as the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
518 message id for purposes of lookup in the catalog, while `num` is used to
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
519 determine which plural form to use. The returned message string is a
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
520 Unicode string.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
521
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
522 If the message id for `context` is not found in the catalog, and a
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
523 fallback is specified, the request is forwarded to the fallback's
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
524 ``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
525 returned, and `plural` is returned in all other cases.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
526 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
527 ctxt_message_id = self.CONTEXT_ENCODING % (context, singular)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
528 try:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
529 tmsg = self._catalog[(ctxt_message_id, self.plural(num))]
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
530 except KeyError:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
531 if self._fallback:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
532 return self._fallback.unpgettext(context, singular, plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
533 if num == 1:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
534 tmsg = unicode(singular)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
535 else:
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
536 tmsg = unicode(plural)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
537 return tmsg
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
538
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
539 def dpgettext(self, domain, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
540 """Like `pgettext()`, but look the message up in the specified
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
541 `domain`.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
542 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
543 return self._domains.get(domain, self).pgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
544
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
545 def dupgettext(self, domain, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
546 """Like `upgettext()`, but look the message up in the specified
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
547 `domain`.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
548 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
549 return self._domains.get(domain, self).upgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
550
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
551 def ldpgettext(self, domain, context, message):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
552 """Equivalent to ``dpgettext()``, but the translation is returned in the
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
553 preferred system encoding, if no other encoding was explicitly set with
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
554 ``bind_textdomain_codeset()``.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
555 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
556 return self._domains.get(domain, self).lpgettext(context, message)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
557
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
558 def dnpgettext(self, domain, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
559 """Like ``npgettext``, but look the message up in the specified
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
560 `domain`.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
561 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
562 return self._domains.get(domain, self).npgettext(context, singular,
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
563 plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
564
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
565 def dunpgettext(self, domain, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
566 """Like ``unpgettext``, but look the message up in the specified
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
567 `domain`.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
568 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
569 return self._domains.get(domain, self).unpgettext(context, singular,
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
570 plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
571
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
572 def ldnpgettext(self, domain, context, singular, plural, num):
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
573 """Equivalent to ``dnpgettext()``, but the translation is returned in
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
574 the preferred system encoding, if no other encoding was explicitly set
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
575 with ``bind_textdomain_codeset()``.
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
576 """
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
577 return self._domains.get(domain, self).lnpgettext(context, singular,
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
578 plural, num)
53c68c594b5f Add support for `msgctxt`. See #54.
palgarvio
parents: 414
diff changeset
579
Copyright (C) 2012-2017 Edgewall Software