diff --git a/config/i18n.ts b/config/i18n.ts index 3c829530..38af54d8 100644 --- a/config/i18n.ts +++ b/config/i18n.ts @@ -2,9 +2,8 @@ import type { NuxtI18nOptions } from '@nuxtjs/i18n' import type { DateTimeFormats, NumberFormats, PluralizationRule, PluralizationRules } from '@intlify/core-base' import type { LocaleObject } from '#i18n' -import { satisfies } from '~~/mocks/semver' -interface InclusiveLocaleKey { +export interface InclusiveLocaleKey { adoptInclusiveWriting?: boolean inclusiveTransform?: (term: string) => string } @@ -16,7 +15,7 @@ declare module '#i18n' { } } -const locales: LocaleObject[] = [ +export const locales: LocaleObject[] = [ { code: 'en-US', file: 'en-US.json', @@ -73,7 +72,7 @@ const locales: LocaleObject[] = [ name: 'Français', adoptInclusiveWriting: true, inclusiveTransform(term: string) { - return term.replace(/·\w+·?/, '') + return term.replaceAll(/·\w+·?/g, '') }, }, { diff --git a/modules/legacy-language.ts b/modules/legacy-language.ts new file mode 100644 index 00000000..b4f6f1e2 --- /dev/null +++ b/modules/legacy-language.ts @@ -0,0 +1,41 @@ +import { readFile, writeFile } from 'node:fs/promises' +import { defineNuxtModule, useNuxt } from '@nuxt/kit' +import type { InclusiveLocaleKey } from '../config/i18n' +import { locales } from '../config/i18n' +import type { LocaleObject } from '#i18n' + +type LocaleObjectWithRawFile = Omit & { + file: Buffer + inclusiveTransform: InclusiveLocaleKey['inclusiveTransform'] +} + +function languageTransformation(locale: LocaleObjectWithRawFile) { + return locale.inclusiveTransform!(locale.file.toString()) +} + +export default defineNuxtModule({ + meta: { name: 'legacy-language' }, + setup() { + const nuxt = useNuxt() + nuxt.hook('nitro:init', (nitro) => { + const dir = `${nitro.options.output.dir}/public` + nitro.hooks.hook('compiled', async () => { + // spot language which needs dot-syntax free alternative + const inclusiveSyntaxLanguages = locales.filter(locale => locale.adoptInclusiveWriting && !!locale.inclusiveTransform && locale.file && locale.code).map(async locale => ({ + ...locale, + file: await readFile(`locales/${locale.file!}`), + })) + + const localesFiles = await Promise.all(inclusiveSyntaxLanguages) as LocaleObjectWithRawFile[] + const transformedFiles = localesFiles + .map(locale => ({ ...locale, file: languageTransformation(locale) })) + .map(async (locale) => { + // @ts-expect-error locales with raw file need right types + await writeFile(`${dir}/${locale.code}-ninc.json`, locale.file) + }) + + Promise.all(transformedFiles) + }) + }) + }, +}) diff --git a/nuxt.config.ts b/nuxt.config.ts index 62be1b68..087da42f 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -31,6 +31,7 @@ export default defineNuxtConfig({ '~/modules/tauri/index', '~/modules/pwa/index', // change to '@vite-pwa/nuxt' once released and remove pwa module '~/modules/stale-dep', + '~/modules/legacy-language', ], experimental: { payloadExtraction: false,