<?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>DEV Community: M.Kazemi</title>
    <description>The latest articles on DEV Community by M.Kazemi (@kazemimuhammad).</description>
    <link>https://dev.to/kazemimuhammad</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%2F1309786%2F6a94fafe-4cbc-46f3-90e2-df3fe112235b.jpeg</url>
      <title>DEV Community: M.Kazemi</title>
      <link>https://dev.to/kazemimuhammad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kazemimuhammad"/>
    <language>en</language>
    <item>
      <title>Laravel 10 multilanguage</title>
      <dc:creator>M.Kazemi</dc:creator>
      <pubDate>Sat, 23 Mar 2024 04:36:36 +0000</pubDate>
      <link>https://dev.to/kazemimuhammad/laravel-multilanguage-1pc7</link>
      <guid>https://dev.to/kazemimuhammad/laravel-multilanguage-1pc7</guid>
      <description>&lt;p&gt;for first step you should publish the language&lt;br&gt;
&lt;code&gt;php artisan lang:publish&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then for step 2 create the language middleware&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Language
{
    public function handle($request, Closure $next)
    {
        App::setLocale(session()-&amp;gt;get('language') ?? 'en');
        return $next($request);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for step 3 register you middleware to the kernel.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;protected $middlewareGroups = [
    'web' =&amp;gt; [
        ...other middelwares
        Language::class,  //add your language middleware here
    ],
    //...
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for step 4 create your route&lt;br&gt;
&lt;code&gt;Route::get('/change-language/{lang}', 'HomeController@changeLanguage');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and then for last step set your controller func&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function changeLanguage($lang)
{
        if (!in_array($lang, ['en', 'de'])) {
            abort(400);
        }
        session(['language' =&amp;gt; $lang]);
        return back();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now in your lang directory you can create anoter directory with symbol name of language you want, like (de) for german.&lt;/p&gt;

&lt;p&gt;note: in the en or de directory you should create the PHP file like (messages.php) and set your keys and value.&lt;/p&gt;

&lt;p&gt;in the blade file you must point to the desired key in the messages file:&lt;br&gt;
&lt;code&gt;&amp;lt;div&amp;gt;{{ __('messages.hello-world') }}&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;done.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
