Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Arguments:
short_langs --- Array of strings. Each string is the short name of
a language. Should be 3 characters long (more should be fine as
well)
Returns:
A dictionnary: Keys are the short languages name, values are the
corresponding long languages names.
"""
long_langs = {}
for short_lang in short_langs:
try:
try:
country = pycountry.languages.get(terminology=short_lang[:3])
except KeyError:
country = pycountry.languages.get(bibliographic=short_lang[:3])
extra = None
if "_" in short_lang:
extra = short_lang.split("_")[1]
long_lang = country.name
if extra != None:
long_lang += " (%s)" % (extra)
long_langs[short_lang] = long_lang
except KeyError, exc:
print ("Warning: Long name not found for language '%s'."
% (short_lang))
print (" Exception was: %s" % (str(exc)))
print (" Will use short name as long name.")
long_langs[short_lang] = short_lang
return long_langs
def __determine_iso_639_3_key():
""" Determine the key needed for accessing ISO 639-3
language codes using pycountry.
"""
# Different version of pycountry seem to use different keys.
# Try a couple (Note: all ISO639-2T codes are ISO639-3 codes
# as well)
for key3 in ["alpha_3", "iso639_3_code", "terminology", "iso639_2T_code", ]:
try:
ret = pycountry.languages.get(**{key3: "deu"})
if ret is None:
continue
return key3
except KeyError:
continue
raise SystemExit("Could not determine pycountry iso_639_3 key")
return
document = self.create_document(foreign_id=foreign_id)
document.source_url = document.get('canonical_url')
document.title = document.get('title')
document.author = document.get('author')
document.file_name = os.path.basename(document.get('pdf_url'))
document.mime_type = 'application/pdf'
try:
created = parse(document.get('created_at'))
document.add_date(created.date().isoformat())
except:
pass
try:
lang = languages.get(iso639_3_code=document.get('language'))
document.add_language(lang.iso639_1_code)
except:
pass
self.emit_url(document, document.get('pdf_url'))
def execute(self, maybe_code):
if isinstance(maybe_code, dict):
maybe_code = maybe_code['#text']
# Force indices to populate
if not languages._is_loaded:
languages._load()
for kwarg in languages.indices.keys():
try:
return languages.get(**{kwarg: maybe_code}).iso639_3_code
except KeyError:
continue
return None
def find_language(code):
try:
return pycountry.languages.get(iso639_1_code=code)
except:
pass
try:
return pycountry.languages.get(name=titlecase(code))
except:
pass
raise CommandError("Unknown language code: {}".format(code))
def execute(self, maybe_code):
if isinstance(maybe_code, dict):
maybe_code = maybe_code['#text']
# Force indices to populate
if not languages._is_loaded:
languages._load()
for kwarg in languages.indices.keys():
try:
return languages.get(**{kwarg: maybe_code}).iso639_3_code
except KeyError:
continue
return None
def _get_languages(languages):
if languages is None or not len(languages):
languages = ['en']
supported = []
for lang in languages:
if lang is None or len(lang.strip()) not in [2, 3]:
continue
lang = lang.lower().strip()
if len(lang) == 2:
try:
c = pycountry.languages.get(iso639_1_code='de')
lang = c.iso639_3_code
except KeyError:
continue
supported.append(lang)
return '+'.join(sorted(supported))
def get_languages_iso3(codes):
"""Turn (pre-set) ISO2 language codes into ISO3 codes."""
supported = []
for lang in ensure_list(codes):
if lang is None or len(lang.strip()) not in [2, 3]:
continue
lang = lang.lower().strip()
if len(lang) == 2:
try:
c = languages.get(alpha_2=lang)
lang = c.alpha_3
except KeyError as ke:
log.exception(ke)
continue
supported.append(lang)
# if not len(supported):
supported.append('eng')
return '+'.join(sorted(set(supported)))
def get_display_name(self, locale):
language_code, _, country_code = locale.partition('_')
term_code = languages.get(bibliographic=language_code).terminology
available_languages = dict(literal_eval(
(self.settings.get('available_languages', '[]'))))
try:
return Locale.parse(term_code).language_name
except UnknownLocaleError:
# Fallback value is the generated value in English or the code
return available_languages.get(locale, locale)