vue i18n- js文件中具有翻译变量的对象

发布于 2025-02-07 11:08:02 字数 1270 浏览 1 评论 0原文

我正在与Vue,Vue I18N和Quasar合作。

因此,我有一个JS文件,该文件包含一个带有ID和名称的对象,我在Q-SELECT选项上使用它。我希望当我更改语言时(在语言下拉下)时,几个月的标签名称也会更改。但这只有在我刷新页面时才发生。我在其他组件中使用相同的JS文件

VUE组件:

<q-select
  v-model="monthValue"
  :options="monthOptions()"
  map-options
  emit-value
  option-value="id"
  option-label="name"
  outlined
  dense
/>
import {getMonths} from "../../components/basic/Months.js";
computed: {
monthOptions() {
      return getMonths;
    },

montrys.js

import { i18n } from "../../boot/i18n.js";

export const getMonths = () => [
  { id: "January", name: i18n.t("MONTHS.JANUARY") },
  { id: "February", name: i18n.t("MONTHS.FEBRUARY") },
  { id: "March", name: i18n.t("MONTHS.MARCH") },
  { id: "April", name: i18n.t("MONTHS.APRIL") },
  { id: "May", name: i18n.t("MONTHS.MAY") },
  { id: "June", name: i18n.t("MONTHS.JUNE") },
  { id: "July", name: i18n.t("MONTHS.JULY") },
  { id: "August", name: i18n.t("MONTHS.AUGUST") },
  { id: "September", name: i18n.t("MONTHS.SEPTEMBER") },
  { id: "October", name: i18n.t("MONTHS.OCTOBER") },
  { id: "November", name: i18n.t("MONTHS.NOVEMBER") },
  { id: "December", name: i18n.t("MONTHS.DECEMBER") }
];

我尝试了很多事情,但是无济于事。有人有建议吗?谢谢。

i'm working with Vue, Vue i18n and Quasar.

So, i have an js file that contains an object with id and name, which i use on q-select options. I want that when i change the language (on language drop-down), the label name from months changes too. But this happens only if i refresh the page. I use the same js file in other components

Vue component:

<q-select
  v-model="monthValue"
  :options="monthOptions()"
  map-options
  emit-value
  option-value="id"
  option-label="name"
  outlined
  dense
/>
import {getMonths} from "../../components/basic/Months.js";
computed: {
monthOptions() {
      return getMonths;
    },

Months.js

import { i18n } from "../../boot/i18n.js";

export const getMonths = () => [
  { id: "January", name: i18n.t("MONTHS.JANUARY") },
  { id: "February", name: i18n.t("MONTHS.FEBRUARY") },
  { id: "March", name: i18n.t("MONTHS.MARCH") },
  { id: "April", name: i18n.t("MONTHS.APRIL") },
  { id: "May", name: i18n.t("MONTHS.MAY") },
  { id: "June", name: i18n.t("MONTHS.JUNE") },
  { id: "July", name: i18n.t("MONTHS.JULY") },
  { id: "August", name: i18n.t("MONTHS.AUGUST") },
  { id: "September", name: i18n.t("MONTHS.SEPTEMBER") },
  { id: "October", name: i18n.t("MONTHS.OCTOBER") },
  { id: "November", name: i18n.t("MONTHS.NOVEMBER") },
  { id: "December", name: i18n.t("MONTHS.DECEMBER") }
];

I've tried so many things, but to no avail.. Does anyone have suggestions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2025-02-14 11:08:02

月份在计算属性之外定义了t的反应性。数组需要在montroptions中创建。如果在组件之间重复使用,则月份。js应该导出一个返回数组而不是数组的函数:

export const getMonths = () => [{...}]

That months is defined outside computed property disables the reactivity of t. The array needs to be created inside monthOptions. If it's reused between components, Months.js should export a function that returns an array instead of an array:

export const getMonths = () => [{...}]
转身以后 2025-02-14 11:08:02

因此,我像Estus所说的那样尝试过,但是我不知道为什么不起作用。所以,我找到了另一种方法。
我离开了翻译发生在组件中。这样:

const getMonths = () => [
  { id: "January", name: "MONTHS.JANUARY" },
  { id: "February", name: "MONTHS.FEBRUARY" },
  { id: "March", name: "MONTHS.MARCH" },
  { id: "April", name: "MONTHS.APRIL" },
  { id: "May", name: "MONTHS.MAY" },
  { id: "June", name: "MONTHS.JUNE" },
  { id: "July", name: "MONTHS.JULY" },
  { id: "August", name: "MONTHS.AUGUST" },
  { id: "September", name: "MONTHS.SEPTEMBER" },
  { id: "October", name: "MONTHS.OCTOBER" },
  { id: "November", name: "MONTHS.NOVEMBER" },
  { id: "December", name: "MONTHS.DECEMBER" }
];

谢谢

computed:
monthOptions() {
      return getMonths().map(months => ({
      ...months,
      name: this.$t(months.name)  
      }))
    },

so i tried like Estus said, but i don't know why didn't work. So, i find another way.
I left the translation happens in the component. Like this:

const getMonths = () => [
  { id: "January", name: "MONTHS.JANUARY" },
  { id: "February", name: "MONTHS.FEBRUARY" },
  { id: "March", name: "MONTHS.MARCH" },
  { id: "April", name: "MONTHS.APRIL" },
  { id: "May", name: "MONTHS.MAY" },
  { id: "June", name: "MONTHS.JUNE" },
  { id: "July", name: "MONTHS.JULY" },
  { id: "August", name: "MONTHS.AUGUST" },
  { id: "September", name: "MONTHS.SEPTEMBER" },
  { id: "October", name: "MONTHS.OCTOBER" },
  { id: "November", name: "MONTHS.NOVEMBER" },
  { id: "December", name: "MONTHS.DECEMBER" }
];

And

computed:
monthOptions() {
      return getMonths().map(months => ({
      ...months,
      name: this.$t(months.name)  
      }))
    },

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文