comparison babel/core.py @ 579:99d51589c822 trunk

use decorators (as we require Python 2.4+ anyway)
author fschwarz
date Tue, 31 Jul 2012 08:46:19 +0000
parents 1de26da5aa25
children 6f86d60dab56
comparison
equal deleted inserted replaced
578:b167d06df1d6 579:99d51589c822
134 134
135 identifier = str(self) 135 identifier = str(self)
136 if not localedata.exists(identifier): 136 if not localedata.exists(identifier):
137 raise UnknownLocaleError(identifier) 137 raise UnknownLocaleError(identifier)
138 138
139 @classmethod
139 def default(cls, category=None, aliases=LOCALE_ALIASES): 140 def default(cls, category=None, aliases=LOCALE_ALIASES):
140 """Return the system default locale for the specified category. 141 """Return the system default locale for the specified category.
141 142
142 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: 143 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']:
143 ... os.environ[name] = '' 144 ... os.environ[name] = ''
151 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) 152 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``)
152 :rtype: `Locale` 153 :rtype: `Locale`
153 :see: `default_locale` 154 :see: `default_locale`
154 """ 155 """
155 return cls(default_locale(category, aliases=aliases)) 156 return cls(default_locale(category, aliases=aliases))
156 default = classmethod(default) 157
157 158 @classmethod
158 def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES): 159 def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES):
159 """Find the best match between available and requested locale strings. 160 """Find the best match between available and requested locale strings.
160 161
161 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) 162 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
162 <Locale "de_DE"> 163 <Locale "de_DE">
181 """ 182 """
182 identifier = negotiate_locale(preferred, available, sep=sep, 183 identifier = negotiate_locale(preferred, available, sep=sep,
183 aliases=aliases) 184 aliases=aliases)
184 if identifier: 185 if identifier:
185 return Locale.parse(identifier, sep=sep) 186 return Locale.parse(identifier, sep=sep)
186 negotiate = classmethod(negotiate) 187
187 188 @classmethod
188 def parse(cls, identifier, sep='_'): 189 def parse(cls, identifier, sep='_'):
189 """Create a `Locale` instance for the given locale identifier. 190 """Create a `Locale` instance for the given locale identifier.
190 191
191 >>> l = Locale.parse('de-DE', sep='-') 192 >>> l = Locale.parse('de-DE', sep='-')
192 >>> l.display_name 193 >>> l.display_name
209 :see: `parse_locale` 210 :see: `parse_locale`
210 """ 211 """
211 if isinstance(identifier, basestring): 212 if isinstance(identifier, basestring):
212 return cls(*parse_locale(identifier, sep=sep)) 213 return cls(*parse_locale(identifier, sep=sep))
213 return identifier 214 return identifier
214 parse = classmethod(parse)
215 215
216 def __eq__(self, other): 216 def __eq__(self, other):
217 return str(self) == str(other) 217 return str(self) == str(other)
218 218
219 def __ne__(self, other): 219 def __ne__(self, other):
224 224
225 def __str__(self): 225 def __str__(self):
226 return '_'.join(filter(None, [self.language, self.script, 226 return '_'.join(filter(None, [self.language, self.script,
227 self.territory, self.variant])) 227 self.territory, self.variant]))
228 228
229 @property
229 def _data(self): 230 def _data(self):
230 if self.__data is None: 231 if self.__data is None:
231 self.__data = localedata.LocaleDataDict(localedata.load(str(self))) 232 self.__data = localedata.LocaleDataDict(localedata.load(str(self)))
232 return self.__data 233 return self.__data
233 _data = property(_data)
234 234
235 def get_display_name(self, locale=None): 235 def get_display_name(self, locale=None):
236 """Return the display name of the locale using the given locale. 236 """Return the display name of the locale using the given locale.
237 237
238 The display name will include the language, territory, script, and 238 The display name will include the language, territory, script, and
272 u'svenska' 272 u'svenska'
273 273
274 :type: `unicode` 274 :type: `unicode`
275 """) 275 """)
276 276
277 @property
277 def english_name(self): 278 def english_name(self):
278 return self.get_display_name(Locale('en')) 279 """The english display name of the locale.
279 english_name = property(english_name, doc="""\
280 The english display name of the locale.
281 280
282 >>> Locale('de').english_name 281 >>> Locale('de').english_name
283 u'German' 282 u'German'
284 >>> Locale('de', 'DE').english_name 283 >>> Locale('de', 'DE').english_name
285 u'German (Germany)' 284 u'German (Germany)'
286 285
287 :type: `unicode` 286 :type: `unicode`"""
288 """) 287 return self.get_display_name(Locale('en'))
289 288
290 #{ General Locale Display Names 289 #{ General Locale Display Names
291 290
291 @property
292 def languages(self): 292 def languages(self):
293 return self._data['languages'] 293 """Mapping of language codes to translated language names.
294 languages = property(languages, doc="""\
295 Mapping of language codes to translated language names.
296 294
297 >>> Locale('de', 'DE').languages['ja'] 295 >>> Locale('de', 'DE').languages['ja']
298 u'Japanisch' 296 u'Japanisch'
299 297
300 :type: `dict` 298 :type: `dict`
301 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ 299 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_"""
302 """) 300 return self._data['languages']
303 301
302 @property
304 def scripts(self): 303 def scripts(self):
305 return self._data['scripts'] 304 """Mapping of script codes to translated script names.
306 scripts = property(scripts, doc="""\
307 Mapping of script codes to translated script names.
308 305
309 >>> Locale('en', 'US').scripts['Hira'] 306 >>> Locale('en', 'US').scripts['Hira']
310 u'Hiragana' 307 u'Hiragana'
311 308
312 :type: `dict` 309 :type: `dict`
313 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ 310 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_"""
314 """) 311 return self._data['scripts']
315 312
313 @property
316 def territories(self): 314 def territories(self):
317 return self._data['territories'] 315 """Mapping of script codes to translated script names.
318 territories = property(territories, doc="""\
319 Mapping of script codes to translated script names.
320 316
321 >>> Locale('es', 'CO').territories['DE'] 317 >>> Locale('es', 'CO').territories['DE']
322 u'Alemania' 318 u'Alemania'
323 319
324 :type: `dict` 320 :type: `dict`
325 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ 321 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_"""
326 """) 322 return self._data['territories']
327 323
324 @property
328 def variants(self): 325 def variants(self):
329 return self._data['variants'] 326 """Mapping of script codes to translated script names.
330 variants = property(variants, doc="""\
331 Mapping of script codes to translated script names.
332 327
333 >>> Locale('de', 'DE').variants['1901'] 328 >>> Locale('de', 'DE').variants['1901']
334 u'Alte deutsche Rechtschreibung' 329 u'Alte deutsche Rechtschreibung'
335 330
336 :type: `dict` 331 :type: `dict`"""
337 """) 332 return self._data['variants']
338 333
339 #{ Number Formatting 334 #{ Number Formatting
340 335
336 @property
341 def currencies(self): 337 def currencies(self):
342 return self._data['currency_names'] 338 """Mapping of currency codes to translated currency names.
343 currencies = property(currencies, doc="""\
344 Mapping of currency codes to translated currency names.
345 339
346 >>> Locale('en').currencies['COP'] 340 >>> Locale('en').currencies['COP']
347 u'Colombian Peso' 341 u'Colombian Peso'
348 >>> Locale('de', 'DE').currencies['COP'] 342 >>> Locale('de', 'DE').currencies['COP']
349 u'Kolumbianischer Peso' 343 u'Kolumbianischer Peso'
350 344
351 :type: `dict` 345 :type: `dict`"""
352 """) 346 return self._data['currency_names']
353 347
348 @property
354 def currency_symbols(self): 349 def currency_symbols(self):
355 return self._data['currency_symbols'] 350 """Mapping of currency codes to symbols.
356 currency_symbols = property(currency_symbols, doc="""\
357 Mapping of currency codes to symbols.
358 351
359 >>> Locale('en', 'US').currency_symbols['USD'] 352 >>> Locale('en', 'US').currency_symbols['USD']
360 u'$' 353 u'$'
361 >>> Locale('es', 'CO').currency_symbols['USD'] 354 >>> Locale('es', 'CO').currency_symbols['USD']
362 u'US$' 355 u'US$'
363 356
364 :type: `dict` 357 :type: `dict`"""
365 """) 358 return self._data['currency_symbols']
366 359
360 @property
367 def number_symbols(self): 361 def number_symbols(self):
368 return self._data['number_symbols'] 362 """Symbols used in number formatting.
369 number_symbols = property(number_symbols, doc="""\
370 Symbols used in number formatting.
371 363
372 >>> Locale('fr', 'FR').number_symbols['decimal'] 364 >>> Locale('fr', 'FR').number_symbols['decimal']
373 u',' 365 u','
374 366
375 :type: `dict` 367 :type: `dict`"""
376 """) 368 return self._data['number_symbols']
377 369
370 @property
378 def decimal_formats(self): 371 def decimal_formats(self):
379 return self._data['decimal_formats'] 372 """Locale patterns for decimal number formatting.
380 decimal_formats = property(decimal_formats, doc="""\
381 Locale patterns for decimal number formatting.
382 373
383 >>> Locale('en', 'US').decimal_formats[None] 374 >>> Locale('en', 'US').decimal_formats[None]
384 <NumberPattern u'#,##0.###'> 375 <NumberPattern u'#,##0.###'>
385 376
386 :type: `dict` 377 :type: `dict`"""
387 """) 378 return self._data['decimal_formats']
388 379
380 @property
389 def currency_formats(self): 381 def currency_formats(self):
382 """Locale patterns for currency number formatting.
383
384 >>> print Locale('en', 'US').currency_formats[None]
385 <NumberPattern u'\\xa4#,##0.00'>
386
387 :type: `dict`"""
390 return self._data['currency_formats'] 388 return self._data['currency_formats']
391 currency_formats = property(currency_formats, doc=r"""\ 389
392 Locale patterns for currency number formatting. 390 @property
393
394 >>> print Locale('en', 'US').currency_formats[None]
395 <NumberPattern u'\xa4#,##0.00'>
396
397 :type: `dict`
398 """)
399
400 def percent_formats(self): 391 def percent_formats(self):
401 return self._data['percent_formats'] 392 """Locale patterns for percent number formatting.
402 percent_formats = property(percent_formats, doc="""\
403 Locale patterns for percent number formatting.
404 393
405 >>> Locale('en', 'US').percent_formats[None] 394 >>> Locale('en', 'US').percent_formats[None]
406 <NumberPattern u'#,##0%'> 395 <NumberPattern u'#,##0%'>
407 396
408 :type: `dict` 397 :type: `dict`"""
409 """) 398 return self._data['percent_formats']
410 399
400 @property
411 def scientific_formats(self): 401 def scientific_formats(self):
412 return self._data['scientific_formats'] 402 """Locale patterns for scientific number formatting.
413 scientific_formats = property(scientific_formats, doc="""\
414 Locale patterns for scientific number formatting.
415 403
416 >>> Locale('en', 'US').scientific_formats[None] 404 >>> Locale('en', 'US').scientific_formats[None]
417 <NumberPattern u'#E0'> 405 <NumberPattern u'#E0'>
418 406
419 :type: `dict` 407 :type: `dict`"""
420 """) 408 return self._data['scientific_formats']
421 409
422 #{ Calendar Information and Date Formatting 410 #{ Calendar Information and Date Formatting
423 411
412 @property
424 def periods(self): 413 def periods(self):
425 return self._data['periods'] 414 """Locale display names for day periods (AM/PM).
426 periods = property(periods, doc="""\
427 Locale display names for day periods (AM/PM).
428 415
429 >>> Locale('en', 'US').periods['am'] 416 >>> Locale('en', 'US').periods['am']
430 u'AM' 417 u'AM'
431 418
432 :type: `dict` 419 :type: `dict`"""
433 """) 420 return self._data['periods']
434 421
422 @property
435 def days(self): 423 def days(self):
436 return self._data['days'] 424 """Locale display names for weekdays.
437 days = property(days, doc="""\
438 Locale display names for weekdays.
439 425
440 >>> Locale('de', 'DE').days['format']['wide'][3] 426 >>> Locale('de', 'DE').days['format']['wide'][3]
441 u'Donnerstag' 427 u'Donnerstag'
442 428
443 :type: `dict` 429 :type: `dict`"""
444 """) 430 return self._data['days']
445 431
432 @property
446 def months(self): 433 def months(self):
447 return self._data['months'] 434 """Locale display names for months.
448 months = property(months, doc="""\
449 Locale display names for months.
450 435
451 >>> Locale('de', 'DE').months['format']['wide'][10] 436 >>> Locale('de', 'DE').months['format']['wide'][10]
452 u'Oktober' 437 u'Oktober'
453 438
454 :type: `dict` 439 :type: `dict`"""
455 """) 440 return self._data['months']
456 441
442 @property
457 def quarters(self): 443 def quarters(self):
458 return self._data['quarters'] 444 """Locale display names for quarters.
459 quarters = property(quarters, doc="""\
460 Locale display names for quarters.
461 445
462 >>> Locale('de', 'DE').quarters['format']['wide'][1] 446 >>> Locale('de', 'DE').quarters['format']['wide'][1]
463 u'1. Quartal' 447 u'1. Quartal'
464 448
465 :type: `dict` 449 :type: `dict`"""
466 """) 450 return self._data['quarters']
467 451
452 @property
468 def eras(self): 453 def eras(self):
469 return self._data['eras'] 454 """Locale display names for eras.
470 eras = property(eras, doc="""\
471 Locale display names for eras.
472 455
473 >>> Locale('en', 'US').eras['wide'][1] 456 >>> Locale('en', 'US').eras['wide'][1]
474 u'Anno Domini' 457 u'Anno Domini'
475 >>> Locale('en', 'US').eras['abbreviated'][0] 458 >>> Locale('en', 'US').eras['abbreviated'][0]
476 u'BC' 459 u'BC'
477 460
478 :type: `dict` 461 :type: `dict`"""
479 """) 462 return self._data['eras']
480 463
464 @property
481 def time_zones(self): 465 def time_zones(self):
482 return self._data['time_zones'] 466 """Locale display names for time zones.
483 time_zones = property(time_zones, doc="""\
484 Locale display names for time zones.
485 467
486 >>> Locale('en', 'US').time_zones['Europe/London']['long']['daylight'] 468 >>> Locale('en', 'US').time_zones['Europe/London']['long']['daylight']
487 u'British Summer Time' 469 u'British Summer Time'
488 >>> Locale('en', 'US').time_zones['America/St_Johns']['city'] 470 >>> Locale('en', 'US').time_zones['America/St_Johns']['city']
489 u"St. John's" 471 u"St. John's"
490 472
491 :type: `dict` 473 :type: `dict`"""
492 """) 474 return self._data['time_zones']
493 475
476 @property
494 def meta_zones(self): 477 def meta_zones(self):
495 return self._data['meta_zones'] 478 """Locale display names for meta time zones.
496 meta_zones = property(meta_zones, doc="""\
497 Locale display names for meta time zones.
498 479
499 Meta time zones are basically groups of different Olson time zones that 480 Meta time zones are basically groups of different Olson time zones that
500 have the same GMT offset and daylight savings time. 481 have the same GMT offset and daylight savings time.
501 482
502 >>> Locale('en', 'US').meta_zones['Europe_Central']['long']['daylight'] 483 >>> Locale('en', 'US').meta_zones['Europe_Central']['long']['daylight']
503 u'Central European Summer Time' 484 u'Central European Summer Time'
504 485
505 :type: `dict` 486 :type: `dict`
506 :since: version 0.9 487 :since: version 0.9"""
507 """) 488 return self._data['meta_zones']
508 489
490 @property
509 def zone_formats(self): 491 def zone_formats(self):
510 return self._data['zone_formats'] 492 """Patterns related to the formatting of time zones.
511 zone_formats = property(zone_formats, doc=r"""\
512 Patterns related to the formatting of time zones.
513 493
514 >>> Locale('en', 'US').zone_formats['fallback'] 494 >>> Locale('en', 'US').zone_formats['fallback']
515 u'%(1)s (%(0)s)' 495 u'%(1)s (%(0)s)'
516 >>> Locale('pt', 'BR').zone_formats['region'] 496 >>> Locale('pt', 'BR').zone_formats['region']
517 u'Hor\xe1rio %s' 497 u'Hor\\xe1rio %s'
518 498
519 :type: `dict` 499 :type: `dict`
520 :since: version 0.9 500 :since: version 0.9"""
521 """) 501 return self._data['zone_formats']
522 502
503 @property
523 def first_week_day(self): 504 def first_week_day(self):
524 return self._data['week_data']['first_day'] 505 """The first day of a week, with 0 being Monday.
525 first_week_day = property(first_week_day, doc="""\
526 The first day of a week, with 0 being Monday.
527 506
528 >>> Locale('de', 'DE').first_week_day 507 >>> Locale('de', 'DE').first_week_day
529 0 508 0
530 >>> Locale('en', 'US').first_week_day 509 >>> Locale('en', 'US').first_week_day
531 6 510 6
532 511
533 :type: `int` 512 :type: `int`"""
534 """) 513 return self._data['week_data']['first_day']
535 514
515 @property
536 def weekend_start(self): 516 def weekend_start(self):
537 return self._data['week_data']['weekend_start'] 517 """The day the weekend starts, with 0 being Monday.
538 weekend_start = property(weekend_start, doc="""\
539 The day the weekend starts, with 0 being Monday.
540 518
541 >>> Locale('de', 'DE').weekend_start 519 >>> Locale('de', 'DE').weekend_start
542 5 520 5
543 521
544 :type: `int` 522 :type: `int`"""
545 """) 523 return self._data['week_data']['weekend_start']
546 524
525 @property
547 def weekend_end(self): 526 def weekend_end(self):
548 return self._data['week_data']['weekend_end'] 527 """The day the weekend ends, with 0 being Monday.
549 weekend_end = property(weekend_end, doc="""\
550 The day the weekend ends, with 0 being Monday.
551 528
552 >>> Locale('de', 'DE').weekend_end 529 >>> Locale('de', 'DE').weekend_end
553 6 530 6
554 531
555 :type: `int` 532 :type: `int`"""
556 """) 533 return self._data['week_data']['weekend_end']
557 534
535 @property
558 def min_week_days(self): 536 def min_week_days(self):
559 return self._data['week_data']['min_days'] 537 """The minimum number of days in a week so that the week is counted as
560 min_week_days = property(min_week_days, doc="""\ 538 the first week of a year or month.
561 The minimum number of days in a week so that the week is counted as the
562 first week of a year or month.
563 539
564 >>> Locale('de', 'DE').min_week_days 540 >>> Locale('de', 'DE').min_week_days
565 4 541 4
566 542
567 :type: `int` 543 :type: `int`"""
568 """) 544 return self._data['week_data']['min_days']
569 545
546 @property
570 def date_formats(self): 547 def date_formats(self):
571 return self._data['date_formats'] 548 """Locale patterns for date formatting.
572 date_formats = property(date_formats, doc="""\
573 Locale patterns for date formatting.
574 549
575 >>> Locale('en', 'US').date_formats['short'] 550 >>> Locale('en', 'US').date_formats['short']
576 <DateTimePattern u'M/d/yy'> 551 <DateTimePattern u'M/d/yy'>
577 >>> Locale('fr', 'FR').date_formats['long'] 552 >>> Locale('fr', 'FR').date_formats['long']
578 <DateTimePattern u'd MMMM y'> 553 <DateTimePattern u'd MMMM y'>
579 554
580 :type: `dict` 555 :type: `dict`"""
581 """) 556 return self._data['date_formats']
582 557
558 @property
583 def time_formats(self): 559 def time_formats(self):
584 return self._data['time_formats'] 560 """Locale patterns for time formatting.
585 time_formats = property(time_formats, doc="""\
586 Locale patterns for time formatting.
587 561
588 >>> Locale('en', 'US').time_formats['short'] 562 >>> Locale('en', 'US').time_formats['short']
589 <DateTimePattern u'h:mm a'> 563 <DateTimePattern u'h:mm a'>
590 >>> Locale('fr', 'FR').time_formats['long'] 564 >>> Locale('fr', 'FR').time_formats['long']
591 <DateTimePattern u'HH:mm:ss z'> 565 <DateTimePattern u'HH:mm:ss z'>
592 566
593 :type: `dict` 567 :type: `dict`"""
594 """) 568 return self._data['time_formats']
595 569
570 @property
596 def datetime_formats(self): 571 def datetime_formats(self):
597 return self._data['datetime_formats'] 572 """Locale patterns for datetime formatting.
598 datetime_formats = property(datetime_formats, doc="""\
599 Locale patterns for datetime formatting.
600 573
601 >>> Locale('en').datetime_formats['full'] 574 >>> Locale('en').datetime_formats['full']
602 u'{1} {0}' 575 u'{1} {0}'
603 >>> Locale('th').datetime_formats['medium'] 576 >>> Locale('th').datetime_formats['medium']
604 u'{1}, {0}' 577 u'{1}, {0}'
605 578
606 :type: `dict` 579 :type: `dict`"""
607 """) 580 return self._data['datetime_formats']
608 581
582 @property
609 def plural_form(self): 583 def plural_form(self):
610 return self._data['plural_form'] 584 """Plural rules for the locale.
611 plural_form = property(plural_form, doc="""\
612 Plural rules for the locale.
613 585
614 >>> Locale('en').plural_form(1) 586 >>> Locale('en').plural_form(1)
615 'one' 587 'one'
616 >>> Locale('en').plural_form(0) 588 >>> Locale('en').plural_form(0)
617 'other' 589 'other'
618 >>> Locale('fr').plural_form(0) 590 >>> Locale('fr').plural_form(0)
619 'one' 591 'one'
620 >>> Locale('ru').plural_form(100) 592 >>> Locale('ru').plural_form(100)
621 'many' 593 'many'
622 594
623 :type: `PluralRule` 595 :type: `PluralRule`"""
624 """) 596 return self._data['plural_form']
625 597
626 598
627 def default_locale(category=None, aliases=LOCALE_ALIASES): 599 def default_locale(category=None, aliases=LOCALE_ALIASES):
628 """Returns the system default locale for a given category, based on 600 """Returns the system default locale for a given category, based on
629 environment variables. 601 environment variables.
Copyright (C) 2012-2017 Edgewall Software