Announcement Amezmo announces Free Tier for all developers. Get free account

How To Get Data In Lang In Laravel With Localization

It’s a new trend to have multi-lingual support in web applications, according to the majority of the audience. It’s definitely great for user experience as it improves the bounce rate and the user feels much comfortable in their native tongue, as most of the study says but What’s the correct way to do it? and is it possible to do in Laravel or you have to replicate the site?

Laravel Localization

It’s completely supported in Laravel out of the box, and it provides you a much organized and convenient way to support more than one language in your PHP application. You can store all the translations in JSON files or PHP files, just follow along in this tutorial and you’ll be able to get data using Laravel’s lang support.

Translation Files

Create two folders like in the format below.

  • resources/lang/en
  • resources/lang/es

Create the following translation files in both folders and name it whatever you like. In these files, you will return all the translations in an array as key-value pairs. For example, homepage.php is what I will use for lang translation of the homepage.

lang/en/homepage.php


<?php    
  return [        
   'title' => 'Sample Site',
   'navigationBar' => [
    "home" => 'Home',
    "pictures" => 'imágenes'
  ],
];

lang/es/homepage.php


<?php    
  return [        
    'title' => 'Sitio de muestra',
    'navigationBar' => [
    'home' => 'hogar',
    "pictures" => 'imágenes'   
  ],
];

Configure lang in Laravel

You must set the route for language locales, so you can access your website like this in different languages respectively. Open the file routes/web.php and configure it like this:


<?php
Route::get('/{locale}', function ($locale) {
    // locale can be 'en', or 'es' 
    // as we've defined in our language files above
    App::setLocale($locale);
    return view('homepage.php');
});

Use different lang entries in your Laravel application

It’s a piece of cake, to retrieve the strings from the language file with the help of __ helper function. let’s try retrieving our string. In your blade template file, homepage.php add the following code.


<h1>{{ __('homepage.title') }}</h1>
<p>{{ __('homepage.navigationBar.home') }}</p>

Overview of how to get lang data in Laravel applications

Laravel can be used for multi-lingual web applications, and you can easily use it in your applications with powerful structure and less code.

Lang in Laravel is a powerful feature, and you should be using it if your major audience is from different countries and if you want an organized and easy way to manage different languages.

If you’re looking for an easy place to host your PHP website, then head on over to Amezmo for the best PHP hosting.

Resources

Premium PHP Hosting for Developers.

Host PHP apps in Amezmo and deploy using the tools you love like GitHub, GitLab, or BitBucket.

Deploy Today for free