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