Skip to content

Commit ae67bd9

Browse files
chore: update examples to latest API conventions
- Remove redundant config params (read from gt.config.json) - Simplify load_translations helpers - Fix Flask eager loading bug: use resolved_locales instead of locales param - Clean up docstrings and imports - All four examples tested and working
1 parent 7011046 commit ae67bd9

5 files changed

Lines changed: 20 additions & 54 deletions

File tree

‎examples/fastapi-eager/app.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""FastAPI example with eager translation loading.
22
3-
Translations are stored in _gt/<locale>.json and loaded at startup.
4-
Configuration is read automatically from gt.config.json in the CWD.
3+
Translations are loaded from _gt/<locale>.json at startup.
4+
Configuration is read from gt.config.json.
55
Run: uv run uvicorn app:app --port 8000
66
"""
77

@@ -17,19 +17,14 @@
1717

1818

1919
def load_translations(locale: str) -> dict[str, str]:
20-
"""Load translations from _gt/<locale>.json."""
2120
path = GT_DIR / f"{locale}.json"
2221
if path.exists():
2322
with open(path) as f:
2423
return json.load(f)
2524
return {}
2625

2726

28-
initialize_gt(
29-
app,
30-
load_translations=load_translations,
31-
eager_loading=True,
32-
)
27+
initialize_gt(app, load_translations=load_translations)
3328

3429

3530
@app.get("/")

‎examples/fastapi-lazy/app.py‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""FastAPI example with lazy translation loading.
22
3-
Translations are stored in _gt/<locale>.json and loaded on first request per locale.
4-
Configuration is read automatically from gt.config.json in the CWD.
3+
Translations are loaded from _gt/<locale>.json on first request per locale.
4+
Configuration is read from gt.config.json.
55
Run: uv run uvicorn app:app --port 8001
66
"""
77

@@ -17,34 +17,24 @@
1717

1818

1919
async def load_translations(locale: str) -> dict[str, str]:
20-
"""Load translations from _gt/<locale>.json."""
2120
path = GT_DIR / f"{locale}.json"
2221
if path.exists():
2322
with open(path) as f:
2423
return json.load(f)
2524
return {}
2625

2726

28-
manager = initialize_gt(
29-
app,
30-
load_translations=load_translations,
31-
eager_loading=False,
32-
)
27+
manager = initialize_gt(app, load_translations=load_translations, eager_loading=False)
3328

3429

35-
async def _ensure_translations(request: Request) -> None:
36-
"""Load translations for the current locale if not already cached.
37-
38-
t() only reads from cache (get_translations_sync), so we must
39-
explicitly trigger a load for the current locale before t() runs.
40-
"""
30+
async def ensure_translations(request: Request) -> None:
31+
"""Load translations for the request locale before t() runs."""
4132
locale = manager.get_locale()
4233
if manager.requires_translation(locale):
4334
await manager.get_translations(locale)
4435

4536

46-
# Add dependency to all routes — runs inside route context after middleware
47-
app.router.dependencies = [Depends(_ensure_translations)]
37+
app.router.dependencies = [Depends(ensure_translations)]
4838

4939

5040
@app.get("/")

‎examples/flask-eager/app.py‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Flask example with eager translation loading.
22
3-
Translations are stored in _gt/<locale>.json and loaded at startup.
4-
Configuration is read automatically from gt.config.json in the CWD.
3+
Translations are loaded from _gt/<locale>.json at startup.
4+
Configuration is read from gt.config.json.
55
Run: uv run python app.py (serves on port 5050)
66
"""
77

88
import json
99
from pathlib import Path
1010

11-
from flask import Flask
11+
from flask import Flask, request
1212
from gt_flask import initialize_gt, t
1313

1414
app = Flask(__name__)
@@ -17,19 +17,14 @@
1717

1818

1919
def load_translations(locale: str) -> dict[str, str]:
20-
"""Load translations from _gt/<locale>.json."""
2120
path = GT_DIR / f"{locale}.json"
2221
if path.exists():
2322
with open(path) as f:
2423
return json.load(f)
2524
return {}
2625

2726

28-
initialize_gt(
29-
app,
30-
load_translations=load_translations,
31-
eager_loading=True,
32-
)
27+
initialize_gt(app, load_translations=load_translations)
3328

3429

3530
@app.get("/")
@@ -39,8 +34,6 @@ def index() -> dict[str, str]:
3934

4035
@app.get("/greet")
4136
def greet() -> dict[str, str]:
42-
from flask import request
43-
4437
name = request.args.get("name", "World")
4538
return {"message": t("Hello, {name}!", name=name)}
4639

‎examples/flask-lazy/app.py‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"""Flask example with lazy translation loading.
22
3-
Translations are stored in _gt/<locale>.json and loaded on first request per locale.
4-
Configuration is read automatically from gt.config.json in the CWD.
3+
Translations are loaded from _gt/<locale>.json on first request per locale.
4+
Configuration is read from gt.config.json.
55
Run: uv run python app.py (serves on port 5051)
66
"""
77

88
import asyncio
99
import json
1010
from pathlib import Path
1111

12-
from flask import Flask
12+
from flask import Flask, request
1313
from gt_flask import initialize_gt, t
1414

1515
app = Flask(__name__)
@@ -18,29 +18,19 @@
1818

1919

2020
def load_translations(locale: str) -> dict[str, str]:
21-
"""Load translations from _gt/<locale>.json."""
2221
path = GT_DIR / f"{locale}.json"
2322
if path.exists():
2423
with open(path) as f:
2524
return json.load(f)
2625
return {}
2726

2827

29-
manager = initialize_gt(
30-
app,
31-
load_translations=load_translations,
32-
eager_loading=False,
33-
)
28+
manager = initialize_gt(app, load_translations=load_translations, eager_loading=False)
3429

3530

3631
@app.before_request
37-
def _ensure_translations() -> None:
38-
"""Load translations for the current locale if not already cached.
39-
40-
Registered after initialize_gt(), so this runs after the locale is set.
41-
t() only reads from cache (get_translations_sync), so we must
42-
explicitly trigger a load for the current locale before t() runs.
43-
"""
32+
def ensure_translations() -> None:
33+
"""Load translations for the request locale before t() runs."""
4434
locale = manager.get_locale()
4535
if manager.requires_translation(locale):
4636
asyncio.run(manager.get_translations(locale))
@@ -53,8 +43,6 @@ def index() -> dict[str, str]:
5343

5444
@app.get("/greet")
5545
def greet() -> dict[str, str]:
56-
from flask import request
57-
5846
name = request.args.get("name", "World")
5947
return {"message": t("Hello, {name}!", name=name)}
6048

‎packages/gt-flask/src/gt_flask/_setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def initialize_gt(
7171
)
7272
set_i18n_manager(manager)
7373

74-
if eager_loading and locales:
74+
if eager_loading and resolved_locales:
7575
asyncio.run(manager.load_all_translations())
7676

7777
@app.before_request

0 commit comments

Comments
 (0)