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

numdel's avatar

Namespacing translations in Laravel with the same namespace (kinda prefix)

tl:dr:

Can two or more packages share the same namespace name? (in the following example package_lang)

FooPackage (ServiceProvider.php)

$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'package_lang');

BarPackage (ServiceProvider.php)

$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'package_lang');

BazPackage (ServiceProvider.php)

$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'package_lang');

Instead of using a unique namespaces, like: foo_lang, bar_lang, baz_lang.

Btw, it works on the loadTranslationsFrom() method with the same namespace (like above, just resources/views directory), but not with loadTranslationsFrom().

Appreciate a little description why or why not?!

--- Detailed following below ---

In the ServiceProvider (of my packages) I always do $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'package_lang'); which literally resolves in foo_lang::, which is cool, because translations can be called like this package_lang::something.promoto

My custom packages does always follow the same structure. (ommited directories which aren't neccessary)

  1. Package Foo
  • /resources
    • /lang
      • /en
        • /foo
          • /something.php
      • /es
        • /foo
          • /something.php
    • /views
  • /src
    • PackageServiceProvider.php
  1. Package Bar
  • /resources
    • /lang
      • /en
        • /bar
          • /something.php
      • /es
        • /bar
          • /something.php
    • /views
  • /src
    • PackageServiceProvider.php
  1. Package Baz
  • /resources
    • /lang
      • /en
        • /baz
          • /something.php
      • /es
        • /baz
          • /something.php
    • /views
  • /src
    • PackageServiceProvider.php

It would be cool, if I can call my translation, which are from the same package-family (package_lang), with the namespace package_lang::foo.something.promote or package_lang::bar.something.promote

0 likes
2 replies
WillianKoessler's avatar

I managed to do this with Laravel 10.

app("translator")->addNamespace(
	namespace: "my_namespace",
	hint: "path/to/my/new/lang/directory/lang_file.php"
);

__("my_namespace::lang_file.message");

Please or to participate in this conversation.