Framework: [5.4] How to use __('hello world') with package specific translations

Created on 14 Feb 2017  路  12Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.11
  • PHP Version: 7.0.15
  • Database Driver & Version: N/A

Description:

How can we have package specific translations to support the use of the new translation method in Laravel 5.4.

__('Hello world!')

Most helpful comment

All 12 comments

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?

Idea

Maybe it could work like this:

Adding translations (like normall)

    public function boot()
    {
        $this->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
    }

Then it could also look for *.json files in that directory.

Getting name-spaced translation:

__('namepace::Hello world!');

If not exists, it could look for __('Hello world!').
If that does not exists rather, then it could fallback to the text Hello 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:

  • Ability to set a loader on the translator.
  • Ability to set translations path on the loader.
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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mianshargeel picture mianshargeel  路  59Comments

PheRum picture PheRum  路  112Comments

MountainDev picture MountainDev  路  128Comments

Demers94 picture Demers94  路  88Comments

nkeena picture nkeena  路  75Comments