vue 3- usei18n- untureck的语法:必须在“设置”函数的顶部调用

发布于 2025-02-08 07:42:48 字数 2349 浏览 9 评论 0 原文

我对无法解决的 usei18n 有问题。无论我做什么,我都无法使i18n翻译工作,并且在我的控制台上永久看到此消息:

unduarking语法:必须在设置的顶部调用函数

堆栈轨道的函数,即当调用 usei> usei18n()函数时,它会发生这种情况,尽管它在一个称为<的函数范围内代码>设置。下一个级别的堆栈跟踪揭示了``usei18n()``函数在``函数''函数中,由于无法检测到我的应用程序的实例而提高异常。

function useI18n(options = {}) {
    const instance = getCurrentInstance();
    if (instance == null) {
        throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
    }
...

我的代码如下:

main.js

// frontend/src/main.ts

import i18n from './i18n';

import Vue, { createApp } from 'vue';

import axios from 'axios';
import VueAxios from 'vue-axios';

import App from './App.vue';

//Vue.use(VueI18n);

const app = createApp(App);
app.use(VueAxios, axios, i18n);
app.provide('axios', app.config.globalProperties.axios);
app.mount('#i-english-editor');

console.log(app);

i18n.ts

import { createI18n } from "vue-i18n";

const i18n = createI18n({
  legacy: false,
  locale: "ja",
  fallbackLocale: 'en',
  globalInjection: true,
  messages: {
    en: {
      message: {
        language: "English",
        greeting: "Hello !"
      }
    },
    ar: {
      message: {
        language: "العربية",
        greeting: "السلام عليكم"
      }
    },
    es: {
      message: {
        language: "Español",
        greeting: "Hola !"
      }
    }
  }
});

export default i18n;

app.Vue,

<div>
{{ t('message.greeting') }}
</div>
...
<script lang="ts">
import {defineComponent, ref, inject} from "vue";
import { useI18n } from "vue-i18n";

export default defineComponent({
  setup() {
    const { t } = useI18n();

...


    return {
      ...
      {t},
    }
  }
});
</script>

如您所见,我的代码看起来像是在线上的各种示例,向我展示了如何使翻译工作。我看过各种解决方案,它们都不为我工作。这包括尝试设置 GlobalInction 的替代方法 true 和使用 {{$ t('Message.greeting')}}}} 揭示了错误:

ctx。$ t不是函数

我实际上是死胡同,无法找到似乎对大多数人有用的解决方案。我的Vue版本是VUE 3,我正在使用的I18N版本是

如果有人知道为什么这会发生在我身上,但没有其他人,如果您有解决方案,我会很感激。

I have a problem with useI18n that I'm unable to solve. Whatever I do, I am unable to get i18n translation to work and in my console perpetually see this message:

Uncaught SyntaxError: Must be called at the top of a setup function

The stack-track reveals that it happens when invoking the useI18n() function despite it being within a function called setup. The next level up the stack trace reveals that within the ```useI18n()`` function that an exception is being raised due to it not detecting an instance of my app.

function useI18n(options = {}) {
    const instance = getCurrentInstance();
    if (instance == null) {
        throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
    }
...

My code is as follows:

main.js

// frontend/src/main.ts

import i18n from './i18n';

import Vue, { createApp } from 'vue';

import axios from 'axios';
import VueAxios from 'vue-axios';

import App from './App.vue';

//Vue.use(VueI18n);

const app = createApp(App);
app.use(VueAxios, axios, i18n);
app.provide('axios', app.config.globalProperties.axios);
app.mount('#i-english-editor');

console.log(app);

i18n.ts

import { createI18n } from "vue-i18n";

const i18n = createI18n({
  legacy: false,
  locale: "ja",
  fallbackLocale: 'en',
  globalInjection: true,
  messages: {
    en: {
      message: {
        language: "English",
        greeting: "Hello !"
      }
    },
    ar: {
      message: {
        language: "العربية",
        greeting: "السلام عليكم"
      }
    },
    es: {
      message: {
        language: "Español",
        greeting: "Hola !"
      }
    }
  }
});

export default i18n;

App.vue

<div>
{{ t('message.greeting') }}
</div>
...
<script lang="ts">
import {defineComponent, ref, inject} from "vue";
import { useI18n } from "vue-i18n";

export default defineComponent({
  setup() {
    const { t } = useI18n();

...


    return {
      ...
      {t},
    }
  }
});
</script>

As you can see, my code looks like the various examples online showing me how to get translations working. I've looked at various solutions and none of them are working for me. This includes trying an alternative method of setting globalInjection to true and using {{ $t('message.greeting') }} in a tag which reveals the error:

ctx.$t is not a function

I am literally at a dead end and can't figure out a solution that seems to work for most people. My version of Vue is Vue 3 and the version of i18n I'm using is [email protected].

If anyone knows why this is happening to me but no-one else, I'd appreciate it if you have a solution.

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

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

发布评论

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

评论(3

半世蒼涼 2025-02-15 07:42:48

这是一个古老的问题,但是今天我发现了同样的问题,并且在网上尚不清楚静止。

似乎Usei18n()是由组成API实现的组合功能,必须在设置函数的顶部调用,以在没有设置的情况下使用TS文件,我们需要使用USENUXTAPP()添加上下文。

export default (): string => {
  const { $i18n } = useNuxtApp();
  const t = $i18n.t;

  return t('my-translation-key');
};

我希望有帮助。

This is an old question, but today I found the same problem and stills to be not clear on the web.

It seems that the useI18n() is a composal function that is implemented by Composition API, must be called at the top of a setup function, to use on a TS file without a setup we need to use the useNuxtApp() to add the context.

export default (): string => {
  const { $i18n } = useNuxtApp();
  const t = $i18n.t;

  return t('my-translation-key');
};

I hope helps.

缘字诀 2025-02-15 07:42:48

更新 - 我设法解决了它。

首先,我摆脱了使用 usei18n 函数的需要,并意识到 $ t 由于相同的 app.use中的多个导入而导致失败。 ...)函数。

我将其更改为:

app.use(VueAxios, axios);
app.use(i18n as any);

app.use 正在将更多的错误扔到控制台中,因为 i18n 不是接受的类型。为了解决这个问题,我从另一个帖子的解决方案中发现了将添加为任何的解决方案,这是解决此问题的一种方式。结果, {{$ t(...)}} 现在正在产生所需的结果。

UPDATE - I managed to solve it.

To start with, I got rid of the need to use the useI18n function and realised that $t was failing due to multiple imports within the same app.use(...) function.

I changed it to:

app.use(VueAxios, axios);
app.use(i18n as any);

app.use was throwing up loads more errors into the console as i18n wasn't an accepted type. To get around the issue, I found out from a solution from another post that adding as any to it is a way around this problem. As a result {{ $t(...) }} is now producing the desired result.

无悔心 2025-02-15 07:42:48

我遇到了同样的问题,但对我而言,事实证明这是矛盾的VUE版本。
我的依赖性使用了VUE 3.5.3,并且也将VUE 3.2.41作为直接依赖性。

作为解决方案,我选择保持直接依赖性,但将其放在与其他间接依赖性相同的VUE版本上(3.5.3)。保持直接依赖性的原因是,它允许Intellij Idea索引软件包并具有自动完成。

I had the same problem but for me it turned out to be conflicting Vue versions.
I had a dependency that used Vue 3.5.3 and i also had Vue 3.2.41 as a direct dependency.

As a solution i chose to keep my direct dependency but put it on the same Vue version as the other indirect dependency (3.5.3). The reason for keeping the direct dependency is that it allows IntelliJ IDEA to index the package and to have auto-completion.

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