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

28 lines
681 B
Python
Raw Permalink Normal View History

2023-01-01 11:13:32 +00:00
from json import load
from os import path
from flask import request
2023-01-04 15:57:16 +00:00
2023-01-01 11:13:32 +00:00
CATALOG_CACHE = {}
def current_lang():
return request.accept_languages.best_match(["en", "fr"])
2023-01-04 14:43:20 +00:00
def i18n(string: str, *args: str) -> str:
2023-01-01 11:13:32 +00:00
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]:
2023-01-04 14:43:20 +00:00
return CATALOG_CACHE[lang][string] % args
2023-01-01 11:13:32 +00:00
2023-01-04 14:43:20 +00:00
return string % args