Translations
Introduction
Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.
Language strings may be stored in files within the application's lang directory. Within this directory, there may be subdirectories for each language supported by the application. This is the approach Laravel uses to manage translation strings for built-in Laravel features such as validation error messages:
To learn in detail about Localization, you can visit the Laravel documentation here.
Publishing the Language Files
By default, the Laravel application skeleton does not include the lang directory. If you would like to customize Laravel's language files or create your own, you should scaffold the lang directory via the lang:publish Artisan command. The lang:publish command will create the lang directory in your application and publish the default set of language files used by Laravel:
php artisan lang:publishConfiguring the Locale
The default language for your application is stored in the config/app.php configuration file's locale configuration option, which is typically set using the APP_LOCALE environment variable. You are free to modify this value to suit the needs of your application.
You may also configure a "fallback language", which will be used when the default language does not contain a given translation string. Like the default language, the fallback language is also configured in the config/app.php configuration file, and its value is typically set using the APP_FALLBACK_LOCALE environment variable.
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => env('APP_LOCALE', 'en_US'),
/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/
'fallback_locale' => 'en_US',Create a new Locale
To support localization in your package, you need to create language files. Follow the steps below to set up a language file for English translations.
Create the lang Folder
- Navigate to the
packages/Webkul/Example/src/Resourcesdirectory. - Create a folder named
lang.
Create Language Code Folders
- Inside the
langfolder, create different folders for each language you want to support. For example, you can create folders for English (en_US), Hindi (hi_IN), etc. - For now, let's create a folder named
en_USto represent English.
Create the app.php File
- Inside the
en_USfolder, create a file namedapp.phpfor language translations.
Directory Structure
The updated directory structure will look like this:
└── packages
└── Webkul
└── Example
└── src
├── ...
└── Resources
├── ...
└── lang
└── en_US
└── app.phpWriting a Translation in app.php
To add translations for your package, you can edit the app.php file located in the language folder (en_US in this case). Below is an example of how you can define translations:
Open the app.php File
- Navigate to
packages/Webkul/Example/src/Resources/lang/en_US. - Open the
app.phpfile.
Add the Translation
- Inside
app.php, define your translations as an associative array. For example:
<?php
return [
'admin' => [
'name' => 'John Doe'
]
];Load Translation from Package
To make translations from your package accessible, you need to register the language file in the service provider and then use them in your Blade templates.
Update the Service Provider
- Open the
ExampleServiceProvider.phpfile located inpackages/Webkul/Example/src/Providers. - Add the following code to the
bootmethod:
<?php
namespace Webkul\Example\Providers;
use Illuminate\Support\ServiceProvider;
class ExampleServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//...
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'example');
}
}Explanation
This code uses
$this->loadTranslationsFromto register translations from the lang directory of your package (packages/ Webkul/Example/src/Resources/lang) under the namespace'example'.The
loadTranslationsFrommethod registers translations for the'example'namespace from the specified path (__DIR__ . '/../Resources/lang').This makes translations accessible throughout your Laravel application using the
'example'namespace prefix.
Use Translations in Blade Files
In your Blade templates (.blade.php files), you can use the @lang helper function to retrieve translations. Use the namespace 'example' followed by the translation key. For example:
@lang('example::app.admin.name')Explanation
The
@lang('example::app.admin.name')syntax fetches the translation for'name'under the'admin'section from your package's translation file (app.php).Replace
'app.admin.name'with your actual translation keys to use different translations as needed in your application.
By following these steps, you've effectively loaded translations from your package and integrated them into your Laravel application's Blade templates. This allows you to maintain language-specific content and support localization within your package.