background preloader

Translations

Facebook Twitter

Laravel 5.5 - Localization from scratch - MundoInterativo. Hi, in this post I will show how you can use localization, remember that there more than one way to do it. For example, you can make a Middleware to handle the localization from User preferences, you can get the localization from specific route, from GeoLocalization, etc… In our example we will make a middleware to check if a session has locale and if had, the middleware will change the locale of your app and we will make some routes only to test it. First step, create a fresh installation of Laravel let’s do it, open your terminal and type: composer create-project laravel/laravel localization A fresh installation of Laravel has in your config/app.php file the default language of your application as you can see: By default all the translation strings of your application are in resources/lang/LOCALE FOLDER What documentation says: Typically, translation strings are stored in files within the resources/lang directory.

/resources /lang /en messages.php /es messages.php Translation files <? <? <? Finding Missing Laravel Translations | Steve Grunwell. Out of the box, Laravel ships with a simple-but-intuitive localization system: when you call trans('some.key')and Laravel will automatically replace it with the value of “key” within resources/lang/{locale}/some.php. Laravel translations also accept placeholders and can handle pluralization, making it extremely easy to build applications that are localization-ready. Of course, building an application that’s localization-ready means the Laravel translations need to be filled out to begin with. It’s far too easy to get on a role writing several views, then miss a string or two when creating the localization files.

Heck, even the comments in the Translator class within Laravel itself doesn’t seem to think much of it: If the line doesn’t exist, we will return back the key which was requested as that will be quick to spot in the UI if language keys are wrong or missing from the application’s language files. Installation $ composer require stevegrunwell/lost-in-translation. Store your translations in the database with Laravel translation loader package. Laravel has built-in support for localization using arrays stored in language files and in 5.4 they are adding the ability for JSON based language files.

Some applications are better suited to have the language strings stored in the database so that end users can change them through a custom user interface, and that’s where the Laravel translation loader package can be helpful. Let’s take a look at how this package can be set up and used. Installation First, install Laravel translation loader through Composer: composer require spatie/spatie/laravel-translation-loader Next, open your config/app.php and replace Laravel’s translation service provider: Illuminate\Translation\TranslationServiceProvider::class, with Laravel translation loader service provider: Spatie\TranslationLoader\TranslationServiceProvider::class, Then publish Laravel translation loader migration file: php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="migrations" php artisan migrate.

Laravel Language Manager. Laravel Langman, created by Mohamed Said, is a GUI for managing your JSON language files in a Laravel project. Langman is installed just like any traditional package and after you’ve added the Service Provider, just point your browser to to open the GUI. Using this interface you’ll be able to browse translation keys in different languages, add/remove keys, scan your project files for missing translations, and finally save your changes to the language JSON files. You can find all the details on the project page and it’s designed to work with the JSON language files that was added in Laravel 5.4.

If you are still using the array-based files you can check out Mohamed’s other package that is an Artisan command to manage those.