@nuxtjs/i18n
Version:
Internationalization for Nuxt
46 lines (45 loc) • 1.74 kB
JavaScript
import { localeCodes, localeLoaders, normalizedLocales } from "#build/i18n-options.mjs";
import { isArray, isString } from "@intlify/shared";
export function createLocaleConfigs(fallbackLocale) {
const localeConfigs = {};
for (const locale of localeCodes) {
const fallbacks = getFallbackLocaleCodes(fallbackLocale, [locale]);
const cacheable = isLocaleWithFallbacksCacheable(locale, fallbacks);
localeConfigs[locale] = { fallbacks, cacheable };
}
return localeConfigs;
}
function getFallbackLocaleCodes(fallback, locales) {
if (fallback === false) {
return [];
}
if (isArray(fallback)) {
return fallback;
}
let fallbackLocales = [];
if (isString(fallback)) {
if (locales.every((locale) => locale !== fallback)) {
fallbackLocales.push(fallback);
}
return fallbackLocales;
}
const targets = [...locales, "default"];
for (const locale of targets) {
if (locale in fallback == false) {
continue;
}
fallbackLocales = [...fallbackLocales, ...fallback[locale].filter(Boolean)];
}
return fallbackLocales;
}
export function isLocaleCacheable(locale) {
return localeLoaders[locale] != null && localeLoaders[locale].every((loader) => loader.cache !== false);
}
export function isLocaleWithFallbacksCacheable(locale, fallbackLocales) {
return isLocaleCacheable(locale) && fallbackLocales.every((fallbackLocale) => isLocaleCacheable(fallbackLocale));
}
export function getDefaultLocaleForDomain(host) {
return normalizedLocales.find((l) => !!l.defaultForDomains?.includes(host))?.code;
}
export const isSupportedLocale = (locale) => localeCodes.includes(locale || "");
export const resolveSupportedLocale = (locale) => isSupportedLocale(locale) ? locale : void 0;