Skip to content

Locales


Locale

Singleton class for managing supported languages and ISO language codes.

Provides functionality to handle language codes in both ISO 639-1 (two-letter) and full name formats.

Attributes:

Name Type Description
_instance Locale or None

Singleton instance of the class

_supported_languages set

Set of supported ISO 639-1 language codes

Methods:

Name Description
get_supported_languages

Returns set of supported languages in specified format

is_language_supported

Checks if a language is supported

get_pt1_from_full

Converts full language name to ISO 639-1 code

get_full_from_pt1

Converts ISO 639-1 code to full language name

Source code in apps/annotator/code/text_processor/locales.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class Locale:
    """
    Singleton class for managing supported languages and ISO language codes.

    Provides functionality to handle language codes in both ISO 639-1 (two-letter)
    and full name formats.

    Attributes
    ----------
    _instance : Locale or None
        Singleton instance of the class
    _supported_languages : set
        Set of supported ISO 639-1 language codes

    Methods
    -------
    get_supported_languages(kind=FORMAT_PT1)
        Returns set of supported languages in specified format
    is_language_supported(language)
        Checks if a language is supported
    get_pt1_from_full(language)
        Converts full language name to ISO 639-1 code
    get_full_from_pt1(language, lower=True)
        Converts ISO 639-1 code to full language name
    """

    _instance = None

    def __new__(cls):
        """
        Creates or returns the singleton instance of Locale.

        Returns
        -------
        Locale
            The singleton instance
        """
        if cls._instance is None:
            cls._instance = super(Locale, cls).__new__(cls)
            cls._supported_languages = {'it','en'}
        return cls._instance

    def get_supported_languages(self, kind=FORMAT_PT1):
        """
        Returns set of supported languages in specified format.

        Parameters
        ----------
        kind : int, default=FORMAT_PT1
            Format type for returned language codes:
            - FORMAT_PT1: ISO 639-1 two-letter codes
            - FORMAT_FULL: Full language names

        Returns
        -------
        set
            Set of language codes or names

        Raises
        ------
        Exception
            If kind parameter is not implemented
        """
        if kind == FORMAT_PT1:
            return self._supported_languages.copy()
        elif kind == FORMAT_FULL:
            return {self.get_full_from_pt1(lang) for lang in self._supported_languages.copy()}
        raise Exception("Locale.get_supported_language() -> kind not implemented")

    def is_language_supported(self, language:str):
        """
        Checks if a language is in the supported languages set.

        Parameters
        ----------
        language : str
            Language code or name to check

        Returns
        -------
        bool
            True if language is supported, False otherwise
        """
        if len(language) > 2:
            language = Lang(language).pt1
        return language in self._supported_languages

    @staticmethod
    def get_pt1_from_full(language:str):
        """
        Converts full language name to ISO 639-1 code.

        Parameters
        ----------
        language : str
            Full language name

        Returns
        -------
        str
            ISO 639-1 two-letter language code
        """
        return Lang(name=language.capitalize()).pt1

    @staticmethod
    def get_full_from_pt1(language:str, lower:bool=True):
        """
        Converts ISO 639-1 code to full language name.

        Parameters
        ----------
        language : str
            ISO 639-1 two-letter language code
        lower : bool, default=True
            If True, returns lowercase name

        Returns
        -------
        str
            Full language name
        """
        return Lang(pt1=language).name if not lower else Lang(pt1=language).name.lower()

__new__()

Creates or returns the singleton instance of Locale.

Returns:

Type Description
Locale

The singleton instance

Source code in apps/annotator/code/text_processor/locales.py
35
36
37
38
39
40
41
42
43
44
45
46
47
def __new__(cls):
    """
    Creates or returns the singleton instance of Locale.

    Returns
    -------
    Locale
        The singleton instance
    """
    if cls._instance is None:
        cls._instance = super(Locale, cls).__new__(cls)
        cls._supported_languages = {'it','en'}
    return cls._instance

get_full_from_pt1(language, lower=True) staticmethod

Converts ISO 639-1 code to full language name.

Parameters:

Name Type Description Default
language str

ISO 639-1 two-letter language code

required
lower bool

If True, returns lowercase name

True

Returns:

Type Description
str

Full language name

Source code in apps/annotator/code/text_processor/locales.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@staticmethod
def get_full_from_pt1(language:str, lower:bool=True):
    """
    Converts ISO 639-1 code to full language name.

    Parameters
    ----------
    language : str
        ISO 639-1 two-letter language code
    lower : bool, default=True
        If True, returns lowercase name

    Returns
    -------
    str
        Full language name
    """
    return Lang(pt1=language).name if not lower else Lang(pt1=language).name.lower()

get_pt1_from_full(language) staticmethod

Converts full language name to ISO 639-1 code.

Parameters:

Name Type Description Default
language str

Full language name

required

Returns:

Type Description
str

ISO 639-1 two-letter language code

Source code in apps/annotator/code/text_processor/locales.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
@staticmethod
def get_pt1_from_full(language:str):
    """
    Converts full language name to ISO 639-1 code.

    Parameters
    ----------
    language : str
        Full language name

    Returns
    -------
    str
        ISO 639-1 two-letter language code
    """
    return Lang(name=language.capitalize()).pt1

get_supported_languages(kind=FORMAT_PT1)

Returns set of supported languages in specified format.

Parameters:

Name Type Description Default
kind int

Format type for returned language codes: - FORMAT_PT1: ISO 639-1 two-letter codes - FORMAT_FULL: Full language names

FORMAT_PT1

Returns:

Type Description
set

Set of language codes or names

Raises:

Type Description
Exception

If kind parameter is not implemented

Source code in apps/annotator/code/text_processor/locales.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def get_supported_languages(self, kind=FORMAT_PT1):
    """
    Returns set of supported languages in specified format.

    Parameters
    ----------
    kind : int, default=FORMAT_PT1
        Format type for returned language codes:
        - FORMAT_PT1: ISO 639-1 two-letter codes
        - FORMAT_FULL: Full language names

    Returns
    -------
    set
        Set of language codes or names

    Raises
    ------
    Exception
        If kind parameter is not implemented
    """
    if kind == FORMAT_PT1:
        return self._supported_languages.copy()
    elif kind == FORMAT_FULL:
        return {self.get_full_from_pt1(lang) for lang in self._supported_languages.copy()}
    raise Exception("Locale.get_supported_language() -> kind not implemented")

is_language_supported(language)

Checks if a language is in the supported languages set.

Parameters:

Name Type Description Default
language str

Language code or name to check

required

Returns:

Type Description
bool

True if language is supported, False otherwise

Source code in apps/annotator/code/text_processor/locales.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def is_language_supported(self, language:str):
    """
    Checks if a language is in the supported languages set.

    Parameters
    ----------
    language : str
        Language code or name to check

    Returns
    -------
    bool
        True if language is supported, False otherwise
    """
    if len(language) > 2:
        language = Lang(language).pt1
    return language in self._supported_languages