def extract_language_options(self) -> None: locale_path = f"{settings.DEPLOY_ROOT}/locale" output_path = f"{locale_path}/language_options.json" data: Dict[str, List[Dict[str, Any]]] = {"languages": []} 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 sorted(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: 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) with override_language(code): name_local = _(name) 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: json.dump(data, writer, indent=2, sort_keys=True) writer.write("\n")
def get_languages_from_locale_subdirectories(dir): current_languages = [] language_data_json = open(os.path.join(dir, "language-data.json")) languages = json.loads(language_data_json.read())["languages"] for locale_dir in os.listdir(dir): if os.path.isdir(os.path.join(dir, locale_dir)): for lang_code, lang_data in languages.items(): autonym = lang_data[-1] if to_language(locale_dir) == lang_code: current_languages += [(lang_code, autonym)] return sorted(set(current_languages))
def post(self, request, *args, **kwargs): if "locale" in request.POST: config = Config.load() config.locale = request.POST["locale"] config.save() asyncio.run(self.notify_config_update("nabd", "locale")) user_language = to_language(config.locale) translation.activate(user_language) request.LANGUAGE_CODE = translation.get_language() context = self.get_context() return render(request, self.template_name(), context=context)
def get(self, request, *args, **kwargs): user_locale = Config.load().locale user_language = to_language(user_locale) translation.activate(user_language) self.request.session[translation.LANGUAGE_SESSION_KEY] = user_language locales = self.get_locales() return render(request, NabWebView.template_name, context={ 'current_locale': user_locale, 'locales': locales })
def get_deeds_ux_pofiles(): deed_ux_pofiles = [] for locale_name in os.listdir(settings.DEEDS_UX_LOCALE_PATH): language_code = translation.to_language(locale_name) pofile_path = get_pofile_path( locale_or_legalcode="locale", language_code=language_code, translation_domain="django", ) if not os.path.isfile(pofile_path): # pragma: no cover continue deed_ux_pofiles.append([language_code, pofile_path]) deed_ux_pofiles.sort(key=lambda x: x[0]) return deed_ux_pofiles
def process_request(self, request): user_locale = Config.load().locale user_language = to_language(user_locale) translation.activate(user_language) request.LANGUAGE_CODE = translation.get_language()
def step_impl(context, language, locale): context.test.assertIn((translation.to_language(locale), language), settings.LANGUAGES)
def default_filename(locale, domain): from django.utils.translation import to_language return os.path.join(to_language(locale), '%s.js' % domain)