From 410965b3c8415473f83bffc0428f5aab55b8c318 Mon Sep 17 00:00:00 2001 From: Michel Edighoffer Date: Sun, 15 Jan 2023 02:10:41 +0100 Subject: [PATCH] feat: generate files --- config/i18n.ts | 7 +++---- modules/legacy-language.ts | 41 ++++++++++++++++++++++++++++++++++++++ nuxt.config.ts | 1 + 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 modules/legacy-language.ts 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,