Description
I have a Laravel site is in English. I want to make it compatible with Dutch also.
I start my homepage small, it only contain 3 sentences.
<h1 class="heading">My business WiFi</h1> <p>Management via this page both your <a>private WiFi</a> as your <a>guest wifi</a>.</p> <p>Or follow the use of your WiFi via our <a>statistics</a>.</p>
which will render this
My business WiFi Management via this page both your private WiFi as your guest wifi. Or follow the use of your WiFi via our statistics.
Attempt
I’ve installed this Laravel package and did all the installation steps from that repo.
My routes, service provider, alias, middleware, resources/lang/ , and the laravellocalization.php are all in to place.
routes.php
Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect','localize'] ], function(){ Route::get('/homepage', 'homepageController@index'); });
update providers array
McamaraLaravelLocalizationLaravelLocalizationServiceProvider::class
update alias
'LaravelLocalization' => McamaraLaravelLocalizationFacadesLaravelLocalization::class
update $routeMiddleware
'localize' => McamaraLaravelLocalizationMiddlewareLaravelLocalizationRoutes::class, 'localizationRedirect' => McamaraLaravelLocalizationMiddlewareLaravelLocalizationRedirectFilter::class, 'localeSessionRedirect' => McamaraLaravelLocalizationMiddlewareLocaleSessionRedirect::class
/config/laravellocalization.php
<?php return [ 'supportedLocales' => [ 'en' => ['name' => 'English','script' => 'Latn', 'native' => 'English'], 'nl' => ['name' => 'Dutch','script' => 'Latn', 'native' => 'Nederlands'], ], 'useAcceptLanguageHeader' => true, 'hideDefaultLocaleInURL' => false, ];
Result
When I visit
http://site/nl/homepage
OR
http://site/en/homepage http://site/homepage
I saw my site load as it should in English, which is good.
It return 404
when I visit some other site that not declared.
China
Ex. http://site/zh/homepage
Which is good because it works as intended.
But my main goal is I want see my site in Dutch when I land on
http://site/nl/homepage
I should see this 3 sentences below in Dutch
My business WiFi Management via this page both your private WiFi as your guest wifi. Or follow the use of your WiFi via our statistics.
Questions
Is this something that I need to declare /resources/lang/nl/texts.php
?
Or is this something that can be achievable via library / plug-in / framework?
Advertisement
Answer
You need to create appropriate files and fill them with translated string (or arrays). If any of the translated string is not found, Laravel will fallback to config('fallback_locale')
.
With
// resources/lang/nl/texts.php <?php return [ 'heading' => 'Mijn zakelijke WiFi', 'line1' => 'Beheer via deze pagina zowel uw <a>private WiFi</a> als uw <a>gast wifi</a>.', 'line2' => 'Of volg het gebruik van uw WiFi via onze <a>statistieken</a>.' ];
and
// resources/lang/en/texts.php <?php return [ 'heading' => 'My business WiFi', 'line1' => 'Management via this page both your <a>private WiFi</a> as your <a>guest wifi</a>.', 'line2' => 'Or follow the use of your WiFi via our <a>statistics</a>.' ];
you will be able to retrieve these strings (prepending filename) anywhere by translate functions like trans('texts.heading')
, trans_choice('texts.heading', $count)
or special Blade directive @lang('texts.heading')
Your view could look like this:
<h1 class="heading">@lang('texts.heading')</h1> <p>{!! trans('texts.line1') !!}</p> <p>{!! trans('texts.line2') !!}</p>
Since there is HTML markup in your translated strings, you will have to unescape them by using {!! ... !!}
.
In order to ease the pain of translation, there is Laravel Translation Manager, great plugin by Barry vd. Heuvel. You can let your users modify these strings and catch untranslated string automatically.
In general, I will not recommend autotranslation services if you require quality translation. If you don’t, just consider javascript library for that.