<?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: Yanal Shoubaki</title>
    <description>The latest articles on DEV Community by Yanal Shoubaki (@yanalshoubaki).</description>
    <link>https://dev.to/yanalshoubaki</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%2F336123%2Fbc8af834-52af-4312-ab58-d9ac81f06da2.jpg</url>
      <title>DEV Community: Yanal Shoubaki</title>
      <link>https://dev.to/yanalshoubaki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yanalshoubaki"/>
    <language>en</language>
    <item>
      <title>How to make translatable laravel app</title>
      <dc:creator>Yanal Shoubaki</dc:creator>
      <pubDate>Sat, 23 Oct 2021 05:27:54 +0000</pubDate>
      <link>https://dev.to/yanalshoubaki/how-to-make-translatable-laravel-app-19dg</link>
      <guid>https://dev.to/yanalshoubaki/how-to-make-translatable-laravel-app-19dg</guid>
      <description>&lt;p&gt;Use these methods to easily make translatable on model attributes in Laravel&lt;/p&gt;

&lt;p&gt;This article will explain the different ways to automatically translate eloquent model attributes while accessing or retrieving or create them.&lt;/p&gt;

&lt;p&gt;There are several ways to achieve this, and we’ll discuss them along with their quirks in this article.&lt;/p&gt;

&lt;p&gt;There are many packages do this work, my favorite package is &lt;a href="https://github.com/spatie/laravel-translatable"&gt;https://github.com/spatie/laravel-translatable&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This package have many methods the will give us an easy way to translate selected attributes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites and setup
&lt;/h2&gt;

&lt;p&gt;This tutorial assumes you are fairly experienced with Laravel (note that this tutorial uses Laravel 7.x).&lt;/p&gt;

&lt;p&gt;Of course, to begin, we have to have our development environment set up. First, install the package:&lt;/p&gt;

&lt;p&gt;You can install the package via composer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require spatie/laravel-translatable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to have another fallback_locale than the app fallback locale &lt;code&gt;(see config/app.php)&lt;/code&gt;, you could publish the config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your config folder you will find file name : translatable.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="cm"&gt;/*
     * If a translation has not been set for a given locale, use this locale instead.
     */&lt;/span&gt;
    &lt;span class="s1"&gt;'fallback_locale'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Making a model translatable
&lt;/h2&gt;

&lt;p&gt;The required steps to make a model translatable are:&lt;br&gt;
First, you need to add the &lt;code&gt;Spatie\Translatable\HasTranslations-trait&lt;/code&gt;.&lt;br&gt;
Next, you should create a public property $translatable which holds an array with all the names of attributes you wish to make translatable.&lt;/p&gt;

&lt;p&gt;Finally, you should make sure that all translatable attributes are set to the text-datatype in your database. If your database supports json-columns, use that.&lt;/p&gt;

&lt;p&gt;Here’s an example of a prepared model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Spatie\Translatable\HasTranslations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;HasTranslations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$translatable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'text'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now we need to select the attributes that we need to translate it:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public $translatable = ['name', 'text'];&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Available methods&lt;br&gt;
Getting a translation&lt;br&gt;
The easiest way to get a translation for the current locale is to just get the property for the translated attribute. For example (given that &lt;code&gt;name&lt;/code&gt; is a translatable attribute):&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;name; // get name attribute&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can also use this method:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public function getTranslation(string $attributeName, string $locale, bool $useFallbackLocale = true) : string&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;this method well get &lt;code&gt;name&lt;/code&gt; attribute translated by locale language you choose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting all translations
&lt;/h2&gt;

&lt;p&gt;You can get all translations by calling &lt;code&gt;getTranslations()&lt;/code&gt; without an argument:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;getTranslations();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or you can use the accessor :&lt;br&gt;
&lt;code&gt;$post-&amp;gt;translations&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting a translation
&lt;/h2&gt;

&lt;p&gt;The easiest way to set a translation for the current locale is to just set the property for a translatable attribute. For example (given that name is a translatable attribute):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;name = 'new post'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;that will generate a json text like this : &lt;code&gt;{"en": "new post"}&lt;/code&gt;inside name column in database table.&lt;br&gt;
for create multi translation:&lt;br&gt;
&lt;code&gt;$post-&amp;gt;name = ['en' =&amp;gt; 'new post', 'ar' =&amp;gt; 'موضوع جديد'];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To set a translation for a specific locale you can use this method:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public function setTranslation(string $attributeName, string $locale, string $value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To actually save the translation, don’t forget to save your model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTranslation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Updated name in English'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTranslation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ar'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Updated name in Arabic'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Forgetting a translation&lt;br&gt;
You can forget a translation for a specific field:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public function forgetTranslation(string $attributeName, string $locale)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example :&lt;br&gt;
&lt;code&gt;$post-&amp;gt;forgetTranslation('name', 'ar')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can forget all translations for a specific locale:&lt;br&gt;
&lt;code&gt;public function forgetAllTranslations(string $locale)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;forgetAllTranslations('ar')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Getting all translations in one go&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public function getTranslations(string $attributeName): array&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;getTranslations('name')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Getting the specified translations in one go&lt;/p&gt;

&lt;p&gt;You can filter the translations by passing an array of locales:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public function getTranslations(string $attributeName, array $allowedLocales): array&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$post-&amp;gt;getTranslations('name', ['en', 'ar'])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace translations in one go&lt;/p&gt;

&lt;p&gt;You can replace all the translations for a single key using this method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;replaceTranslations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$translations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$newTranslations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'en'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;replaceTranslations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$newTranslations&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getTranslations&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ['en' =&amp;gt; 'hello']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting the model locale
&lt;/h2&gt;

&lt;p&gt;The default locale used to translate models is the application locale, however it can sometimes be handy to use a custom locale.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;setLocale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return `en` translation&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// will set the `en` translation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can use usingLocale static method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Will automatically set the `en` translation&lt;/span&gt;
&lt;span class="nv"&gt;$newsItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;usingLocale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating models
&lt;/h2&gt;

&lt;p&gt;You can immediately set translations when creating a model. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
   &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="s1"&gt;'en'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Name in English'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s1"&gt;'ar'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'الأسم بالعربي'&lt;/span&gt;
   &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Querying translatable attributes&lt;br&gt;
If you’re using MySQL 5.7 or above, it’s recommended that you use the json data type for housing translations in the db. This will allow you to query these columns like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name-&amp;gt;en'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Name in English'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can set translation using &lt;code&gt;app()-&amp;gt;setLocale('en')&lt;/code&gt; , this an easy way to get translation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;setLocale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'en'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// will get 'en' translation&lt;/span&gt;
&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'hello'&lt;/span&gt; &lt;span class="c1"&gt;// will set 'en' translation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;resource : &lt;a href="https://github.com/spatie/laravel-translatable"&gt;https://github.com/spatie/laravel-translatable&lt;/a&gt;&lt;/p&gt;

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