Skip to content

refactor(gt-i18n): registration interface#18

Merged
ErnestM1234 merged 5 commits intomainfrom
e/gt-i18n/refactor-args
Mar 8, 2026
Merged

refactor(gt-i18n): registration interface#18
ErnestM1234 merged 5 commits intomainfrom
e/gt-i18n/refactor-args

Conversation

@ErnestM1234
Copy link
Contributor

@ErnestM1234 ErnestM1234 commented Mar 8, 2026

for string registration, move arguments from a dictionary to kwargs and convert sugar syntax to underscores

Greptile Summary

This PR refactors the gt-i18n string registration interface by renaming all $-prefixed GT reserved keys to underscore-prefixed equivalents ($context_context, $id_id, $max_chars_max_chars, $_source__source, $_hash__hash, $_fallback__fallback), making kwargs valid Python identifiers. It also addresses previous review feedback by replacing the brittle prefix-based variable filtering with an explicit _GT_RESERVED_KEYS blocklist, and by extracting duplicated test helper code into a shared helpers.py module.

Key changes:

  • _extract_variables.py: New _GT_RESERVED_KEYS frozenset replaces startswith("$") filter — user variables with _ prefixes are no longer silently dropped
  • _msg.py, _t.py, _interpolate.py, _is_encoded.py: All internal key lookups updated to new naming convention
  • tests/helpers.py + tests/conftest.py: Shared test utilities extracted (resolving the duplicated-helper concern from the prior review)
  • Minor docstring inconsistency in _interpolate.py: step 1 still describes the old prefix-filtering behavior rather than the new explicit blocklist

Confidence Score: 5/5

  • This PR is safe to merge — it is a clean, consistent rename across the library with no logic changes and good test coverage.
  • All $-prefixed keys are replaced uniformly across production code and tests. The blocklist-based filtering in extract_variables is correct and complete (_context, _id, _max_chars, __hash, __source, __fallback all included). A new test explicitly verifies user _-prefixed vars are preserved. The only finding is a minor stale docstring on a single line in _interpolate.py.
  • No files require special attention beyond the minor docstring fix in _interpolate.py.

Important Files Changed

Filename Overview
packages/gt-i18n/src/gt_i18n/translation_functions/_extract_variables.py Replaced prefix-based filtering with an explicit _GT_RESERVED_KEYS blocklist, correctly addressing the previous review concern about user variables starting with _ being silently dropped.
packages/gt-i18n/src/gt_i18n/translation_functions/_interpolate.py Key references updated from $_fallback/$max_chars to __fallback/_max_chars. Minor docstring inconsistency on line 28 still says "filter out _-prefixed keys" but the actual logic now uses an explicit blocklist.
packages/gt-i18n/src/gt_i18n/translation_functions/_msg.py All $-prefixed keys ($_hash, $context, $id, $max_chars) correctly renamed to their _/__ equivalents. Encoding path and hash logic remain intact.
packages/gt-i18n/tests/helpers.py New shared test utility module with JS_TO_PY_KEY_MAP, convert_keys, and convert_encoded_string, directly resolving the duplicated-helper concern from the previous review.
packages/gt-i18n/tests/test_extract_variables.py Tests updated to reflect the new key naming and a new test (test_user_underscore_prefixed_keys_preserved) correctly validates that user variables with a _ prefix are no longer silently dropped.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["t(message, **kwargs)\n_context, _id, _max_chars"] --> B["hash_message()\nuses _context, _id, _max_chars"]
    A --> C{"translation found?"}
    C -->|yes| D["interpolate_message(translated,\n{**kwargs, __fallback: message})"]
    C -->|no| E["interpolate_message(message, kwargs)"]
    D --> F["extract_variables(options)\nblocks _GT_RESERVED_KEYS"]
    E --> F
    F --> G["user vars only\n(e.g. name, count, _custom_var ✓)"]
    F --> H["GT reserved keys removed\n_context, _id, _max_chars\n__hash, __source, __fallback"]
    D --> I{"format error?"}
    I -->|"__fallback present"| J["retry with source msg\n(__fallback=None)"]
    I -->|no fallback| K["return raw + _max_chars cutoff"]
    J --> L["interpolated result"]
    G --> L
Loading

Last reviewed commit: 1da7587

Greptile also left 1 inline comment on this PR.

@ErnestM1234 ErnestM1234 changed the title E/gt i18n/refactor args refactor(gt-i18n): registration interface Mar 8, 2026
@ErnestM1234 ErnestM1234 enabled auto-merge (squash) March 8, 2026 01:54
@pie575
Copy link

pie575 commented Mar 8, 2026

@greptileai review

1 similar comment
@pie575
Copy link

pie575 commented Mar 8, 2026

@greptileai review

@ErnestM1234
Copy link
Contributor Author

@greptileai review

idk if greptile is real bro

@ErnestM1234
Copy link
Contributor Author

@greptileai plz review

Copy link

@pie575 pie575 left a comment

Choose a reason for hiding this comment

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

Image

@ErnestM1234 ErnestM1234 merged commit 7a7ce0d into main Mar 8, 2026
7 checks passed
@ErnestM1234 ErnestM1234 deleted the e/gt-i18n/refactor-args branch March 8, 2026 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants