Replies: 6 comments 3 replies
|
Hello @varna, I don't know if this will work for the server side but here is how I fixed the error Create a Nuxt3 plugin and add the following: You can then access your i18n Instance, example: I hope this can help you. |
|
In your case, this would be import { createI18n } from "vue-i18n";
import type { I18nOptions } from "vue-i18n";
import lt from "../../assets/locales/lt.json";
import en from "../../assets/locales/en.json";
const messages: I18nOptions["messages"] = Object.assign({
lt: lt,
en: en,
});
const i18n = createI18n({
legacy: false,
locale: "en",
fallbackLocale: "en",
messages,
});
export const { t, locale } = i18n.global;and your api event handler would be modified this way: export default defineEventHandler(async (event) => {
if (!event.context.params?.locale) {
throw createError({
statusCode: 400,
statusMessage: "locale is not provided",
});
}
locale.value = event.context.params?.locale
const categories = await $fetch(`/api/categories`)
for (const id in categories) {
categories[id].name = t(categories[id].name) // t is auto-imported
}
return categories;
});This might not be the most performant way to proceed as I'm not fully acquainted with the way Nitro/Nuxt is structured, but it works. |
|
I just did this: Avoiding the the module. |
|
This is working for me: export default defineEventHandler(async (event) => { |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'd like to translate some server API calls:
Problems
useI18nis undefinedMust be called at the top of a 'setup' functiont(key, locale)/assets, i.e. with config:References (tag later)
https://stackoverflow.com/questions/74995135/how-to-use-usei18n-on-server-api-directory
All reactions