Skip to content

Commit b05a279

Browse files
committed
translation stringables
1 parent fbe5162 commit b05a279

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

localization.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ If your placeholder contains all capital letters, or only has its first letter c
169169
'welcome' => 'Welcome, :NAME', // Welcome, DAYLE
170170
'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle
171171

172+
<a name="object-replacement-formatting"></a>
173+
#### Object Replacement Formatting
174+
175+
If you attempt to provide an object as a translation placeholder, the object's `__toString` method will be invoked. The [`__toString`](https://www.php.net/manual/en/language.oop5.magic.php#object.tostring) method is one of PHP's built-in "magic methods". However, sometimes you may not have control over the `__toString` method of a given class, such as when the class that you are interacting with belongs to a third-party library.
176+
177+
In these cases, Laravel allows you to register a custom formatting handler for that particular type of object. To accomplish this, you should invoke the translator's `stringable` method. The `stringable` method accepts a closure, which should type-hint the type of object that it is responsible for formatting. Typically, the `stringable` method should be invoked within the `boot` method of your application's `AppServiceProvider` class:
178+
179+
use Illuminate\Support\Facades\Lang;
180+
use Money\Money;
181+
182+
/**
183+
* Bootstrap any application services.
184+
*
185+
* @return void
186+
*/
187+
public function boot()
188+
{
189+
Lang::stringable(function (Money $money) {
190+
return $money->formatTo('en_GB');
191+
});
192+
}
193+
172194
<a name="pluralization"></a>
173195
### Pluralization
174196

0 commit comments

Comments
 (0)