Put app strings into a YAML file for easy editing by product owner

This commit is contained in:
George Drummond
2018-12-13 15:02:52 -05:00
parent 8eecb62034
commit f806425d91
35 changed files with 845 additions and 246 deletions

View 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", "")