This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata2/translations/__init__.py
2023-06-04 17:53:29 +02:00

28 lines
681 B
Python

from json import load
from os import path
from flask import request
CATALOG_CACHE = {}
def current_lang():
return request.accept_languages.best_match(["en", "fr"])
def i18n(string: str, *args: str) -> str:
lang = current_lang()
if lang != "en" and lang not in CATALOG_CACHE:
catalog_file = f"{path.dirname(__file__)}/{lang}.json"
if path.exists(catalog_file):
with open(catalog_file) as catalog_json:
catalog = load(catalog_json)
CATALOG_CACHE[lang] = catalog
if lang in CATALOG_CACHE and string in CATALOG_CACHE[lang]:
return CATALOG_CACHE[lang][string] % args
return string % args