text: import{_ as a,c as e,o as t,a as n}from"./app.5d92c05f.js";const m='{"title":"Multilanguage","description":"","frontmatter":{},"headers":[{"level":2,"title":"Configuring your model","slug":"configuring-your-model"},{"level":2,"title":"Getting translated values","slug":"getting-translated-values"},{"level":2,"title":"Setting translated values","slug":"setting-translated-values"},{"level":2,"title":"Do not automatically translate","slug":"do-not-automatically-translate"}],"relativePath":"bread/multilanguage.md","lastUpdated":1640692859382}',s={},l=n(`<h1 id="multilanguage" tabindex="-1">Multilanguage <a class="header-anchor" href="#multilanguage" aria-hidden="true">#</a></h1><p>Voyager was built with multi-language support in mind. Translatable fields will be stored as JSON in your database.<br> In order to query (search) for translated values, Voyager uses the JSON SQL syntax and therefore needs <em>real</em> JSON columns to function properly.</p><h2 id="configuring-your-model" tabindex="-1">Configuring your model <a class="header-anchor" href="#configuring-your-model" aria-hidden="true">#</a></h2><div class="language-php"><pre><code><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
  
  <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\\</span>Models</span><span class="token punctuation">;</span>
  
  <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\\</span>Database<span class="token punctuation">\\</span>Eloquent<span class="token punctuation">\\</span>Model</span><span class="token punctuation">;</span>
  <span class="token keyword">use</span> <span class="token package">VoyagerAdmin<span class="token punctuation">\\</span>Voyager<span class="token punctuation">\\</span>Traits<span class="token punctuation">\\</span>Translatable</span><span class="token punctuation">;</span>
  
  <span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
  <span class="token punctuation">{</span>
      <span class="token keyword">use</span> <span class="token package">Translatable</span><span class="token punctuation">;</span>
  
      <span class="token keyword">public</span> <span class="token variable">$translatable</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">&#39;my_field&#39;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  </span></code></pre></div><h2 id="getting-translated-values" tabindex="-1">Getting translated values <a class="header-anchor" href="#getting-translated-values" aria-hidden="true">#</a></h2><p>To get the translated value of a field in the default locale, you can simply access the property:</p><p><code>$myModel-&gt;my_field</code></p><p>To get the value in a locale which is not the default call:</p><p><code>$myModel-&gt;getTranslated(&#39;my_field&#39;, &#39;de&#39;, &#39;en&#39;, &#39;Default value&#39;)</code></p><p>The first parameter is the field you want to access, the second the locale you want to get the value in, the third the fallback locale if a translation does not exist in the wanted locale and the firth parameter is the value which will be returned when none of the locales exist.</p><h2 id="setting-translated-values" tabindex="-1">Setting translated values <a class="header-anchor" href="#setting-translated-values" aria-hidden="true">#</a></h2><p>The current locale of a property can be set as normal:</p><p><code>$myModel-&gt;my_field = &#39;New value&#39;</code></p><p>To set another locale, call:</p><p><code>$myModel-&gt;setTranslated(&#39;my_field&#39;, &#39;Neuer Wert&#39;, &#39;de&#39;)</code></p><h2 id="do-not-automatically-translate" tabindex="-1">Do not automatically translate <a class="header-anchor" href="#do-not-automatically-translate" aria-hidden="true">#</a></h2><p>Sometimes it is useful to not automatically translate properties when accessing them. This can be done by calling <code>dontTranslate()</code> on your model. To re-activate automatic translations, call <code>translate()</code>. After calling <code>dontTranslate()</code> you will get an array containing locale =&gt; value pairs, for example:</p><div class="language-"><pre><code>[
      &quot;en&quot; =&gt; &quot;Value&quot;,
      &quot;de&quot; =&gt; &quot;Wert&quot;
  ]
  </code></pre></div>`,18),o=[l];function p(c,i,r,u,d,g){return t(),e("div",null,o)}var k=a(s,[["render",p]]);export{m as __pageData,k as default};
  