简体   繁体   English

如何使用 typescript 访问 vue3 组件内的 i18n t function

[英]how to access the i18n t function inside a vue3 component using typescript

I am trying to write a control that can automatically handle interpolation for i18n strings:我正在尝试编写一个可以自动处理 i18n 字符串插值的控件:

<script lang="ts">

import { ref, defineComponent, inject } from "vue";
import type { InterpolationItem, InterpolationData, myData } from "../types/types";
import { useI18n } from 'vue-i18n'
    
export default defineComponent({
  name: "LocCtrl",
  props: {
    locKey: {
      type: String,
      required: true,
    },
    page: {
      type: String,
      required: true,
    },
  },
  setup() {
    // something has to be done here with useI18n(). If I put it at the top, the compiler
    // says it has to be here, but what?
    const { rt, t } = useI18n();

    const myData = inject("myData") as myData;
    const data = ref<InterpolationData>({
      items: [],
    });

    return {
      data,
      myData
    };
  },
  mounted() {
    //currently t (or whatever i need) is undefined
    this.data.items = this.interpolate( t(this.page + "." + this.locKey));
  },

The questions of what I'm trying to do are commented above上面评论了我正在尝试做的问题

There were several problems with my setup.我的设置有几个问题。 This project below gives you a project you can clone and copy configuration into your project, for example with Beyond Compare.下面的这个项目为您提供了一个项目,您可以将配置克隆和复制到您的项目中,例如使用 Beyond Compare。 You may have to use the package.json settings in this issue that I've posted on it so that npm install actually works.您可能必须在我发布的这个问题中使用 package.json 设置,这样 npm 安装才能真正起作用。

https://github.com/xiaoluoboding/vue-i18n-practice/issues https://github.com/xiaoluoboding/vue-i18n-practice/issues

I also had my hacky Locale.ts possibly interfering with the operation of i18n.我也有我的 hacky Locale.ts 可能会干扰 i18n 的操作。 Once completely removed i18n started working.完全删除后,i18n 开始工作。

But once installed properly this.$t and this.$i18n are available in components everywhere.但是一旦正确安装this.$tthis.$i18n就可以在任何地方的组件中使用。

mounted() {
    // just an example of changing the locale, you don't necessarily 
    // need to do this all the time:
    const storage = useLocalStorage("site_locale", "en-US");
    this.$i18n.locale = storage.value;

    // this calls my custom interpolation code, 
    // which is why I needed to do this
    this.data.items = 
      this.interpolate(this.getItem(this.page, this.locKey));
  },
  methods: {
    //functions that directly access i18n strings:
    getItem(page: string, key: string): string {
      return this.getLoc(page + "." + key);
    },
    getLoc(x: string): string {
      return this.$t(x);
    },

After you get i18n working, this example uses yaml for the loc file format, so using a yaml beautifier such as this one can catch formatting errors which have weird errors that tell you nothing when loading the locale files:使 i18n 工作后,此示例使用 yaml 作为 loc 文件格式,因此使用像这样的 yaml 美化器可以捕获格式错误,这些错误在加载语言环境文件时不会告诉您任何奇怪的错误:

https://codebeautify.org/yaml-beautifier https://codebeautify.org/yaml-美化器

Some lines have to have quotes on them if they contain certain characters such as: or brackets or @:lookupSomething.something references:如果某些行包含某些字符,例如:或括号或@:lookupSomething.something 引用,则某些行必须在其上加上引号:

xBox:
  NC: "@:common.NC"
  CP: Change X
  PE: Pre-exisiting Conditions
  xDesc: "[petData^xAge] [*ageUnit**xData^ageUnit] old [xData^breed]"
  SO: Start Over
  WP: Wrong X?

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

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