Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/gt-fastapi/src/gt_fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""FastAPI integration for General Translation."""

from gt_i18n import declare_static, declare_var, decode_vars, derive, get_default_locale, get_locale, get_locales, t
from gt_i18n import declare_static, declare_var, decode_vars, derive, get_default_locale, get_locale, get_locales, get_version_id, t

from gt_fastapi._setup import initialize_gt

Expand All @@ -14,4 +14,5 @@
"get_locale",
"get_locales",
"get_default_locale",
"get_version_id",
]
3 changes: 3 additions & 0 deletions packages/gt-fastapi/src/gt_fastapi/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ def initialize_gt(
resolved_cache_url = cache_url or file_config.get("cache_url")
resolved_custom_mapping = custom_mapping or file_config.get("custom_mapping")

resolved_version_id = file_config.get("version_id")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 version_id not passable as a direct parameter to initialize_gt()

resolved_version_id is read only from the config file — there is no version_id keyword argument on initialize_gt(). Every other configuration value (project_id, cache_url, default_locale, locales, custom_mapping) follows the pattern of accepting both a function argument (which takes precedence) and a file-config fallback. Users who want to set version_id programmatically — e.g. in tests or in apps that don't use gt.config.json — have no way to do so.

The same issue exists in packages/gt-flask/src/gt_flask/_setup.py at line 59.

Suggested fix (FastAPI — Flask is identical):

def initialize_gt(
    app: Any,
    *,
    default_locale: str | None = None,
    locales: list[str] | None = None,
    custom_mapping: CustomMapping | None = None,
    project_id: str | None = None,
    cache_url: str | None = None,
    version_id: str | None = None,   # ← add
    get_locale: Callable[..., str] | None = None,
    load_translations: Callable[[str], dict[str, str]] | None = None,
    eager_loading: bool = True,
    config_path: str | None = None,
    load_config: Callable[[str | None], GTConfig] | None = None,
) -> I18nManager:
    ...
    resolved_version_id = version_id or file_config.get("version_id")
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/gt-fastapi/src/gt_fastapi/_setup.py
Line: 59

Comment:
**`version_id` not passable as a direct parameter to `initialize_gt()`**

`resolved_version_id` is read only from the config file — there is no `version_id` keyword argument on `initialize_gt()`. Every other configuration value (`project_id`, `cache_url`, `default_locale`, `locales`, `custom_mapping`) follows the pattern of accepting both a function argument (which takes precedence) and a file-config fallback. Users who want to set `version_id` programmatically — e.g. in tests or in apps that don't use `gt.config.json` — have no way to do so.

The same issue exists in `packages/gt-flask/src/gt_flask/_setup.py` at line 59.

Suggested fix (FastAPI — Flask is identical):

```
def initialize_gt(
    app: Any,
    *,
    default_locale: str | None = None,
    locales: list[str] | None = None,
    custom_mapping: CustomMapping | None = None,
    project_id: str | None = None,
    cache_url: str | None = None,
    version_id: str | None = None,   # ← add
    get_locale: Callable[..., str] | None = None,
    load_translations: Callable[[str], dict[str, str]] | None = None,
    eager_loading: bool = True,
    config_path: str | None = None,
    load_config: Callable[[str | None], GTConfig] | None = None,
) -> I18nManager:
    ...
    resolved_version_id = version_id or file_config.get("version_id")
```

How can I resolve this? If you propose a fix, please make it concise.


manager = I18nManager(
default_locale=resolved_default_locale,
locales=resolved_locales,
custom_mapping=resolved_custom_mapping,
project_id=resolved_project_id,
cache_url=resolved_cache_url,
load_translations=load_translations,
version_id=resolved_version_id,
)
set_i18n_manager(manager)

Expand Down
3 changes: 2 additions & 1 deletion packages/gt-flask/src/gt_flask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Flask integration for General Translation."""

from gt_i18n import declare_static, declare_var, decode_vars, derive, get_default_locale, get_locale, get_locales, t
from gt_i18n import declare_static, declare_var, decode_vars, derive, get_default_locale, get_locale, get_locales, get_version_id, t

from gt_flask._setup import initialize_gt

Expand All @@ -14,4 +14,5 @@
"get_locale",
"get_locales",
"get_default_locale",
"get_version_id",
]
3 changes: 3 additions & 0 deletions packages/gt-flask/src/gt_flask/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ def initialize_gt(
resolved_cache_url = cache_url or file_config.get("cache_url")
resolved_custom_mapping = custom_mapping or file_config.get("custom_mapping")

resolved_version_id = file_config.get("version_id")

manager = I18nManager(
default_locale=resolved_default_locale,
locales=resolved_locales,
custom_mapping=resolved_custom_mapping,
project_id=resolved_project_id,
cache_url=resolved_cache_url,
load_translations=load_translations,
version_id=resolved_version_id,
)
set_i18n_manager(manager)

Expand Down
3 changes: 3 additions & 0 deletions packages/gt-i18n/src/gt_i18n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get_locale,
get_locales,
)
from gt_i18n.helpers._version_id import get_version_id
from gt_i18n.i18n_manager import (
ContextVarStorageAdapter,
I18nManager,
Expand Down Expand Up @@ -49,6 +50,8 @@
"get_locale",
"get_locales",
"get_default_locale",
# Version
"get_version_id",
# Derive / variable helpers
"declare_var",
"derive",
Expand Down
7 changes: 7 additions & 0 deletions packages/gt-i18n/src/gt_i18n/helpers/_version_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from gt_i18n.i18n_manager._singleton import get_i18n_manager


def get_version_id() -> str | None:
"""Get the version ID for the current source, if set."""
manager = get_i18n_manager()
return manager.get_version_id()
6 changes: 6 additions & 0 deletions packages/gt-i18n/src/gt_i18n/i18n_manager/_i18n_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def __init__(
store_adapter: StorageAdapter | None = None,
load_translations: TranslationsLoader | None = None,
cache_expiry_time: int = 60_000,
version_id: str | None = None,
) -> None:
self._version_id = version_id
self._default_locale = default_locale
locales_set: set[str] = {default_locale, *(locales or [])}
self._locales = list(locales_set)
Expand Down Expand Up @@ -82,6 +84,10 @@ def get_gt_instance(self) -> GT:
custom_mapping=self._custom_mapping,
)

def get_version_id(self) -> str | None:
"""Get the version ID for the current source, if set."""
return self._version_id

def get_locales(self) -> list[str]:
return list(self._locales)

Expand Down
2 changes: 2 additions & 0 deletions packages/gt-i18n/src/gt_i18n/internal/_load_gt_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cacheUrl": "cache_url",
"runtimeUrl": "runtime_url",
"customMapping": "custom_mapping",
"_versionId": "version_id",
}


Expand All @@ -26,6 +27,7 @@ class GTConfig(TypedDict, total=False):
cache_url: str
runtime_url: str
custom_mapping: CustomMapping
version_id: str


def load_gt_config(config_path: str | None = None) -> GTConfig:
Expand Down
Loading