Adding a New Namespace to the Message Loader

Problem

You want to add a new set of namespaced messages to your translator.

Solution

Use the Lang::addNamespace() method.

$namespace = 'custom';
$path = app_path().'/storage/custom-messages';
Lang::addNamespace($namespace, $path);

Now, assuming you had an error.php file in app/storage/custom-messages/en with the key 'test', then the following key would work.

echo Lang::get('custom::error.test');

Discussion

This method allows packages to have their own language files.

Most often a package will have a service provider, which will call an internal package() method in the service provider to register all the namespaces the package uses. The language files are just one of those registrations.

But if the package has no configuration or views, it may call this method directly. Most likely, if it's calling this method directly it's calling it at a low level using the $this->app['translator']->addNamespace(...) syntax.

comments powered by Disqus