Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JussiMannisto's avatar

Proper place for adding language namespaces

Hi,

I'm developing a dual-site which consists of an API served by a Lumen instance, and a CMS site built with Laravel (both ver. 5.5). These sites share a database and some resources, such as translations.

I'm using language namespaces to access the shared translations, like so:

Lang::addNamespace('someNamespace', realpath(base_path('../shared/resources/lang')));

I'm wondering what is the correct file & place for adding the namespace? I can't do it in bootstrap/app.php since the Lang facade hasn't been set up at that point. Currently I'm doing this in a service provider and it works, but that doesn't seem to me like the "proper" place to do it.

0 likes
2 replies
martinbean's avatar

Currently I'm doing this in a service provider and it works, but that doesn't seem to me like the "proper" place to do it.

@JussiMannisto Nope, the boot() method of a service provider is the proper place to register a language namespace :)

Namespaces for resources like language files and views are for more “packages”, so if you had a package that added a blog to your application then you may register a blog:: namespace and access translations like trans('blog::messages.posts').

If you have both an API and a CMS, then you might create a package for your application that has its own service provider, and you have translations specific for your application that you load in there:

<?php // vendor/package/src/PackageServiceProvider.php

namespace Vendor\Package;

class PackageServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app['lang']->addNamespace('package', __DIR__.'/../resources/lang');
    }
}

Then like Laravel, you would store your translations in a resources/lang/{locale} directory, at the same level as your src directory:

  • /resources
    • /lang
      • /en
        • /messages.php
      • /es
        • /messages.php
    • /views
  • /src
    • /Console
      • /Commands
    • /Http
      • /Controllers
    • PackageServiceProvider.php
2 likes
JussiMannisto's avatar

Thanks Martin!

That's pretty much what I've done, except the package doesn't have its own service provider. I'll definitely be adding that.

Please or to participate in this conversation.