Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mvnobrega's avatar

Multi-language site and translation

In some cases I will need to generate the translation within the Vuejs component. In others within the blade template.

I need to resolve this with a single translation file that works on both sides. If I use vue-i18n, I will only be able to translate in the Vuejs component, the native blade translation will only work in Blade.

To avoid having two ways to translate, how can I best solve this?

0 likes
9 replies
mvnobrega's avatar

That way doesn't work very well for me.

I am trying as follows:

my blade.php --> <nav-bar lang="{{ json_encode(__('lang')) }}"></nav-bar>

my component:

<template>

<nav class="middle-section ">
        <ul>
            <li><a href="#">{{ lang.navbar.inicio }}</a></li>
        </ul>
    </nav>

</template>

But I have two problems. The first is that the return of the props comes like this: lang="{&quot;navbar&quot;:{&quot;inicio&quot;:&quot;In\u00edcio&quot;,&quot;contato&quot;:&quot;Contato&quot;,&quot;sonhos&quot;:&quot;Sonhos&quot;,&quot;blog&quot;:&quot;Blog&quot;,&quot;langIcon&quot;:&quot;flag-icon flag-br&quot;,&quot;meuPerfil&quot;:&quot;Meu Perfil&quot;,&quot;meusRelatos&quot;:&quot;Meus Relatos&quot;,&quot;msg&quot;:&quot;Mensagens&quot;,&quot;notif&quot;:&quot;Notifica\u00e7\u00f5es&quot;,&quot;prefer&quot;:&quot;Prefer\u00eancias&quot;,&quot;lang&quot;:&quot;Idioma:&quot;,&quot;sair&quot;:&quot;Sair&quot;},&quot;home&quot;:{&quot;headerH1&quot;:&quot;\u201c\u00c9 t\u00e3o incr\u00edvel;}}

The second problem is that I can't call the component like this: {{ lang.navbar.home }}

Any suggestion ?

piljac1's avatar
piljac1
Best Answer
Level 28

If you wish to keep your way of passing translations as a prop to your component instead of using Lang.js, simply add a : to lang so your JSON encoded data is processed as a JS object.

<nav-bar :lang="{{ json_encode(__('lang')) }}"></nav-bar>

As for the second part of the problem, have you defined lang as a prop in your component ?

<script>
  // ...
  export default {
    // ...
    props: {
      lang: {
        type: Object,
        required: true
      }
    }
    // ...
  }
</script>
1 like
mvnobrega's avatar

{!! json_encode(__('lang')) !!} Partly resolved. But now there seems to be an accentuation problem. Accents do not appear to be configured for UTF-8. Example: In\u00edcio

How do I solve this?

piljac1's avatar

@mvnobrega I deleted my previous answer because I looked at your issue too quickly. Check out my rewritten answer.

Basically omitting the : treats your content as a string, but adding : (which is a short for v-bind:) treats the content as JS logic.

mvnobrega's avatar

I defined the props as an object and put ":lang" but it doesn't work. I have the same problem of &quot and accentuation.

I ended up finding this alternative:

<nav-bar :lang="{!! json_encode(__('lang'), JSON_UNESCAPED_UNICODE) !!}"> </nav-bar>

But I can't access the translation this way: {{ lang.navbar.inicio }}

the generated json is as follows:

<nav-bar :lang="{"navbar":{"inicio":"Início","contato":"Contato","sonhos":"Sonhos","blog":"Blog","langIcon":"flag-icon flag-br","meuPerfil":"Meu Perfil","meusRelatos":"Meus Relatos","msg":"Mensagens","notif":"Notificações","prefer":"Preferências","lang":"Idioma:","sair":"Sair"}}"></nav-bar>

Any tips?

mvnobrega's avatar

It was my fault when publishing here. But the code is correct and the problem remains.

mvnobrega's avatar

Buddy, it worked! I don't know what I had done before that it wasn't working. I just remade and it worked perfectly.

Thank you very much

Please or to participate in this conversation.