Skip to content

fix: reject language codes outside [A-Za-z0-9_-] - #7

Merged
muhammetsafak merged 2 commits into
mainfrom
fix/reject-unsafe-language-code
Nov 24, 2026
Merged

muhammetsafak merged 2 commits into
mainfrom
fix/reject-unsafe-language-code

Conversation

@muhammetsafak

Copy link
Copy Markdown
Contributor

Summary

  • setDefault() and change() now throw TranslatorException for an empty identifier or one containing anything other than letters, digits, _ and - (matched with \A...\z, so a trailing newline is rejected too).
  • The check runs before any state is changed and before any file is read, so a rejected code leaves the translator untouched.
  • Previously the identifier was joined into the path of a file that was required, so ../config/install could run PHP files outside the language directory.
  • README, docs/ (API reference, Exceptions, Quick Start, Recipes) and CHANGELOG.md describe the new behaviour.

Compatibility

Codes such as en, en_US and pt-BR keep working. A language pack whose name uses . or / must be renamed. No public signature changes.

Test plan

  • vendor/bin/phpunit (95 tests; the 50 new ones fail without the fix)
  • vendor/bin/phpstan analyse
  • vendor/bin/php-cs-fixer fix --dry-run --diff
  • composer validate --strict
  • wiki-sync.sh --check docs

setDefault() and change() joined the identifier into the path of a file that
was require'd, so a request-supplied value such as ../config/install could
run PHP files outside the language directory. Both now throw a
TranslatorException before any file is touched and leave the state unchanged.
The docs and the changelog describe the new behaviour.
setDefault() and change() assigned the active/default language before loading
the pack, so change('xx') left xx active after the exception. They now load
first. In directory mode a language is cached only after all its files have
loaded, so a broken file no longer leaves a half-loaded language that a later
call accepts silently. The interface docblocks list the invalid-code case.
@muhammetsafak
muhammetsafak merged commit dfd6548 into main Nov 24, 2026
18 checks passed
@muhammetsafak
muhammetsafak deleted the fix/reject-unsafe-language-code branch November 24, 2026 07:40
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.

1 participant