I am in the process of configuring a global head setup in Nuxt for my application, which will be customized by some subpages. The content within this global head needs to be translated.
To achieve this, I have created a file called seoHead.js
with the following code:
import Vue from "vue";
export const $t = (sign) => Vue.prototype.$nuxt.$options.i18n.t(sign);
export default {
title: $t("seoGlobal.title"),
meta: [
{ charset: "utf-8" },
{ name: "viewport", content: "width=device-width, initial-scale=1" },
{
hid: "description",
name: "description",
content: $t("seoGlobal.description"),
},
{
hid: "ogSiteName",
name: "og:site_name",
content: "Test Page",
},
{
hid: "ogTitle",
name: "og:title",
content: $t("seoGlobal.ogTitle"),
},
(...)
],
};
I then import and utilize this data in my index.vue
and other pages like so:
import seoHead from "~/constants/seoHead";
export default {
head() {
const metaI18n = this.$nuxtI18nSeo();
const currentPath = process.env.LP_URL + this.$router.currentRoute.fullPath;
return {
...seoHead,
meta: [
{
hid: "ogLocale",
name: "og:locale",
content: metaI18n.meta[0].content,
},
{
hid: "ogLocaleAlternate",
name: "og:locale:alternate",
content: metaI18n.meta[1].content,
},
{
hid: "ogUrl",
name: "og:url",
content: currentPath,
},
],
};
},
(...)
However, I have encountered an issue with the error message
Cannot read property '$options' of undefined
. This is puzzling to me as I have already used the code export const $t = (sign) => Vue.prototype.$nuxt.$options.i18n.t(sign);
in another JavaScript file. Does anyone have insights on why this error is occurring? Is there a better approach to translating global head options?