Skip to content

[9.x] Stringables for Translator class - #45874

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
peterfox:feature/translation-stringables
Jan 31, 2023
Merged

[9.x] Stringables for Translator class#45874
taylorotwell merged 2 commits into
laravel:9.xfrom
peterfox:feature/translation-stringables

Conversation

@peterfox

@peterfox peterfox commented Jan 30, 2023

Copy link
Copy Markdown
Contributor

Changes

  • Adds a stringable method to the Translator class
  • Adds the ReflectsClosures trait to the Translator class
  • Adds a mechanism to process objects passed into the translator get method to return the object into a string.
  • Adds a test to demo the use of altering how a Carbon datetime object could be altered from its default string form.

Why

This behaves precisely to how Stringables work with the Blade compiler. The use case for this is that sometimes it's a pain having to format everything before passing it to the get method. Instead, the translator can handle the values as objects and convert them to strings.

Notes

This won't cause conflicts or break anything, as it's all additional code. That said, there is no Facade for the Translator currently. This would be hard to document as a feature as the more visible method would be:

trans()->stringable(fn(Carbon $carbon) => $carbon->format('Y-m-d'));

@peterfox peterfox changed the title [Features] Stringables for Translator class [9.x] Stringables for Translator class Jan 30, 2023
@taylorotwell
taylorotwell merged commit fc299c0 into laravel:9.x Jan 31, 2023
@peterfox
peterfox deleted the feature/translation-stringables branch January 31, 2023 21:13
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