How can we have package specific translations to support the use of the new translation method in Laravel 5.4.
__('Hello world!')
Actually there isn't a way to read package translations using the JSON loader since the loader is not namespaced, do you have a suggested solution for this?
Maybe it could work like this:
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
}
Then it could also look for
*.json
files in that directory.
__('namepace::Hello world!');
If not exists, it could look for
__('Hello world!')
.
If that does not exists rather, then it could fallback to the textHello world!
.
Or to make it simple it should just be possible to load translations json files via the service provider.
Then the JSON could look like this:
{
"namespace::Hello world!": "Hello world!"
}
Or even
{
"namespace": {
"Hello world!": "Hello world!"
}
}
I think it's better not to namespace JSON translations, like __("Hello") is hello everywhere, the whole point is to make the translation keys natural so that you can use them without having to manage namespaces and keys.
For cases like you mentioned I think it's better to use the other loader, the array loader, rather than the JSON one.
I am cool with using it without a namespace. But would it become possible to load JSON translations from a package then, like if a package have some predefined translations for specific languages that a user can always overwrite by making their own translations.
Yes that seems like a good idea, I'll look into that :)
Any progress @themsaid 馃槃
Hello @themsaid, what's the status on this?
This is a great feature, please make it happen!! :)
Thanks
Ideas:
protected function loadsTranslationsFrom($path, $namespace = '*') {
if ($namespace == '*') {
$this->app['translator']->getLoader()->setPath($path);
} else {
$this->app['translator']->addNamespace($namespace, $path);
}
}
Edit:
class MultipleFileLoader extends FileLoader
{
public function __construct(Filesystem $files, $paths)
{
$this->path = (array) $paths;
$this->files = $files;
}
/**
* Load a locale from the given JSON file paths.
*
* @param array $paths
* @param string $locale
* @return array
*/
protected function loadJsonPath($paths, $locale)
{
$translations = [];
foreach ($paths as $path) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$translations[] = (array) json_decode($this->files->get($full), true);
}
}
return array_merge(...$translations);
}
}
In the service provider:
protected function registerTranslator()
{
$this->app->extend('translator', function ($translator, $app) {
$paths = [
dirname(__DIR__)."/publishable/translations",
$app['path.lang'],
// maybe pull more translation paths from the config....
];
$loader = new MultipleFileLoader($app['files'], $paths);
$translator->setLoader($loader);
return $translator;
});
}
I hope this feature can update soon. I'm developing packages too. JSON translation should be namespaced because the language can differ with the same key.
Maybe in L5.5? That would be a nice surprise.
Here you go: https://github.com/laravel/framework/pull/20599
Most helpful comment
Here you go: https://github.com/laravel/framework/pull/20599