def extract_language_options(self) -> None: locale_path = "{}/locale".format(settings.STATIC_ROOT) output_path = "{}/language_options.json".format(locale_path) data = {'languages': []} # type: Dict[str, List[Dict[str, Any]]] try: locales = self.get_locales() except CalledProcessError: # In case we are not under a Git repo, fallback to getting the # locales using listdir(). locales = os.listdir(locale_path) locales.append('en') locales = list(set(locales)) for locale in locales: if locale == 'en': data['languages'].append({ 'name': 'English', 'name_local': 'English', 'code': 'en', 'locale': 'en', }) continue lc_messages_path = os.path.join(locale_path, locale, 'LC_MESSAGES') if not os.path.exists(lc_messages_path): # Not a locale. continue info = {} # type: Dict[str, Any] code = to_language(locale) percentage = self.get_translation_percentage(locale_path, locale) try: name = LANG_INFO[code]['name'] name_local = LANG_INFO[code]['name_local'] except KeyError: # Fallback to getting the name from PO file. filename = self.get_po_filename(locale_path, locale) name = self.get_name_from_po_file(filename, locale) name_local = with_language(name, code) info['name'] = name info['name_local'] = name_local info['code'] = code info['locale'] = locale info['percent_translated'] = percentage data['languages'].append(info) with open(output_path, 'w') as writer: ujson.dump(data, writer, indent=2) writer.write('\n')
def create_language_name_map(self): # type: () -> None join = os.path.join static_root = settings.STATIC_ROOT path = join(static_root, 'locale', 'language_options.json') output_path = join(static_root, 'locale', 'language_name_map.json') with open(path, 'r') as reader: languages = ujson.load(reader) lang_list = [] for lang_info in languages['languages']: name = lang_info['name'] lang_info['name'] = with_language(name, lang_info['code']) lang_list.append(lang_info) lang_list.sort(key=lambda lang: lang['name']) with open(output_path, 'w') as output_file: ujson.dump({'name_map': lang_list}, output_file, indent=4)