UNPKG

@nuxtjs/i18n

Version:

Internationalization for Nuxt

31 lines (30 loc) 1.04 kB
import { defineNuxtPlugin, useNuxtApp } from "#imports"; import { useSwitchLocalePath } from "#i18n"; const identifier = __SWITCH_LOCALE_PATH_LINK_IDENTIFIER__; const switchLocalePathLinkWrapperExpr = new RegExp( [`<!--${identifier}-\\[(\\w+)\\]-->`, `.+?`, `<!--/${identifier}-->`].join(""), "g" ); export default defineNuxtPlugin({ name: "i18n:plugin:switch-locale-path-ssr", dependsOn: ["i18n:plugin"], setup(_nuxt) { const nuxt = useNuxtApp(_nuxt._id); const switchLocalePath = useSwitchLocalePath(nuxt); nuxt.hook("app:rendered", (ctx) => { if (ctx.renderResult?.html == null) { return; } ctx.renderResult.html = ctx.renderResult.html.replaceAll( switchLocalePathLinkWrapperExpr, (match, p1) => { const encoded = encodeURI(switchLocalePath(p1 ?? "")); return match.replace( /href="([^"]+)"/, `href="${encoded || "#"}" ${!encoded && __I18N_STRICT_SEO__ ? "data-i18n-disabled" : ""}` ); } ); }); } });