diff options
-rw-r--r-- | fietsboek/__init__.py | 2 | ||||
-rw-r--r-- | fietsboek/config.py | 3 | ||||
-rw-r--r-- | fietsboek/util.py | 21 | ||||
-rw-r--r-- | fietsboek/views/default.py | 6 |
4 files changed, 24 insertions, 8 deletions
diff --git a/fietsboek/__init__.py b/fietsboek/__init__.py index e208a61..fe6bf52 100644 --- a/fietsboek/__init__.py +++ b/fietsboek/__init__.py @@ -80,6 +80,8 @@ def main(_global_config, **settings): config.include('.models') config.scan() config.add_translation_dirs('fietsboek:locale/') + for pack in parsed_config.language_packs: + config.add_translation_dirs(f'{pack}:locale/') config.set_session_factory(my_session_factory) config.set_security_policy(SecurityPolicy()) config.set_csrf_storage_policy(CookieCSRFStoragePolicy()) diff --git a/fietsboek/config.py b/fietsboek/config.py index d2efe77..b8a2cfb 100644 --- a/fietsboek/config.py +++ b/fietsboek/config.py @@ -141,6 +141,9 @@ class Config(BaseModel): session_key: str """Session key.""" + language_packs: PyramidList = Field([], alias="fietsboek.language_packs") + """Additional language packs to load.""" + available_locales: PyramidList = ["en", "de"] """Available locales.""" diff --git a/fietsboek/util.py b/fietsboek/util.py index 71f5d16..6b2190b 100644 --- a/fietsboek/util.py +++ b/fietsboek/util.py @@ -284,7 +284,7 @@ def check_password_constraints(password, repeat_password=None): raise ValueError(_("password_constraint.length")) -def read_localized_resource(locale_name, path, raise_on_error=False): +def read_localized_resource(locale_name, path, locale_packages=None, raise_on_error=False): """Reads a localized resource. Localized resources are located in the ``fietsboek/locale/**`` directory. @@ -294,6 +294,9 @@ def read_localized_resource(locale_name, path, raise_on_error=False): :param locale_name: Name of the locale. :type locale_name: str + :param locale_packages: Names of packages in which locale data is searched. + By default, only built-in locales are searched. + :type locale_packages: list[str] :param raise_on_error: Raise an error instead of returning a placeholder. :type raise_on_error: bool :raises FileNotFoundError: If the path could not be found and @@ -307,13 +310,17 @@ def read_localized_resource(locale_name, path, raise_on_error=False): if "_" in locale_name: locales.append(locale_name.split("_", 1)[0]) + if locale_packages is None: + locale_packages = ['fietsboek'] + for locale in locales: - locale_dir = importlib_resources.files('fietsboek') / 'locale' / locale - resource_path = locale_dir / path - try: - return resource_path.read_text() - except (FileNotFoundError, ModuleNotFoundError, NotADirectoryError): - pass + for package in locale_packages: + locale_dir = importlib_resources.files(package) / 'locale' / locale + resource_path = locale_dir / path + try: + return resource_path.read_text() + except (FileNotFoundError, ModuleNotFoundError, NotADirectoryError): + pass if raise_on_error: raise FileNotFoundError(f"Resource {path!r} not found") return f"{locale_name}:{path}" diff --git a/fietsboek/views/default.py b/fietsboek/views/default.py index f4aaa8f..516aef7 100644 --- a/fietsboek/views/default.py +++ b/fietsboek/views/default.py @@ -40,7 +40,11 @@ def home(request): # Show the default home page locale = request.localizer.locale_name - content = util.read_localized_resource(locale, "html/home.html") + content = util.read_localized_resource( + locale, + "html/home.html", + locale_packages=request.config.language_packs, + ) return { 'home_content': content, } |