<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: RachidMA</title>
    <description>The latest articles on Forem by RachidMA (@rachidma).</description>
    <link>https://forem.com/rachidma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1103722%2Fe7a99c27-1ee2-49d0-b8ee-d04a0628ed3c.jpeg</url>
      <title>Forem: RachidMA</title>
      <link>https://forem.com/rachidma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rachidma"/>
    <language>en</language>
    <item>
      <title>Language Switching in Laravel: A Step-by-Step Guide to Multilingual Websites</title>
      <dc:creator>RachidMA</dc:creator>
      <pubDate>Sun, 18 Jun 2023 16:08:56 +0000</pubDate>
      <link>https://forem.com/rachidma/language-switching-in-laravel-a-step-by-step-guide-to-multilingual-websites-50eb</link>
      <guid>https://forem.com/rachidma/language-switching-in-laravel-a-step-by-step-guide-to-multilingual-websites-50eb</guid>
      <description>&lt;h2&gt;
  
  
  Hi, developers. 🙋‍♂️
&lt;/h2&gt;

&lt;p&gt;Today we will learn how to Easily Implement Language Switching and Translation in Your Laravel Project. Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is it important to implement language switching and translation in a Laravel project?&lt;/strong&gt;🕵️‍♂️&lt;/p&gt;

&lt;p&gt;Creating a website that supports multiple languages is crucial for connecting with a diverse audience. In this guide, we will walk through the process of implementing switching and translation in a Laravel project. By following these &lt;code&gt;step-by-step&lt;/code&gt; instructions, you'll be able to create a user-friendly language switcher and easily translate your website's content. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Set Up Your Laravel Project.&lt;/p&gt;

&lt;p&gt;To begin, create a new Laravel project using the Laravel Composer. This will serve as the foundation for implementing the language switching and translation functionality.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer create-project laravel/laravel LaravelLingo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Make sure you run your php install&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Add Language Switch Form&lt;br&gt;
Create a language switch form component that allows users to select their preferred language. This component can be included in your views using Blade templating. The form should include options for each language you want to support.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Under &lt;code&gt;resources/views directory&lt;/code&gt;, create a folder components&lt;/li&gt;
&lt;li&gt;create a file name it &lt;code&gt;language-switch.blade.php&lt;/code&gt; under components directory.&lt;/li&gt;
&lt;li&gt;Create a switcher form, the form should look like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;language-switch.blade.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoxopzqctgyk8i2i0i0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoxopzqctgyk8i2i0i0m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The form's value will contain the language picked by the user and will be submitted to &lt;code&gt;language.switch&lt;/code&gt; route&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Today we will use &lt;code&gt;onchange="this.form.submit()"&lt;/code&gt; inside the form just for demonstration, you are free to use other methods to submit the form with the picked language&lt;/p&gt;

&lt;p&gt;Laravel provides built-in localization features that allow you to easily manage language files and translations. Make sure localization is enabled in your Laravel application by checking the &lt;code&gt;config/app.php&lt;/code&gt; file. The locale option should be set to the default language (English in your case) and the &lt;code&gt;fallback_locale&lt;/code&gt; option should also be set, as showing bellow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flip0lrv1mkxpl7yird0c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flip0lrv1mkxpl7yird0c.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Include your language switcher in your &lt;code&gt;welcome.blade&lt;/code&gt;.&lt;br&gt;
Inside your welcome.blade, include the component language-switcher or depends on where you want to implement it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;welcome.blade.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frylt6ddpujb01a5voqqz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frylt6ddpujb01a5voqqz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, lets display a simple message when user switch the language. for example &lt;/p&gt;

&lt;p&gt;&lt;code&gt;'Welcome to the home page!'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-Add html tags to display the message above or under the switch container, for example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12zdb6fdc9o2k8msd611.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12zdb6fdc9o2k8msd611.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in the welcome page above will have two messages, a message will show the language picked using the switch and the translation of the message to different languages, Today we will use French and Arabic.&lt;/p&gt;

&lt;p&gt;So to be able to fetch the translation of the message displayed we will have to follow some more steps: &lt;/p&gt;

&lt;p&gt;HOW IT WORKS?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Create Language Route&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d8oxaewm7hc7h0bmmpn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d8oxaewm7hc7h0bmmpn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Create language Controller&lt;/p&gt;

&lt;p&gt;run &lt;code&gt;php artisan make:controller LanguageController&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LanguageController.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftx4xop0dxxnhgh10h14g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftx4xop0dxxnhgh10h14g.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above will store the language in the session and return a variable &lt;code&gt;language_switched&lt;/code&gt; which will be used later in the welcome.blade &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Create A Middleware&lt;br&gt;
The middleware will fetch to new picked language from session and set the local to the new language&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:middleware LanguageMiddleware&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;LanguageMiddleware.php&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t55wz8r13nspabueija.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t55wz8r13nspabueija.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as mentioned above, the middleware will setLocale to the new language fetched from the session.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;If you want to check if local language is correctly set, use laravel log by adding a line code. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Log::info("Locale set to: " . $language . " (Selected language: " . $language . ")");


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to  &lt;code&gt;storage/logs/laravel.log&lt;/code&gt; file to check, you should see results below every time we switch language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1jf89pd25hsonnmhggdi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1jf89pd25hsonnmhggdi.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Register Middleware.&lt;/p&gt;

&lt;p&gt;Now we need to register the middleware, go to &lt;code&gt;Http/Kernel.php&lt;/code&gt; and place your middleware as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3jms7km4xfjhoi7gdgu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3jms7km4xfjhoi7gdgu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Translate The Text&lt;br&gt;
The fun part, let's translate the welcome message, do you remember it?&lt;br&gt;
&lt;code&gt;'Welcome to the home page!'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create two subdirectories under &lt;code&gt;resources/lang&lt;/code&gt; folder one for French language&lt;code&gt;(fr)&lt;/code&gt; and one for Arabic&lt;code&gt;(ar)&lt;/code&gt;&lt;br&gt;
Your directory should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxujo7iic22ek3px54jqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxujo7iic22ek3px54jqf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside each directory, create a file called &lt;code&gt;messages.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-French:&lt;br&gt;
&lt;code&gt;resources/lang/fr/messages.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4wgmm5gejawnk4srmvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4wgmm5gejawnk4srmvz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-Arabic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rpi21docklv11fjlx0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rpi21docklv11fjlx0a.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each messages file should contain the translation of the message you need to translate and give a name so we can call it later from welcome.blade.php file&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Lets test it.&lt;br&gt;
To access the translation of each text you want to display based on the language picked, laravel has a helper&lt;code&gt;(__)&lt;/code&gt;double underscore followed by the name of the text, here we are using what so called 'welcome'.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;div class="text-message"&amp;gt;
     &amp;lt;p&amp;gt;{{__('messages.welcome')}}&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Since laravel local is set to &lt;code&gt;en&lt;/code&gt; for English by default, the message will be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxsjwy8kpf7jx8yml9dq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxsjwy8kpf7jx8yml9dq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedneyw6nfuebxkn6gyw2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedneyw6nfuebxkn6gyw2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Switch to French:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvoyrpw4s2aa5bqfz0bs8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvoyrpw4s2aa5bqfz0bs8.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Switch to Arabic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qmojwg35nosx69goklg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qmojwg35nosx69goklg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!👏🏻👏🏻👍🏻&lt;/p&gt;

&lt;p&gt;You have successfully implemented language switching and translation in your Laravel project. By following these steps, you can provide a localized experience to your users.&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
