diff --git a/examples/fastapi-eager/_gt/es.json b/examples/fastapi-eager/_gt/es.json new file mode 100644 index 0000000..84a3870 --- /dev/null +++ b/examples/fastapi-eager/_gt/es.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Hola, mundo!", + "9b323e35e1a80c51": "Hola, {name}!" +} diff --git a/examples/fastapi-eager/_gt/fr.json b/examples/fastapi-eager/_gt/fr.json new file mode 100644 index 0000000..fba7e6d --- /dev/null +++ b/examples/fastapi-eager/_gt/fr.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Bonjour, le monde!", + "9b323e35e1a80c51": "Bonjour, {name}!" +} diff --git a/examples/fastapi-eager/app.py b/examples/fastapi-eager/app.py index 8a1fae5..b636079 100644 --- a/examples/fastapi-eager/app.py +++ b/examples/fastapi-eager/app.py @@ -1,39 +1,38 @@ """FastAPI example with eager translation loading. -Translations are loaded at startup for all configured locales. +Translations are stored in _gt/.json and loaded at startup. +Configuration is read from gt.config.json. Run: uv run uvicorn app:app --port 8000 """ +import json +from pathlib import Path + from fastapi import FastAPI from gt_fastapi import initialize_gt, t app = FastAPI(title="FastAPI Eager Example") -# Pre-built translation dictionaries keyed by hash. -# hash_message("Hello, world!") -> "8042e0a3d395c1fb" -# hash_message("Hello, {name}!") -> "9b323e35e1a80c51" -TRANSLATIONS: dict[str, dict[str, str]] = { - "es": { - "8042e0a3d395c1fb": "Hola, mundo!", - "9b323e35e1a80c51": "Hola, {name}!", - }, - "fr": { - "8042e0a3d395c1fb": "Bonjour, le monde!", - "9b323e35e1a80c51": "Bonjour, {name}!", - }, -} +BASE_DIR = Path(__file__).parent +GT_DIR = BASE_DIR / "_gt" + +with open(BASE_DIR / "gt.config.json") as f: + config = json.load(f) def load_translations(locale: str) -> dict[str, str]: - """Return translations for a locale from the in-memory dictionary.""" - print(f"[eager] Loading translations for '{locale}'") - return TRANSLATIONS.get(locale, {}) + """Load translations from _gt/.json.""" + path = GT_DIR / f"{locale}.json" + if path.exists(): + with open(path) as f: + return json.load(f) + return {} initialize_gt( app, - default_locale="en", - locales=["en", "es", "fr"], + default_locale=config.get("defaultLocale", "en"), + locales=config.get("locales"), load_translations=load_translations, eager_loading=True, ) diff --git a/examples/fastapi-eager/gt.config.json b/examples/fastapi-eager/gt.config.json new file mode 100644 index 0000000..9940de2 --- /dev/null +++ b/examples/fastapi-eager/gt.config.json @@ -0,0 +1,4 @@ +{ + "defaultLocale": "en", + "locales": ["es", "fr"] +} diff --git a/examples/fastapi-lazy/_gt/es.json b/examples/fastapi-lazy/_gt/es.json new file mode 100644 index 0000000..84a3870 --- /dev/null +++ b/examples/fastapi-lazy/_gt/es.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Hola, mundo!", + "9b323e35e1a80c51": "Hola, {name}!" +} diff --git a/examples/fastapi-lazy/_gt/fr.json b/examples/fastapi-lazy/_gt/fr.json new file mode 100644 index 0000000..fba7e6d --- /dev/null +++ b/examples/fastapi-lazy/_gt/fr.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Bonjour, le monde!", + "9b323e35e1a80c51": "Bonjour, {name}!" +} diff --git a/examples/fastapi-lazy/app.py b/examples/fastapi-lazy/app.py index 64d91c3..0f95651 100644 --- a/examples/fastapi-lazy/app.py +++ b/examples/fastapi-lazy/app.py @@ -1,36 +1,38 @@ """FastAPI example with lazy translation loading. -Translations are loaded on first request per locale, not at startup. +Translations are stored in _gt/.json and loaded on first request per locale. +Configuration is read from gt.config.json. Run: uv run uvicorn app:app --port 8001 """ +import json +from pathlib import Path + from fastapi import Depends, FastAPI, Request from gt_fastapi import initialize_gt, t app = FastAPI(title="FastAPI Lazy Example") -TRANSLATIONS: dict[str, dict[str, str]] = { - "es": { - "8042e0a3d395c1fb": "Hola, mundo!", - "9b323e35e1a80c51": "Hola, {name}!", - }, - "fr": { - "8042e0a3d395c1fb": "Bonjour, le monde!", - "9b323e35e1a80c51": "Bonjour, {name}!", - }, -} +BASE_DIR = Path(__file__).parent +GT_DIR = BASE_DIR / "_gt" + +with open(BASE_DIR / "gt.config.json") as f: + config = json.load(f) async def load_translations(locale: str) -> dict[str, str]: - """Simulate loading translations from a remote source.""" - print(f"[lazy] Loading translations for '{locale}'") - return TRANSLATIONS.get(locale, {}) + """Load translations from _gt/.json.""" + path = GT_DIR / f"{locale}.json" + if path.exists(): + with open(path) as f: + return json.load(f) + return {} manager = initialize_gt( app, - default_locale="en", - locales=["en", "es", "fr"], + default_locale=config.get("defaultLocale", "en"), + locales=config.get("locales"), load_translations=load_translations, eager_loading=False, ) diff --git a/examples/fastapi-lazy/gt.config.json b/examples/fastapi-lazy/gt.config.json new file mode 100644 index 0000000..9940de2 --- /dev/null +++ b/examples/fastapi-lazy/gt.config.json @@ -0,0 +1,4 @@ +{ + "defaultLocale": "en", + "locales": ["es", "fr"] +} diff --git a/examples/flask-eager/_gt/es.json b/examples/flask-eager/_gt/es.json new file mode 100644 index 0000000..84a3870 --- /dev/null +++ b/examples/flask-eager/_gt/es.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Hola, mundo!", + "9b323e35e1a80c51": "Hola, {name}!" +} diff --git a/examples/flask-eager/_gt/fr.json b/examples/flask-eager/_gt/fr.json new file mode 100644 index 0000000..fba7e6d --- /dev/null +++ b/examples/flask-eager/_gt/fr.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Bonjour, le monde!", + "9b323e35e1a80c51": "Bonjour, {name}!" +} diff --git a/examples/flask-eager/app.py b/examples/flask-eager/app.py index bfe8948..4853e27 100644 --- a/examples/flask-eager/app.py +++ b/examples/flask-eager/app.py @@ -1,36 +1,38 @@ """Flask example with eager translation loading. -Translations are loaded at startup for all configured locales. +Translations are stored in _gt/.json and loaded at startup. +Configuration is read from gt.config.json. Run: uv run python app.py (serves on port 5050) """ +import json +from pathlib import Path + from flask import Flask from gt_flask import initialize_gt, t app = Flask(__name__) -TRANSLATIONS: dict[str, dict[str, str]] = { - "es": { - "8042e0a3d395c1fb": "Hola, mundo!", - "9b323e35e1a80c51": "Hola, {name}!", - }, - "fr": { - "8042e0a3d395c1fb": "Bonjour, le monde!", - "9b323e35e1a80c51": "Bonjour, {name}!", - }, -} +BASE_DIR = Path(__file__).parent +GT_DIR = BASE_DIR / "_gt" + +with open(BASE_DIR / "gt.config.json") as f: + config = json.load(f) def load_translations(locale: str) -> dict[str, str]: - """Return translations for a locale from the in-memory dictionary.""" - print(f"[eager] Loading translations for '{locale}'") - return TRANSLATIONS.get(locale, {}) + """Load translations from _gt/.json.""" + path = GT_DIR / f"{locale}.json" + if path.exists(): + with open(path) as f: + return json.load(f) + return {} initialize_gt( app, - default_locale="en", - locales=["en", "es", "fr"], + default_locale=config.get("defaultLocale", "en"), + locales=config.get("locales"), load_translations=load_translations, eager_loading=True, ) diff --git a/examples/flask-eager/gt.config.json b/examples/flask-eager/gt.config.json new file mode 100644 index 0000000..9940de2 --- /dev/null +++ b/examples/flask-eager/gt.config.json @@ -0,0 +1,4 @@ +{ + "defaultLocale": "en", + "locales": ["es", "fr"] +} diff --git a/examples/flask-lazy/_gt/es.json b/examples/flask-lazy/_gt/es.json new file mode 100644 index 0000000..84a3870 --- /dev/null +++ b/examples/flask-lazy/_gt/es.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Hola, mundo!", + "9b323e35e1a80c51": "Hola, {name}!" +} diff --git a/examples/flask-lazy/_gt/fr.json b/examples/flask-lazy/_gt/fr.json new file mode 100644 index 0000000..fba7e6d --- /dev/null +++ b/examples/flask-lazy/_gt/fr.json @@ -0,0 +1,4 @@ +{ + "8042e0a3d395c1fb": "Bonjour, le monde!", + "9b323e35e1a80c51": "Bonjour, {name}!" +} diff --git a/examples/flask-lazy/app.py b/examples/flask-lazy/app.py index 4f87f9d..eae9192 100644 --- a/examples/flask-lazy/app.py +++ b/examples/flask-lazy/app.py @@ -1,38 +1,39 @@ """Flask example with lazy translation loading. -Translations are loaded on first request per locale, not at startup. +Translations are stored in _gt/.json and loaded on first request per locale. +Configuration is read from gt.config.json. Run: uv run python app.py (serves on port 5051) """ import asyncio +import json +from pathlib import Path from flask import Flask from gt_flask import initialize_gt, t app = Flask(__name__) -TRANSLATIONS: dict[str, dict[str, str]] = { - "es": { - "8042e0a3d395c1fb": "Hola, mundo!", - "9b323e35e1a80c51": "Hola, {name}!", - }, - "fr": { - "8042e0a3d395c1fb": "Bonjour, le monde!", - "9b323e35e1a80c51": "Bonjour, {name}!", - }, -} +BASE_DIR = Path(__file__).parent +GT_DIR = BASE_DIR / "_gt" + +with open(BASE_DIR / "gt.config.json") as f: + config = json.load(f) def load_translations(locale: str) -> dict[str, str]: - """Simulate loading translations from a remote source.""" - print(f"[lazy] Loading translations for '{locale}'") - return TRANSLATIONS.get(locale, {}) + """Load translations from _gt/.json.""" + path = GT_DIR / f"{locale}.json" + if path.exists(): + with open(path) as f: + return json.load(f) + return {} manager = initialize_gt( app, - default_locale="en", - locales=["en", "es", "fr"], + default_locale=config.get("defaultLocale", "en"), + locales=config.get("locales"), load_translations=load_translations, eager_loading=False, ) diff --git a/examples/flask-lazy/gt.config.json b/examples/flask-lazy/gt.config.json new file mode 100644 index 0000000..9940de2 --- /dev/null +++ b/examples/flask-lazy/gt.config.json @@ -0,0 +1,4 @@ +{ + "defaultLocale": "en", + "locales": ["es", "fr"] +}