xiCO2k / laravel-vue-i18n

Allows to connect your `Laravel` Framework translation files with `Vue`.
MIT License
594 stars 49 forks source link

Function trans in script setup doesn't show translated value when using php translations #107

Closed kskrlinnorth2 closed 1 year ago

kskrlinnorth2 commented 1 year ago

We used this package in previous project, but there we used json translations. It turned out it wasn't easy to maintain or organize so we switched to only php translations for next project. However the trans() method doesn't return translated string, only key,

When using trans and $t in template for same translation key, it works.

Using L10 with Inertia and vue3.

app.js image

lang/en/app.php translations image

some code that uses those translations image

All i get when I output name, description and btnLabel from settingsNav are translation keys.

Code is inside persistent layout under script setup.

Is there any workaround, other than sending my settingsNav as prop from Laravel controller?

xiCO2k commented 1 year ago

are you using the vite plugin to import the php translations?

kskrlinnorth2 commented 1 year ago

This is my vite config

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import i18n from "laravel-vue-i18n/vite";
import laravel from "laravel-vite-plugin";

export default defineConfig({
  plugins: [
    laravel({
      input: ["resources/css/datepicker.scss", "resources/js/app.js"],
      ssr: "resources/js/ssr.js",
      refresh: true,
    }),
    vue({
      template: {
        transformAssetUrls: {
          base: null,
          includeAbsolute: false,
        },
      },
    }),
    i18n(),
  ],
  ssr: {
    noExternal: ["@inertiajs/server"],
  },
});
xiCO2k commented 1 year ago

humm can you try to use wTrans instead?

kskrlinnorth2 commented 1 year ago

It works, but it outputs my translations with extra double quotes around it.

<li v-for="item in settingsNav" :key="item.name">{{ item.name }}</li> Gives me this output with quotes visible: "Some value"

<li v-for="item in settingsNav" :key="item.name">{{ item.name.value }}</li> Gives me output without quotes

My workaround was not to use trans and instead just pass translation key and in my template just output it with $t(item.name) which would return correct translation (without quotes) and without need of extra value call.

xiCO2k commented 1 year ago

yes that's the way to go, and the way this package was designed to work.

Thanks for the feedback @kskrlinnorth2