title | author | format | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Laravel Localization |
Vladimir Lelicanin - SAE Institute |
|
-
Localization is the process of adapting a product to meet the language, cultural, and other specific requirements of a particular country or region.
-
Laravel Localization allows you to create and maintain translations of your application text in a number of different languages.
-
Wider audience: Localization provides access to a wider audience.
-
Improved user experience: Developers can make an application more user-friendly through Localization.
-
Increased revenue: Localization can increase revenue and brand awareness.
// Getting the application's current locale:
$locale = app()->getLocale();
// Getting the translated message by key:
$message = __('welcome');
// Getting the translated message with replacement values:
$message = __('Hello :name', ['name' => 'John']);
-
Language files are stored in the
resources/lang
directory. -
You can create a new language file using the
php artisan make:lang
command.
// Create a new language file with name "messages" in "en" language
php artisan make:lang en messages
// resources/lang/en/messages.php
return [
'welcome' => 'Welcome to my Website!',
'click_here' => 'Click :link to continue.',
'http_error' => 'Error :code: :message',
];
// resources/views/welcome.blade.php
<h1>{{ __('welcome') }}</h1>
<p>{{ __('click_here', ['link' => route('home')]) }}</p>
$n = 5;
// Equivalent to _n('One apple', ':count apples', $n);
echo trans_choice('{0} No apples|{1} One apple|[2,*] :count apples', $n, ['count' => $n]));
// The translation might be:
// '{0} No apples|{1} :count apple|[2,*] :count apples'
// resources/lang/en/apples.php
return [
'apple' => '{0} No apples|{1} One apple|[2,*] :count apples',
];
// Usage:
$n = 5;
$apple = trans_choice('apples.apple', $n, ['count' => $n]));
echo $apple;
// resources/config/app.php
'fallback_locale' => 'en',
- If a language does not exist in the requested Locale, Laravel will use the fallback locale as a backup.
-
Locale selector can let users choose their preferred language.
-
It is useful for settings or profile page.
-
It can be implemented using Select Dropdown or Radio Button.
// resources/config/app.php
'providers' => [
Illuminate\Translation\TranslationServiceProvider::class,
App\Providers\CustomTranslationServiceProvider::class,
],
- You can register your custom translation provider in
app.php
.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
class CustomTranslationServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('translator', function ($app) {
$loader = new FileLoader(new Filesystem, $app->basePath().'/my_resources/lang');
$locale = $app->getLocale();
$trans = new Translator($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
return $trans;
});
}
}
-
Laravel can cache translations to speed up the application.
-
Use
php artisan translation:cache
to cache the translations.
- You can use the
translation:find
command to validate your translations.
$ php artisan translation:find
- This command will search all of the translatable strings and ensure that their translations are present and contain the appropriate substitution placeholders.
-
Laravel Localization provides an easy way to localize your web applications.
-
With Laravel Localization, you can easily translate application text into different languages.
-
This feature ensures your app is accessible to an international audience.