feat: generate files

This commit is contained in:
Michel Edighoffer 2023-01-15 02:10:41 +01:00
parent 863eab1598
commit 410965b3c8
3 changed files with 45 additions and 4 deletions

View file

@ -2,9 +2,8 @@ import type { NuxtI18nOptions } from '@nuxtjs/i18n'
import type { DateTimeFormats, NumberFormats, PluralizationRule, PluralizationRules } from '@intlify/core-base' import type { DateTimeFormats, NumberFormats, PluralizationRule, PluralizationRules } from '@intlify/core-base'
import type { LocaleObject } from '#i18n' import type { LocaleObject } from '#i18n'
import { satisfies } from '~~/mocks/semver'
interface InclusiveLocaleKey { export interface InclusiveLocaleKey {
adoptInclusiveWriting?: boolean adoptInclusiveWriting?: boolean
inclusiveTransform?: (term: string) => string inclusiveTransform?: (term: string) => string
} }
@ -16,7 +15,7 @@ declare module '#i18n' {
} }
} }
const locales: LocaleObject[] = [ export const locales: LocaleObject[] = [
{ {
code: 'en-US', code: 'en-US',
file: 'en-US.json', file: 'en-US.json',
@ -73,7 +72,7 @@ const locales: LocaleObject[] = [
name: 'Français', name: 'Français',
adoptInclusiveWriting: true, adoptInclusiveWriting: true,
inclusiveTransform(term: string) { inclusiveTransform(term: string) {
return term.replace(/·\w+·?/, '') return term.replaceAll(/·\w+·?/g, '')
}, },
}, },
{ {

View file

@ -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<LocaleObject, 'file'> & {
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)
})
})
},
})

View file

@ -31,6 +31,7 @@ export default defineNuxtConfig({
'~/modules/tauri/index', '~/modules/tauri/index',
'~/modules/pwa/index', // change to '@vite-pwa/nuxt' once released and remove pwa module '~/modules/pwa/index', // change to '@vite-pwa/nuxt' once released and remove pwa module
'~/modules/stale-dep', '~/modules/stale-dep',
'~/modules/legacy-language',
], ],
experimental: { experimental: {
payloadExtraction: false, payloadExtraction: false,