Hide keyboard shortcuts

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 

2 

3from .choices import ALL_LANGUAGE_CHOICES 

4from .constants import LANGUAGES 

5 

6# Exports 

7 

8__all__ = ( 

9 "Language", 

10) 

11 

12# Classes 

13 

14 

15class Language(object): 

16 """A language descriptor.""" 

17 

18 def __init__(self, code, country=None, label=None): 

19 self.code = code 

20 self.country = country 

21 self._label = label or code 

22 

23 def __repr__(self): 

24 return "<%s %s:%s>" % (self.__class__.__name__, self.code, self.label) 

25 

26 def __str__(self): 

27 return self.label 

28 

29 def get_localized_name(self): 

30 """Get the localized (unicode) name of the language. 

31 

32 :rtype: str | unicode 

33 

34 .. note:: 

35 If the tag is not found, the language label is returned. 

36 

37 """ 

38 return LANGUAGES.get(self.code, self.label) 

39 

40 @property 

41 def is_valid(self): 

42 """Indicates whether the language code is valid. 

43 

44 :rtype: bool 

45 

46 """ 

47 return self.code in LANGUAGES 

48 

49 @property 

50 def label(self): 

51 """Get the language label. 

52 

53 :rtype: str 

54 

55 """ 

56 if self.is_valid: 

57 for key, value in ALL_LANGUAGE_CHOICES: 

58 if self.code == key: 

59 return str(value) 

60 

61 return self._label 

62 

63 @property 

64 def localized_name(self): 

65 """Alias for ``get_localized_name()``.""" 

66 return self.get_localized_name()