简体   繁体   English

vue v-for:在 vue i18n 中使用 switch 进行翻译

[英]vue v-for: translate with switch in vue i18n

I have a problem to translate the information with vue i18n that is called through "v-for", all the data from the template is translated without problem, but those that I export through arrays and scripts do not render我在使用通过“v-for”调用的 vue i18n 翻译信息时遇到问题,模板中的所有数据都没有问题地翻译,但是我通过 arrays 导出的那些和脚本不呈现

i am using vue-i18n: version 7.8.1我正在使用 vue-i18n:版本 7.8.1

You're only ever setting the helpTitles property when your component is created.您只需要在创建组件时设置helpTitles属性。

I would suggest using $t() in your templates instead of within data() .我建议在模板中使用$t()而不是在data()中。 Then it will automatically react to changes.然后它会自动对变化做出反应。

I honestly don't think using an array from the translation file is a great idea.老实说,我不认为使用翻译文件中的数组是个好主意。 I'd be more inclined to add them with their own keys, just like your question and info translation keys, eg我更倾向于使用自己的密钥添加它们,就像您的问题和信息翻译密钥一样,例如

helpStartedTitle: "GETTING STARTED - MODEL",
helpMembersTitle: "MEMBERS",
helpAccountTitle: "ACCOUNT",
//etc

You could then set up the keys in your data like this然后您可以像这样在数据中设置密钥

data: () => {
  const keys = [
    "helpStarted",
    "helpMembers",
    "helpAccount", 
    "helpPayment", 
    "helpSocial", 
    "helpFraud", 
    "helpSupport",
    "helpStudio"
  ]

  return {
    helpInfo: keys.map((key, id) => ({
      id,
      title: `general.help.${key}Title`,
      question: `general.help.${key}`,
      answer: `general.help.${key}Info`
    }))
  }
}

then in your template然后在你的模板中

<div v-for="help in helpInfo" :key="help.id">
  <div :id="help.id" class="help-subtitle">{{ $t(help.title) }}:</div>
  <HelpItem
    :question="$t(help.question)"
    :answer="$t(help.answer)"
    :num="help.id"
  />
</div>

Even better would be to just pass the translation keys through to your HelpItem component and use $t() with it.更好的方法是将翻译键传递给您的HelpItem组件并使用$t()

<HelpItem
  :question="help.question"
  :answer="help.answer"
  :num="help.id"
/>

and in the HelpItem component并在HelpItem组件中

export default {
  name: "HelpItem",
  props: {
    question: String,
    answer: String,
    num: Number
  },
  // etc
}
<!-- just guessing here -->
<h1>{{ $t(question) }}</h1>
<p>{{ $t(answer) }}</p>

FYI, I've corrected answear to answer throughout.仅供参考,我已经更正了answear以在整个过程中answer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM