Skip to content

Translate a module

To translate strings in the Blog module, add language files under Modules/Blog/lang and read them with the blog:: namespace. You need an enabled Blog module. module:make does not create the lang folder by default, so create it first.

  1. Create a PHP language file for each locale in Modules/Blog/lang/<locale>/.

    Modules/Blog/lang/en/messages.php
    <?php
    return [
    'welcome' => 'Welcome to the blog',
    ];
    Modules/Blog/lang/fr/messages.php
    <?php
    return [
    'welcome' => 'Bienvenue sur le blog',
    ];
  2. Read the translation using <module>::<file>.<key>.

    Modules/Blog/resources/views/index.blade.php
    <x-blog::layouts.master>
    <h1>{{ __('blog::messages.welcome') }}</h1>
    </x-blog::layouts.master>

    In PHP, use __('blog::messages.welcome') or trans('blog::messages.welcome').

  3. Optional: add JSON translation files in Modules/Blog/lang/ for string-keyed translations.

    Modules/Blog/lang/fr.json
    {
    "Read more": "Lire la suite"
    }

    JSON translations have no module namespace; read them with __('Read more').

    The lang folder now looks like this:

    • DirectoryModules
      • DirectoryBlog
        • Directorylang
          • Directoryen
            • messages.php
          • Directoryfr
            • messages.php
          • fr.json
  4. Check for missing keys across locales.

    Terminal window
    php artisan module:lang Blog

    This compares PHP language files across locales (it skips JSON files). If any file or key is missing in a locale, the command lists the differences and exits with 1.

  5. Check the result in the fr locale.

    Terminal window
    php artisan tinker --execute="app()->setLocale('fr'); echo __('blog::messages.welcome').' | '.__('Read more');"
    Bienvenue sur le blog | Lire la suite

Auto-discover translations without ModuleServiceProvider

Section titled “Auto-discover translations without ModuleServiceProvider”

Generated module providers extend ModuleServiceProvider and load module translations automatically. If a module does not extend ModuleServiceProvider (for example, a --plain module or one extending Illuminate\Support\ServiceProvider), enable auto-discover.translations in config/modules.php (default false):

config/modules.php
'auto-discover' => [
'migrations' => true,
'translations' => true,
],

Publish a module’s language files so translators can edit them in the application:

Terminal window
php artisan module:publish-translation Blog

This copies Modules/Blog/lang (including JSON files) to lang/modules/blog. Existing files are skipped unless you pass --force.

Once lang/modules/blog exists, the module loads translations exclusively from that directory for both PHP and JSON files, with no fallback to Modules/Blog/lang.