Is there any way to load translations from laravel locale files?
I checked out the Translator class and it only looks in the Lang folder.
It will be easiest to do:
Carbon::getTranslator()->setMessages('xx', include resource_path('lang/xx/...'));
But you can also extend the Carbon\Translator and replace it by:
Carbon::setTranslator($yourCustomTranslator);
However if you think there is an error in translations please let us know and/or submit a pull request!
Thank you so much. Did I just miss that in the documentation, or can we change the title of this issue to incomplete docs about localization and tag it to be fixed?
In case anyone else came across this, I put the first code you provided right before I call Carbon::setLocale($locale);. It doesn't work if you put it after that.
It's documented https://carbon.nesbot.com/docs/#api-localization
It's documented https://carbon.nesbot.com/docs/#api-localization
setMessages is not in the documentation
You're absolutely right, we added some methods to the translator from symfony/translation, and we should document it.
Found it, it was documented but hidden in the v1 documentation block. I will extract it.
New system easier to customize released and documented.
Is it me or is setMessages still not in the docs?
It's not. Because we now provide a much easier way (that avoids giving twice the locale name and allows to extend an existing language): setTranslations this method is equivalent to setMessages with the current locale.
So with Carbon >= 2.9.0, you can customize translation as the example in the doc:
// we recommend to use custom language name/variant
// rather than overriding an existing language
// to avoid conflict such as "en_Boring" in the example below:
$boringLanguage = 'en_Boring';
$translator = \Carbon\Translator::get($boringLanguage);
$translator->setTranslations([
'day' => ':count boring day|:count boring days',
]);
// as this language starts with "en_" it will inherit from the locale "en"
$date1 = Carbon::create(2018, 1, 1, 0, 0, 0);
$date2 = Carbon::create(2018, 1, 4, 4, 0, 0);
echo $date1->locale($boringLanguage)->diffForHumans($date2); // 3 boring days before
$translator->setTranslations([
'before' => function ($time) {
return '['.strtoupper($time).']';
},
]);
echo $date1->locale($boringLanguage)->diffForHumans($date2); // [3 BORING DAYS]
I just added examples for all our additional methods in the Translator: https://carbon.nesbot.com/docs/#custom-translations
Now every possibilities with the translator should be covered.