-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Handle missing translation strings using callback #49040
[10.x] Handle missing translation strings using callback #49040
Conversation
ab2c690
to
caf88c0
Compare
…ined callback to facilitate customising missing translation behaviour. Callback can return a string which is subsequently returned by the Translator get method.
426b4e7
to
5cb7336
Compare
This is a lovely feature, personally I'd like to use this to report missing translation string automatically |
return $key; | ||
} | ||
|
||
// Call user defined missing translation handler callback, prevent infitite loops. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - "infitite"->"infinite"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Call user defined missing translation handler callback, prevent infitite loops. | |
// Call user defined missing translation handler callback, prevent infinite loops. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in this PR, thank you.
…nslation callback itself tries to translation a missing translation.
5cb7336
to
9f8eb09
Compare
It is a very minor procedural point, but force-pushing commits is not generally considered the way to make minor incremental changes to code in a PR - a normal push is generally used. |
Congrats @DeanWunder for getting this PR accepted. 👍 |
Congrats again to @DeanWunder - this PR is now live in Laravel 10.33.0. |
[![Mend Renovate logo banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [laravel/framework](https://laravel.com) ([source](https://github.com/laravel/framework)) | `10.30.1` -> `10.33.0` | [![age](https://developer.mend.io/api/mc/badges/age/packagist/laravel%2fframework/10.33.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/packagist/laravel%2fframework/10.33.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/packagist/laravel%2fframework/10.30.1/10.33.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/packagist/laravel%2fframework/10.30.1/10.33.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>laravel/framework (laravel/framework)</summary> ### [`v10.33.0`](https://github.com/laravel/framework/blob/HEAD/CHANGELOG.md#v10330---2023-11-21) [Compare Source](https://github.com/laravel/framework/compare/v10.32.1...v10.33.0) - \[10.x] Fix wrong parameter passing and add these rules to dependent rules by [@​kayw-geek](https://github.com/kayw-geek) in [https://github.com/laravel/framework/pull/49008](https://github.com/laravel/framework/pull/49008) - \[10.x] Make Validator::getValue() public by [@​shinsenter](https://github.com/shinsenter) in [https://github.com/laravel/framework/pull/49007](https://github.com/laravel/framework/pull/49007) - \[10.x] Custom messages for `Password` validation rule by [@​rcknr](https://github.com/rcknr) in [https://github.com/laravel/framework/pull/48928](https://github.com/laravel/framework/pull/48928) - \[10.x] Round milliseconds in database seeder console output runtime by [@​SjorsO](https://github.com/SjorsO) in [https://github.com/laravel/framework/pull/49014](https://github.com/laravel/framework/pull/49014) - \[10.x] Add a `Number` utility class by [@​caendesilva](https://github.com/caendesilva) in [https://github.com/laravel/framework/pull/48845](https://github.com/laravel/framework/pull/48845) - \[10.x] Fix the replace() method in DefaultService class by [@​jonagoldman](https://github.com/jonagoldman) in [https://github.com/laravel/framework/pull/49022](https://github.com/laravel/framework/pull/49022) - \[10.x] Pass the property $validator as a parameter to the $callback Closure by [@​shinsenter](https://github.com/shinsenter) in [https://github.com/laravel/framework/pull/49015](https://github.com/laravel/framework/pull/49015) - \[10.x] Fix Cache DatabaseStore::add() error occur on Postgres within transaction by [@​xdevor](https://github.com/xdevor) in [https://github.com/laravel/framework/pull/49025](https://github.com/laravel/framework/pull/49025) - \[10.x] Support asserting against chained batches by [@​taylorotwell](https://github.com/taylorotwell) in [https://github.com/laravel/framework/pull/49003](https://github.com/laravel/framework/pull/49003) - \[10.x] Prevent DB `Cache::get()` occur race condition by [@​xdevor](https://github.com/xdevor) in [https://github.com/laravel/framework/pull/49031](https://github.com/laravel/framework/pull/49031) - \[10.x] Fix notifications being counted as sent without a "shouldSend" method by [@​joelwmale](https://github.com/joelwmale) in [https://github.com/laravel/framework/pull/49030](https://github.com/laravel/framework/pull/49030) - \[10.x] Fix tests failure on Windows by [@​hafezdivandari](https://github.com/hafezdivandari) in [https://github.com/laravel/framework/pull/49037](https://github.com/laravel/framework/pull/49037) - \[10.x] Add unless conditional on validation rules by [@​michaelnabil230](https://github.com/michaelnabil230) in [https://github.com/laravel/framework/pull/49048](https://github.com/laravel/framework/pull/49048) - \[10.x] Handle string based payloads that are not JSON or form data when creating PSR request instances by [@​timacdonald](https://github.com/timacdonald) in [https://github.com/laravel/framework/pull/49047](https://github.com/laravel/framework/pull/49047) - \[10.x] Fix directory separator CMD display on windows by [@​imanghafoori1](https://github.com/imanghafoori1) in [https://github.com/laravel/framework/pull/49045](https://github.com/laravel/framework/pull/49045) - \[10.x] Fix mapSpread doc by [@​timacdonald](https://github.com/timacdonald) in [https://github.com/laravel/framework/pull/48941](https://github.com/laravel/framework/pull/48941) - \[10.x] Tiny `Support\Collection` test fix - Unused data provider parameter by [@​stevebauman](https://github.com/stevebauman) in [https://github.com/laravel/framework/pull/49053](https://github.com/laravel/framework/pull/49053) - \[10.x] Feat: Add color_hex validation rule by [@​nikopeikrishvili](https://github.com/nikopeikrishvili) in [https://github.com/laravel/framework/pull/49056](https://github.com/laravel/framework/pull/49056) - \[10.x] Handle missing translation strings using callback by [@​DeanWunder](https://github.com/DeanWunder) in [https://github.com/laravel/framework/pull/49040](https://github.com/laravel/framework/pull/49040) - \[10.x] Add Str::transliterate to Stringable by [@​dwightwatson](https://github.com/dwightwatson) in [https://github.com/laravel/framework/pull/49065](https://github.com/laravel/framework/pull/49065) - Add Alpha Channel support to Hex validation rule by [@​ahinkle](https://github.com/ahinkle) in [https://github.com/laravel/framework/pull/49069](https://github.com/laravel/framework/pull/49069) ### [`v10.32.1`](https://github.com/laravel/framework/blob/HEAD/CHANGELOG.md#v10321---2023-11-14) [Compare Source](https://github.com/laravel/framework/compare/v10.32.0...v10.32.1) - \[10.x] Add `[@pushElseIf](https://github.com/pushElseIf)` and `[@pushElse](https://github.com/pushElse)` by [@​jasonmccreary](https://github.com/jasonmccreary) in [https://github.com/laravel/framework/pull/48990](https://github.com/laravel/framework/pull/48990) ### [`v10.32.0`](https://github.com/laravel/framework/blob/HEAD/CHANGELOG.md#v10320---2023-11-14) [Compare Source](https://github.com/laravel/framework/compare/v10.31.0...v10.32.0) - Update PendingRequest.php by [@​mattkingshott](https://github.com/mattkingshott) in [https://github.com/laravel/framework/pull/48939](https://github.com/laravel/framework/pull/48939) - \[10.x] Change array_key_exists with null coalescing assignment operator in FilesystemAdapter by [@​miladev95](https://github.com/miladev95) in [https://github.com/laravel/framework/pull/48943](https://github.com/laravel/framework/pull/48943) - \[10.x] Use container to resolve email validator class by [@​orkhanahmadov](https://github.com/orkhanahmadov) in [https://github.com/laravel/framework/pull/48942](https://github.com/laravel/framework/pull/48942) - \[10.x] Added `getGlobalMiddleware` method to HTTP Client Factory by [@​pascalbaljet](https://github.com/pascalbaljet) in [https://github.com/laravel/framework/pull/48950](https://github.com/laravel/framework/pull/48950) - \[10.x] Detect MySQL read-only mode error as a lost connection by [@​cosmastech](https://github.com/cosmastech) in [https://github.com/laravel/framework/pull/48937](https://github.com/laravel/framework/pull/48937) - \[10.x] Adds more implicit validation rules for `present` based on other fields by [@​diamondobama](https://github.com/diamondobama) in [https://github.com/laravel/framework/pull/48908](https://github.com/laravel/framework/pull/48908) - \[10.x] Refactor set_error_handler callback to use arrow function in `InteractsWithDeprecationHandling` by [@​miladev95](https://github.com/miladev95) in [https://github.com/laravel/framework/pull/48954](https://github.com/laravel/framework/pull/48954) - \[10.x] Test Improvements by [@​crynobone](https://github.com/crynobone) in [https://github.com/laravel/framework/pull/48962](https://github.com/laravel/framework/pull/48962) - Fix issue that prevents BladeCompiler to raise an exception when temporal compiled blade template is not found. by [@​juanparati](https://github.com/juanparati) in [https://github.com/laravel/framework/pull/48957](https://github.com/laravel/framework/pull/48957) - \[10.x] Fix how nested transaction callbacks are handled by [@​mateusjatenee](https://github.com/mateusjatenee) in [https://github.com/laravel/framework/pull/48859](https://github.com/laravel/framework/pull/48859) - \[10.x] Fixes Batch Callbacks not triggering if job timeout while in transaction by [@​crynobone](https://github.com/crynobone) in [https://github.com/laravel/framework/pull/48961](https://github.com/laravel/framework/pull/48961) - \[10.x] expressions in migration computations fail by [@​tpetry](https://github.com/tpetry) in [https://github.com/laravel/framework/pull/48976](https://github.com/laravel/framework/pull/48976) - \[10.x] Fixes Exception: Cannot traverse an already closed generator when running Arr::first with an empty generator and no callback by [@​moshe-autoleadstar](https://github.com/moshe-autoleadstar) in [https://github.com/laravel/framework/pull/48979](https://github.com/laravel/framework/pull/48979) - fixes issue with stderr when there was "]" character. by [@​nikopeikrishvili](https://github.com/nikopeikrishvili) in [https://github.com/laravel/framework/pull/48975](https://github.com/laravel/framework/pull/48975) - \[10.x] Fix Postgres cache store failed to put exist cache in transaction by [@​xdevor](https://github.com/xdevor) in [https://github.com/laravel/framework/pull/48968](https://github.com/laravel/framework/pull/48968) ### [`v10.31.0`](https://github.com/laravel/framework/blob/HEAD/CHANGELOG.md#v10310---2023-11-07) [Compare Source](https://github.com/laravel/framework/compare/v10.30.1...v10.31.0) - \[10.x] Allow `Sleep::until()` to be passed a timestamp as a string by [@​jameshulse](https://github.com/jameshulse) in [https://github.com/laravel/framework/pull/48883](https://github.com/laravel/framework/pull/48883) - \[10.x] Fix whereHasMorph() with nullable morphs by [@​MarkKremer](https://github.com/MarkKremer) in [https://github.com/laravel/framework/pull/48903](https://github.com/laravel/framework/pull/48903) - \[10.x] Handle `class_parents` returning false in `class_uses_recursive` by [@​RoflCopter24](https://github.com/RoflCopter24) in [https://github.com/laravel/framework/pull/48902](https://github.com/laravel/framework/pull/48902) - \[10.x] Enable default retrieval of all fragments in `fragments()` and `fragmentsIf()` methods by [@​tabuna](https://github.com/tabuna) in [https://github.com/laravel/framework/pull/48894](https://github.com/laravel/framework/pull/48894) - \[10.x] Allow placing a batch on a chain by [@​khepin](https://github.com/khepin) in [https://github.com/laravel/framework/pull/48633](https://github.com/laravel/framework/pull/48633) - \[10.x] Dispatch 'connection failed' event in async http client request by [@​gdebrauwer](https://github.com/gdebrauwer) in [https://github.com/laravel/framework/pull/48900](https://github.com/laravel/framework/pull/48900) - authenticate method refactored to use null coalescing operator by [@​miladev95](https://github.com/miladev95) in [https://github.com/laravel/framework/pull/48917](https://github.com/laravel/framework/pull/48917) - \[10.x] Add support for Sec-Purpose header by [@​nanos](https://github.com/nanos) in [https://github.com/laravel/framework/pull/48925](https://github.com/laravel/framework/pull/48925) - \[10.x] Allow setting retain_visibility config option on Flysystem filesystems by [@​jnoordsij](https://github.com/jnoordsij) in [https://github.com/laravel/framework/pull/48935](https://github.com/laravel/framework/pull/48935) - \[10.x] Escape forward slashes when exploding wildcard rules by [@​matt-farrugia](https://github.com/matt-farrugia) in [https://github.com/laravel/framework/pull/48936](https://github.com/laravel/framework/pull/48936) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/Lapotor/RadioRoster-api). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy41OS44IiwidXBkYXRlZEluVmVyIjoiMzcuNTkuOCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Thanks @Sophist-UK I can now say I contributed to laravel 😍 |
This change adds the ability to customise the behaviour of how missing translations are handled. The current behaviour is to return the key unchanged. For example
{{ __('user.name') }}
will render asuser.name
if the corresponding translation key doesn't exist in language files. This change allows a callback to define the missing translation behaviour increasing customisability. The callback can return a string which will then be returned by theget
method.An example of a use case for this is: I would like to add the ability to trigger a log message if a translation key is missed in production to detect when cryptic text such as user.name are being displayed to users in production.
There are packages to solve this problem using static analysis such as https://github.com/LarsWiegers/laravel-translations-checker, however static analysis is limited and can not pick up run-time generated keys, e.g.
__("user.$field")
. This change would make it possible to detect translation misses in run-time.Example of usage in any service provider:
These changes also include functionality to prevent infinite loops where the user defined callback itself contains a missing translation.
This idea was originally proposed at: #48862