Put app strings into a YAML file for easy editing by product owner
This commit is contained in:
35
atst/utils/localization.py
Normal file
35
atst/utils/localization.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import yaml
|
||||
import os
|
||||
from functools import lru_cache
|
||||
from atst.utils import getattr_path
|
||||
|
||||
ENV = os.getenv("FLASK_ENV", "dev")
|
||||
|
||||
|
||||
class LocalizationInvalidKeyError(Exception):
|
||||
def __init__(self, key, variables):
|
||||
self.key = key
|
||||
self.variables = variables
|
||||
|
||||
def __str__(self):
|
||||
return "Requested {key} and variables {variables} with but an error occured".format(
|
||||
key=self.key, variables=self.variables
|
||||
)
|
||||
|
||||
|
||||
localizations_cache_size = 1 if ENV == "dev" else None
|
||||
|
||||
|
||||
@lru_cache(maxsize=localizations_cache_size)
|
||||
def load_cached_translations_file(file_name):
|
||||
return open(file_name).read()
|
||||
|
||||
|
||||
def translate(key, variables={}):
|
||||
translations = yaml.safe_load(load_cached_translations_file("translations.yaml"))
|
||||
value = getattr_path(translations, key)
|
||||
|
||||
if value == None:
|
||||
raise LocalizationInvalidKeyError(key, variables)
|
||||
|
||||
return value.format(**variables).replace("\n", "")
|
Reference in New Issue
Block a user