<?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: Farouk BRAIK</title>
    <description>The latest articles on Forem by Farouk BRAIK (@fabraik).</description>
    <link>https://forem.com/fabraik</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%2F287205%2Fc01d085c-f9f0-473c-9b87-768414197c41.png</url>
      <title>Forem: Farouk BRAIK</title>
      <link>https://forem.com/fabraik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fabraik"/>
    <language>en</language>
    <item>
      <title>Part 1 : How to support multiple languages with laravel</title>
      <dc:creator>Farouk BRAIK</dc:creator>
      <pubDate>Fri, 20 May 2022 00:32:09 +0000</pubDate>
      <link>https://forem.com/fabraik/part-1-how-to-support-multiple-languages-with-laravel-pni</link>
      <guid>https://forem.com/fabraik/part-1-how-to-support-multiple-languages-with-laravel-pni</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Part 1 : How to support multiple languages with laravel&lt;/li&gt;
&lt;li&gt;
Part 2 : How to support multiple languages using laravel's middlewares Coming soon&lt;/li&gt;
&lt;li&gt;
Part 3 : How to support multiple languages using laravel's models Coming soon&lt;/li&gt;
&lt;li&gt;
Part 4 : How to support multiple languages using laravel's packages Coming soon&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Having laravel as a backend framework has its perks, and one of them is the ability to support multiple languages (locales) through your website. &lt;/p&gt;

&lt;p&gt;Today we are going to see how this is going to work, but first of all we are going to create a new laravel application. &lt;/p&gt;

&lt;p&gt;By typing &lt;code&gt;laravel new laravel_multi_languages&lt;/code&gt; we create a new applicatin: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F8D6y7oO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81802xyhrgkvhbk8lbdx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F8D6y7oO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81802xyhrgkvhbk8lbdx.png" alt="a laravel new project command through a terminal" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we will then open the application with our code editor (in my case VSCode)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3PqlCs-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8sg0ncobtv7wvtn648o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3PqlCs-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8sg0ncobtv7wvtn648o.png" alt="opening VSCode inside the application's folder from a terminal" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Note&lt;/strong&gt;: I will be using laravel 9, if you have an older version it means that you should find your 'lang' folder in your 'resources' directory.&lt;/p&gt;
&lt;h2&gt;
  
  
  Laravel localization
&lt;/h2&gt;

&lt;p&gt;Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.&lt;/p&gt;

&lt;p&gt;Every laravel application has a language directory somewhere, for laravel 9, a 'lang' directory should be found in the base path of the application, but for older version, it should be found inside the 'resources' directory.&lt;br&gt;
laravel stores its storage in a folder structure, every folder is a locale (like 'en') and then we follow those sub folders with php files that indicates the proper translation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/lang
    /en
        messages.php
    /es
        messages.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;all we need to use a translation is to use files with the same name for each locale and then use the same key to translate. &lt;br&gt;
Let's see how to get this to work: &lt;/p&gt;

&lt;p&gt;inside the 'lang' directory for our new project there is only 'en' locale, but that's okay, it is the default locale of a new laravel application, we will be adding another new locale for our application (Let's say French or 'fr')&lt;/p&gt;

&lt;p&gt;the first thing we need to do is to define the supported locales of our application, this way if the user will try to switch locale, we will know if we will let them or not.&lt;/p&gt;

&lt;p&gt;. inside our 'config' directory we open the 'app.php' file, after that, we add a new line :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// config/app.php 
...
// here we pass an array of the available locales
'locales' =&amp;gt; ['en', 'fr'],

// this is the default locale
'locale' =&amp;gt; 'en',
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;. right after that we create a new folder inside the 'lang' directory called 'fr' to use it for french translations, let's also create a new file called 'hello_localization.php' inside both locales' folders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/lang
    /en
        hello_localization.php
    /fr
        hello_localization.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we want to try and say 'hello' in both french and english, we need a unique key that will refer to 'hello' in both languages, se inside each 'hello_localization.php' folder we return write like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lang/en/hello_localization.php 
&amp;lt;?php

return [
  'hello_message' =&amp;gt; 'Hello!'
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And also for the other language&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lang/fr/hello_localization.php 
&amp;lt;?php

return [
  'hello_message' =&amp;gt; 'Bonjour!'
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we gave this 'hello' a name called 'hello_message', this name indicates that the returned value should be a hello message like it says. &lt;/p&gt;

&lt;p&gt;So right now, our application knows how to say hello in both languages which is good, but we need to test that out.&lt;/p&gt;

&lt;p&gt;. In order to test this out we need to create a new route&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// routes/web.php
...
Route::get('/say-hello', function () {
    return __('hello_localization.hello_message');
});
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you can see here &lt;code&gt;__()&lt;/code&gt; is a helper function that will translate the code giving into the string, in our case we want to display the 'hello_message', but we also need to specify the file that it came from, for that we enter 'hello_localization' (without the '.php') and then writing the translation code that we want to work on. &lt;br&gt;
by calling this function the application will use its own locale and tries to translate it. &lt;/p&gt;

&lt;p&gt;the way we access and modify our application's locale is the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is how we modify an application's locale
app()-&amp;gt;setLocale($locale);

// This is how we get an application's locale 
app()-&amp;gt;getLocale();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's serve our application now by using &lt;code&gt;php artisan serve&lt;/code&gt; inside a terminal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4BNzzVqH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frdcs5b98c0p7a5du63x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4BNzzVqH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frdcs5b98c0p7a5du63x.png" alt="running a 'php artisan serve' through a terminal" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and if we go now to &lt;code&gt;127.0.0.1:8000/say-hello&lt;/code&gt;, we should get a message like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g5_plAZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofilfoec1abi1r0ejjdg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g5_plAZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofilfoec1abi1r0ejjdg.png" alt="Accessing the applications 'say-hello' route in english" width="561" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say we want to display the application's hello message in french, all we need to do is adding this line in 'web.php' file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// routes/web.php
...
Route::get('/say-hello', function () {
    app()-&amp;gt;setLocale('fr');
    return __('hello_localization.hello_message');
});
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we go again to &lt;code&gt;127.0.0.1:8000/say-hello&lt;/code&gt;, we should get a message like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MJbMYqyq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qauohpjjkrb12fkvyktl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MJbMYqyq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qauohpjjkrb12fkvyktl.png" alt="Accessing the applications 'say-hello' route in french" width="585" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have a basic understanding of how localization works, in the next part, I will show you how to remember the users locale choice, and/or prevent the application from updating to a non existing locale.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;u&gt;Important Notice&lt;/u&gt;&lt;/strong&gt;: Kindly leave me a comment or a suggesting that will help me deliver better content to you, your help is greatly appreciated.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Hello World!</title>
      <dc:creator>Farouk BRAIK</dc:creator>
      <pubDate>Tue, 10 May 2022 20:49:04 +0000</pubDate>
      <link>https://forem.com/fabraik/hello-world-3kb1</link>
      <guid>https://forem.com/fabraik/hello-world-3kb1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yolZd4BT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw7qlp6gq037sqz79lno.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yolZd4BT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw7qlp6gq037sqz79lno.jpg" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
“Hello World”... These are the two words that most of us developers used when we started our journey into development and programming in general. &lt;/p&gt;

&lt;p&gt;I spent nearly 6 years of coding as a hobby, I used to learn through watching some tutorial via YouTube, reading some articles and so on, and even though I am thankful for every person that had helped me learn programming in general, I couldn’t help but noticed that after that I passed that first learning curve, there was always something missing. &lt;/p&gt;

&lt;p&gt;That is why I decided on making some tutorials on my blog, and soon starting a YouTube video to follow after it, the purpose of this journey is to help everyone to get started on web development with a solid base and they will be capable of creating anything they want with that, but before that, I just want to point on somethings that are very important!&lt;/p&gt;

&lt;p&gt;Computer programming in general in a vast field and it is expanding as you are reading this, but don’t worry, I don’t mean to scare or discourage you, I simply want to tell you the truth that could be either exiting for some people, or will have an opposite effect towards others. Through this journey I will be posting how to start your journey of web development from scratch and I will guide you towards a better mastery of web development by also building some web applications in the process. And so, I will encourage you to keep on reading and checking out my content as it will be updated regularly, so stay tuned!  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Knowledge is power. Francis Bacon&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>journey</category>
    </item>
  </channel>
</rss>
