28 lines
681 B
Python
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
|