Skip to content
Advertisement

Best practice to render Laravel site in other language

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.

JavaScript

which will render this

JavaScript

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

JavaScript

update providers array

JavaScript

update alias

JavaScript

update $routeMiddleware

JavaScript

/config/laravellocalization.php

JavaScript

Result

When I visit

JavaScript

OR

JavaScript

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

JavaScript

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

JavaScript

and

JavaScript

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:

JavaScript

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement