/usr/lib/python3/dist-packages/Onboard/LanguageSupport.py is in onboard 1.2.0-0ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | # -*- coding: utf-8 -*-
# Copyright © 2012-2015 marmuta <marmvta@gmail.com>
#
# This file is part of Onboard.
#
# Onboard is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Onboard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, print_function, unicode_literals
import subprocess
import gettext
from xml.dom import minidom
from Onboard.utils import open_utf8
### Logging ###
import logging
_logger = logging.getLogger("LanguageSupport")
###############
class LanguageDB:
"""
Keeps track of languages selectable in the language menu.
"""
_main_languages = {"aa" : "ET",
"ar" : "EG",
"bn" : "BD",
"ca" : "ES",
"cs" : "CZ",
"da" : "DK", # Danish
"de" : "DE",
"el" : "GR",
"en" : "US",
"es" : "ES",
"eu" : "ES",
"fr" : "FR",
"fy" : "BL",
"ga" : "IE", # Irish
"gd" : "GB", # Scottish Gaelic
"it" : "IT",
"li" : "NL",
"nl" : "NL",
"om" : "ET",
"pl" : "PL", # Polish
"pa" : "PK",
"pt" : "PT",
"ro" : "RO", # Romanian
"ru" : "RU",
"so" : "SO",
"sr" : "RS",
"sv" : "SE",
"ti" : "ER",
"tr" : "TR", # Turkish
}
def __init__(self, wp = None):
self._wp = wp
self._locale_ids = []
self._iso_codes = ISOCodes()
@staticmethod
def get_main_languages():
return list(LanguageDB._main_languages.keys())
def get_language_full_name_or_id(self, lang_id):
full_name = self.get_language_full_name(lang_id)
if full_name:
return full_name
return lang_id
def get_language_full_name(self, lang_id):
lang_code, country_code = self.split_lang_id(lang_id)
name = self._iso_codes.get_translated_language_name(lang_code)
if country_code:
country = self._iso_codes.get_translated_country_name(country_code)
if not country:
country = country_code
name += " (" + country + ")"
return name
def get_language_name(self, lang_id):
lang_code, country_code = self.split_lang_id(lang_id)
name = self._iso_codes.get_translated_language_name(lang_code)
return name
def get_language_code(self, lang_id):
lang_code, country_code = self.split_lang_id(lang_id)
return lang_code
def find_system_model_language_id(self, lang_id):
"""
Return lang_id if there is an exact match, else return the
default lang_id for its lanugage code.
Doctests:
>>> l = LanguageDB()
>>> l.get_system_model_language_ids = lambda : ["en_US", "en_GB", "de_DE"]
>>> l.find_system_model_language_id("en")
'en_US'
>>> l.find_system_model_language_id("en_XY")
'en_US'
>>> l.find_system_model_language_id("en_US")
'en_US'
>>> l.find_system_model_language_id("en_GB")
'en_GB'
"""
known_ids = self.get_system_model_language_ids()
if not lang_id in known_ids:
lang_code, country_code = self.split_lang_id(lang_id)
lang_id = self.get_main_lang_id(lang_code)
return lang_id
def get_system_model_language_ids(self):
"""
List of language_ids of the available system language models.
"""
return self._wp.get_system_model_names()
def get_language_ids(self):
"""
List of language_ids (ll_CC) that can be selected by the user.
"""
return self._wp.get_merged_model_names()
def get_main_lang_id(self, lang_code):
"""
Complete given language code to the language id of the main lanugage.
"""
lang_id = ""
country_code = self._main_languages.get(lang_code, "")
if country_code:
lang_id = lang_code + "_" + country_code
return lang_id
def _get_language_ids_in_locale(self):
lang_ids = []
for locale_id in self._get_locale_ids():
lang_code, country_code, encoding = self._split_locale_id(locale_id)
if lang_code and not lang_code in ["POSIX", "C"]:
lang_id = lang_code
if country_code:
lang_id += "_" + country_code
lang_ids.append(lang_id)
return lang_ids
def _get_locale_ids(self):
if not self._locale_ids:
self._locale_ids = self._find_locale_ids()
return self._locale_ids
def _find_locale_ids(self):
locale_ids = []
try:
locale_ids = subprocess.check_output(["locale", "-a"]) \
.decode("UTF-8").split("\n")
except OSError as e:
_logger.error(_format("Failed to execute '{}', {}", \
" ".join(self.args), e))
return [id for id in locale_ids if id]
@staticmethod
def _split_locale_id(locale_id):
tokens = locale_id.split(".")
lang_id = tokens[0] if len(tokens) >= 1 else ""
encoding = tokens[1] if len(tokens) >= 2 else ""
lang_code, country_code = LanguageDB.split_lang_id(lang_id)
return lang_code, country_code, encoding
@staticmethod
def split_lang_id(lang_id):
tokens = lang_id.split("_")
lang_code = tokens[0] if len(tokens) >= 1 else ""
country_code = tokens[1] if len(tokens) >= 2 else ""
return lang_code, country_code
class ISOCodes:
"""
Load and translate ISO language and country codes.
Doctests:
>>> ic = ISOCodes()
>>> ic.get_language_name("en")
'English'
>>> ic.get_country_name("DE")
'Germany'
"""
def __init__(self):
self._languages = {} # lowercase lang_ids
self._countries = {} # uppercase country_ids
self._read_all()
def get_language_name(self, lang_code):
return self._languages.get(lang_code, "")
def get_country_name(self, country_code):
return self._countries.get(country_code, "")
def get_translated_language_name(self, lang_code):
return gettext.dgettext("iso_639", self.get_language_name(lang_code))
def get_translated_country_name(self, country_code):
country_name = self.get_country_name(country_code)
if not country_name:
country_name = country_code
return gettext.dgettext("iso_3166", country_name)
def _read_all(self):
self._read_languages()
self._read_countries()
def _read_languages(self):
with open_utf8("/usr/share/xml/iso-codes/iso_639.xml") as f:
dom = minidom.parse(f).documentElement
for node in dom.getElementsByTagName("iso_639_entry"):
lang_code = self._get_attr(node, "iso_639_1_code")
if not lang_code:
lang_code = self._get_attr(node, "iso_639_2T_code")
lang_name = self._get_attr(node, "name", "")
if lang_code and lang_name:
self._languages[lang_code] = lang_name
def _read_countries(self):
with open_utf8("/usr/share/xml/iso-codes/iso_3166.xml") as f:
dom = minidom.parse(f).documentElement
for node in dom.getElementsByTagName("iso_3166_entry"):
country_code = self._get_attr(node, "alpha_2_code")
country_name = self._get_attr(node, "name")
if country_code and country_name:
self._countries[country_code.upper()] = country_name
@staticmethod
def _get_attr(node, name, default = ""):
attr = node.attributes.get(name)
if attr:
return attr.value
return ""
|