# Imports
from .choices import ALL_LANGUAGE_CHOICES
from .constants import LANGUAGES
# Exports
__all__ = (
"Language",
)
# Classes
[docs]class Language(object):
"""A language descriptor."""
[docs] def __init__(self, code, country=None, label=None):
self.code = code
self.country = country
self._label = label or code
def __repr__(self):
return "<%s %s:%s>" % (self.__class__.__name__, self.code, self.label)
def __str__(self):
return self.label
[docs] def get_localized_name(self):
"""Get the localized (unicode) name of the language.
:rtype: str | unicode
.. note::
If the tag is not found, the language label is returned.
"""
return LANGUAGES.get(self.code, self.label)
@property
def is_valid(self):
"""Indicates whether the language code is valid.
:rtype: bool
"""
return self.code in LANGUAGES
@property
def label(self):
"""Get the language label.
:rtype: str
"""
if self.is_valid:
for key, value in ALL_LANGUAGE_CHOICES:
if self.code == key:
return str(value)
return self._label
@property
def localized_name(self):
"""Alias for ``get_localized_name()``."""
return self.get_localized_name()