Skip to content

[6.x] Adjust translation method usage#14610

Merged
jasonvarga merged 6 commits into6.xfrom
fix-trans-imports
May 6, 2026
Merged

[6.x] Adjust translation method usage#14610
jasonvarga merged 6 commits into6.xfrom
fix-trans-imports

Conversation

@jasonvarga
Copy link
Copy Markdown
Member

@jasonvarga jasonvarga commented May 6, 2026

What & Why

Fixes a lang-file/title collision where a translation key resolving to an array (e.g. a lang-file group whose name collides with a string key) caused incorrect output. Statamic's Statamic\trans() wrapper detects this case and falls back to returning the key, but only if call sites actually route through the wrapper — Laravel's global trans(), __(), and trans_choice() do not have this protection.

This has happened many times in the past. We've fixed the specifically reported locations, but this keeps popping up, so this PR addresses it.

#2341, #4852, #6326, #7672, #14609
#5144, #6331, #9525

Changes

Helpers

  • Added a Statamic\trans_choice() namespaced helper (and Statamic::transChoice() static method) that mirrors the existing Statamic\trans() — returning the key when the lookup resolves to an array, otherwise delegating to Laravel's trans_choice() for normal pluralization.

Call-site imports

  • Added use function Statamic\trans as __; to every file using __() (240 files) — routes the alias through Statamic's wrapper.
  • Added use function Statamic\trans; to files calling trans() directly without the alias.
  • Added use function Statamic\trans_choice; to files calling trans_choice() directly.

PHP's use function rebinds unqualified calls at compile time, so no call-site rewrites were needed — just imports.

CI enforcement

  • Added scripts/check-trans-import.sh which scans src/**/*.php and resources/views/**/*.blade.php for unqualified __(), trans(), and trans_choice() calls and fails if the corresponding use function Statamic\… import is missing.
  • Wired into .github/workflows/code-style-lint.yml to run on every push.

Tests

  • Added tests for Statamic::trans and Statamic::transChoice covering the happy path and the array-collision fallback.

Closes #14609

jasonvarga and others added 2 commits May 6, 2026 13:43
Adds 'use function Statamic\trans as __' to all PHP and Blade files
that use __() for translations, preventing a lang-file/title collision
(see issue #14609). Also adds a CI check script to enforce this going
forward.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ll sites

Adds a Statamic\trans_choice helper mirroring Statamic\trans (returning
the key when the lookup resolves to an array), then imports trans/
trans_choice in files that call them directly without the __() alias.
The CI check script now also enforces these imports.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jasonvarga jasonvarga changed the title [6.x] Fix missing Statamic\trans imports across the codebase [6.x] Adjust translation method usage May 6, 2026
jasonvarga and others added 4 commits May 6, 2026 15:18
This reverts commit e141308.
Covers the happy-path translation/pluralization and the array-collision
fallback (returning the key when the lookup resolves to an array).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jasonvarga jasonvarga merged commit b4e4101 into 6.x May 6, 2026
18 checks passed
@jasonvarga jasonvarga deleted the fix-trans-imports branch May 6, 2026 19:44
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.

Lang file name conflicts with page title

1 participant