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.
-
Create a PHP language file for each locale in
Modules/Blog/lang/<locale>/.Modules/Blog/lang/en/messages.php <?phpreturn ['welcome' => 'Welcome to the blog',];Modules/Blog/lang/fr/messages.php <?phpreturn ['welcome' => 'Bienvenue sur le blog',]; -
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')ortrans('blog::messages.welcome'). -
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
langfolder now looks like this:DirectoryModules
DirectoryBlog
Directorylang
Directoryen
- messages.php
Directoryfr
- messages.php
- fr.json
-
Check for missing keys across locales.
Terminal window php artisan module:lang BlogThis 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. -
Check the result in the
frlocale.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):
'auto-discover' => [ 'migrations' => true, 'translations' => true,],Publish translations to the application
Section titled “Publish translations to the application”Publish a module’s language files so translators can edit them in the application:
php artisan module:publish-translation BlogThis 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.