@nuxtjs/i18n
Version:
Internationalization for Nuxt
76 lines (75 loc) • 3.05 kB
JavaScript
import { isString } from "@intlify/shared";
import { localeCodes } from "#build/i18n.options.mjs";
import { useRuntimeConfig } from "#app";
export function getRouteName(routeName) {
if (isString(routeName)) return routeName;
if (routeName != null) return routeName.toString();
return "(null)";
}
export function getLocaleRouteName(routeName, locale, opts) {
const { defaultLocale, strategy, routesNameSeparator, defaultLocaleRouteNameSuffix, differentDomains } = opts;
const localizedRoutes = strategy !== "no_prefix" || differentDomains;
const name = getRouteName(routeName) + (localizedRoutes ? routesNameSeparator + locale : "");
if (locale === defaultLocale && strategy === "prefix_and_default") {
return name + routesNameSeparator + defaultLocaleRouteNameSuffix;
}
return name;
}
function matchBrowserLocale(locales, browserLocales) {
const matchedLocales = [];
for (const [index, browserCode] of browserLocales.entries()) {
const matchedLocale = locales.find((l) => l.language?.toLowerCase() === browserCode.toLowerCase());
if (matchedLocale) {
matchedLocales.push({ code: matchedLocale.code, score: 1 - index / browserLocales.length });
break;
}
}
for (const [index, browserCode] of browserLocales.entries()) {
const languageCode = browserCode.split("-")[0].toLowerCase();
const matchedLocale = locales.find((l) => l.language?.split("-")[0].toLowerCase() === languageCode);
if (matchedLocale) {
matchedLocales.push({ code: matchedLocale.code, score: 0.999 - index / browserLocales.length });
break;
}
}
return matchedLocales;
}
function compareBrowserLocale(a, b) {
if (a.score === b.score) {
return b.code.length - a.code.length;
}
return b.score - a.score;
}
export function findBrowserLocale(locales, browserLocales) {
const normalizedLocales = locales.map((l) => ({ code: l.code, language: l.language || l.code }));
const matchedLocales = matchBrowserLocale(normalizedLocales, browserLocales);
if (matchedLocales.length === 0) {
return "";
}
if (matchedLocales.length > 1) {
matchedLocales.sort(compareBrowserLocale);
}
return matchedLocales[0].code;
}
export function getLocalesRegex(localeCodes2) {
return new RegExp(`^/(${localeCodes2.join("|")})(?:/|$)`, "i");
}
const localesPattern = `(${localeCodes.join("|")})`;
export const regexpPath = getLocalesRegex(localeCodes);
export function createLocaleFromRouteGetter() {
const { routesNameSeparator, defaultLocaleRouteNameSuffix } = useRuntimeConfig().public.i18n;
const defaultSuffixPattern = `(?:${routesNameSeparator}${defaultLocaleRouteNameSuffix})?`;
const regexpName = new RegExp(`${routesNameSeparator}${localesPattern}${defaultSuffixPattern}$`, "i");
return (route) => {
if (isString(route)) {
return route.match(regexpPath)?.[1] ?? "";
}
if (route.name) {
return getRouteName(route.name).match(regexpName)?.[1] ?? "";
}
if (route.path) {
return route.path.match(regexpPath)?.[1] ?? "";
}
return "";
};
}