display.txt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. .. -*- mode: rst; encoding: utf-8 -*-
  2. ====================
  3. Locale Display Names
  4. ====================
  5. .. contents:: Contents
  6. :depth: 2
  7. .. sectnum::
  8. Introduction
  9. ============
  10. While `message catalogs <messages.html>`_ allow you to localize any messages
  11. in your application, there are a number of strings that are used in many
  12. applications for which translations are readily available.
  13. Imagine for example you have a list of countries that users can choose from,
  14. and you'd like to display the names of those countries in the language the
  15. user prefers. Instead of translating all those country names yourself in your
  16. application, you can make use of the translations provided by the locale data
  17. included with Babel, which is based on the `Common Locale Data Repository
  18. (CLDR) <http://unicode.org/cldr/>`_ developed and maintained by the `Unicode
  19. Consortium <http://unicode.org/>`_.
  20. The ``Locale`` Class
  21. ====================
  22. You normally access such locale data through the `Locale`_ class provided
  23. by Babel:
  24. .. code-block:: pycon
  25. >>> from babel import Locale
  26. >>> locale = Locale('en', 'US')
  27. >>> locale.territories['US']
  28. u'United States'
  29. >>> locale = Locale('es', 'MX')
  30. >>> locale.territories['US']
  31. u'Estados Unidos'
  32. .. _`Locale`: api/babel.core.Locale-class.html
  33. In addition to country/territory names, the locale data also provides access to
  34. names of languages, scripts, variants, time zones, and more. Some of the data
  35. is closely related to number and date formatting.
  36. Most of the corresponding ``Locale`` properties return dictionaries, where the
  37. key is a code such as the ISO country and language codes. Consult the API
  38. documentation for references to the relevant specifications.
  39. Calender Display Names
  40. ======================
  41. The `Locale`_ class provides access to many locale display names related to
  42. calendar display, such as the names of week days or months.
  43. These display names are of course used for date formatting, but can also be
  44. used, for example, to show a list of months to the user in their preferred
  45. language:
  46. .. code-block:: pycon
  47. >>> locale = Locale('es')
  48. >>> month_names = locale.months['format']['wide'].items()
  49. >>> month_names.sort()
  50. >>> for idx, name in month_names:
  51. ... print name
  52. enero
  53. febrero
  54. marzo
  55. abril
  56. mayo
  57. junio
  58. julio
  59. agosto
  60. septiembre
  61. octubre
  62. noviembre
  63. diciembre