scripts/mycroft/util/lang/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 
16 __active_lang = "en-us" # English is the default active language
17 # TODO: Should this really be stored in the user config file?
18 
19 
21  """ Get the active full language code (BCP-47)
22 
23  Returns:
24  str: A BCP-47 language code, e.g. ("en-us", or "pt-pt")
25  """
26  return __active_lang
27 
28 
29 def set_active_lang(lang_code):
30  """ Set the active BCP-47 language code to be used in formatting/parsing
31 
32  Args:
33  lang (str): BCP-47 language code, e.g. "en-us" or "es-mx"
34  """
35  global __active_lang
36  if __active_lang != lang_code:
37  # TODO: Validate lang codes?
38  __active_lang = lang_code
39 
40 
41 def get_primary_lang_code(lang=None):
42  """ Get the primary language code
43 
44  Args:
45  lang (str, optional): A BCP-47 language code, or None for default
46 
47  Returns:
48  str: A primary language family, such as "en", "de" or "pt"
49  """
50  # split on the hyphen and only return the primary-language code
51  # NOTE: This is typically a two character code. The standard allows
52  # 1, 2, 3 and 4 character codes. In the future we can consider
53  # mapping from the 3 to 2 character codes, for example. But for
54  # now we can just be careful in use.
55  return get_full_lang_code(lang).split("-")[0]
56 
57 
58 def get_full_lang_code(lang=None):
59  """ Get the full language code
60 
61  Args:
62  lang (str, optional): A BCP-47 language code, or None for default
63 
64  Returns:
65  str: A full language code, such as "en-us" or "de-de"
66  """
67  if not lang:
68  lang = __active_lang
69 
70  return lang or "en-us"


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40