Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from .choices import ALL_LANGUAGE_CHOICES
4from .constants import LANGUAGES
6# Exports
8__all__ = (
9 "Language",
10)
12# Classes
15class Language(object):
16 """A language descriptor."""
18 def __init__(self, code, country=None, label=None):
19 self.code = code
20 self.country = country
21 self._label = label or code
23 def __repr__(self):
24 return "<%s %s:%s>" % (self.__class__.__name__, self.code, self.label)
26 def __str__(self):
27 return self.label
29 def get_localized_name(self):
30 """Get the localized (unicode) name of the language.
32 :rtype: str | unicode
34 .. note::
35 If the tag is not found, the language label is returned.
37 """
38 return LANGUAGES.get(self.code, self.label)
40 @property
41 def is_valid(self):
42 """Indicates whether the language code is valid.
44 :rtype: bool
46 """
47 return self.code in LANGUAGES
49 @property
50 def label(self):
51 """Get the language label.
53 :rtype: str
55 """
56 if self.is_valid:
57 for key, value in ALL_LANGUAGE_CHOICES:
58 if self.code == key:
59 return str(value)
61 return self._label
63 @property
64 def localized_name(self):
65 """Alias for ``get_localized_name()``."""
66 return self.get_localized_name()