Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 1x 8x 13x 8x 11x 1x 1x 8x 1x 1x 12x 8x 1x 14x 14x 14x 8x 8x 8x 8x 8x 3x 4x 8x | import {
addServerHandler,
createResolver,
defineNuxtModule,
// eslint-disable-next-line sonarjs/deprecation -- moving to moduleDependencies is its own change
installModule,
useLogger,
} from "@nuxt/kit";
import type { Nuxt } from "@nuxt/schema";
import type { LocaleObject } from "@nuxtjs/i18n";
import { UndefinedVendorError } from "#core/domain/errors";
import { isVendorName, VENDOR_NAMES } from "#core/registry";
import { TRANSLATIONS_API_PATH, type TranslationsConfig } from "./config";
const getLocaleCodes = ({ options }: Nuxt): string[] => {
const locales = options._layers.flatMap<string | LocaleObject>(
(layer) => layer.config.i18n?.locales ?? [],
);
return [
...new Set(locales.map((locale) => (typeof locale === "string" ? locale : locale.code))),
];
};
const logger = useLogger("@joinery/i18n");
const warnAboutLocales = (locales: string[], { options }: Nuxt): void => {
if (!locales.length) {
logger.warn(
"No locales declared under `i18n.locales`, so no loader was registered and nothing will translate.",
);
return;
}
const defaultLocale = options._layers.find((layer) => layer.config.i18n?.defaultLocale)?.config
.i18n?.defaultLocale;
if (defaultLocale && !locales.includes(defaultLocale)) {
logger.warn(
`\`i18n.defaultLocale\` is "${defaultLocale}", which is not one of the declared locales (${locales.join(", ")}).`,
);
}
};
export default defineNuxtModule<TranslationsConfig>({
meta: { name: "@joinery/i18n/nuxt", configKey: "translations" },
setup: async (resolvedOptions, nuxt) => {
const resolver = createResolver(import.meta.url);
const name = resolvedOptions.vendor?.name;
if (!isVendorName(name)) throw new UndefinedVendorError(name, VENDOR_NAMES);
const locales = getLocaleCodes(nuxt);
warnAboutLocales(locales, nuxt);
nuxt.options.runtimeConfig.translations = { ...resolvedOptions, locales };
// BFF
addServerHandler({
route: `${TRANSLATIONS_API_PATH}/:locale`,
method: "get",
handler: resolver.resolve("./runtime/server/translations.get"),
});
// Locale loader
nuxt.hook("i18n:registerModule", (register) => {
register({
langDir: resolver.resolve("./runtime/locales"),
locales: locales.map((code) => ({ code, file: "loader.ts" })),
});
});
// eslint-disable-next-line sonarjs/deprecation -- moving to moduleDependencies is its own change
await installModule("@nuxtjs/i18n");
},
});
|