def country_name_for_isocode(iso_code, lang=ICELANDIC_LANG_ISOCODE): """ Return country name for an ISO 3166-1 alpha-2 code. """ assert len(iso_code) == 2 assert len(lang) == 2 iso_code = iso_code.upper() lang = lang.lower() if lang not in available_languages(): return None countries = dict(countries_for_language(lang)) return countries.get(iso_code)
def isocode_for_country_name(country_name, lang=ICELANDIC_LANG_ISOCODE): """ Return the ISO 3166-1 alpha-2 code for a country name in the specified language (two-char ISO 639-1) """ assert len(lang) == 2 lang = lang.lower() if lang not in available_languages(): return None countries = countries_for_language(lang) # This is cached by module for iso_code, name in countries: if name == country_name: return iso_code if lang in COUNTRY_NAME_TO_ISOCODE_ADDITIONS: return COUNTRY_NAME_TO_ISOCODE_ADDITIONS[lang].get(country_name) return None
def getcountries(): countries = [] for language in available_languages(): countries.append(dict(countries_for_language(language))) dictionary = dict([(key, []) for key in countries[96]]) #7 96 204 263 for country in dictionary: dictionary[country].append(countries[7][country]) dictionary[country].append(countries[96][country]) dictionary[country].append(countries[204][country]) dictionary[country].append(countries[263][country]) dictionary[country] = list(set(dictionary[country])) return (dictionary)
def isocode_for_country_name(country_name, lang=ICELANDIC_LANG_ISOCODE): """ Return the ISO 3166-1 alpha-2 code for a country name in the specified language (two-char ISO 639-1). """ assert len(lang) == 2 lang = lang.lower() if lang not in available_languages(): return None # Hardcoded mappings take precedence if lang in COUNTRY_NAME_TO_ISOCODE_ADDITIONS: if country_name in COUNTRY_NAME_TO_ISOCODE_ADDITIONS[lang]: return COUNTRY_NAME_TO_ISOCODE_ADDITIONS[lang][country_name] countries = countries_for_language(lang) # This is cached by module uc_cn = capitalize_placename(country_name) for iso_code, name in countries: if name == country_name or name == uc_cn: return iso_code return None
def languages(self): return available_languages()
Using the country_list library create a list of all countries in many different languages (~620). Store them in a disk. (ONE TIME EXECUTION) ''' import sys, os from country_list import available_languages from country_list import countries_for_language out_dir = sys.argv[1] #create a dict for each language. for each language key load #the country names in that language as a list and store. countries = {} for lang in available_languages(): countries[lang] = [ v for k, v in dict(countries_for_language(lang)).items() ] #write country names list in a separate file for each language for lang, countrynames in countries.items(): with open(os.path.join(out_dir, lang + ".txt"), "w") as out_f: for countryname in countrynames: out_f.write(countryname + "\n") #write 2 letter country codes for each language as well with open(os.path.join(out_dir, "country_codes.txt"), "w") as out_f: for country_code in dict(countries_for_language('en')).keys(): out_f.write(country_code + "\n")
def test_available_languages(self): self.assertEqual( len(country_list.available_languages()), 628, "Languages have been added/removed", )