2024-09-11 17:37:59 +03:00
|
|
|
import { lstat } from 'node:fs'
|
2023-02-15 22:15:11 +03:00
|
|
|
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
2024-02-24 19:46:14 +03:00
|
|
|
import { currentLocales } from '../config/i18n'
|
2023-02-15 22:15:11 +03:00
|
|
|
|
|
|
|
const virtual = 'virtual:emoji-mart-lang-importer'
|
|
|
|
const resolvedVirtual = `\0${virtual}.mjs`
|
|
|
|
|
|
|
|
export default defineNuxtModule({
|
|
|
|
meta: {
|
|
|
|
name: 'emoji-mark-translation',
|
|
|
|
},
|
|
|
|
setup(_, nuxt) {
|
|
|
|
const resolver = createResolver(import.meta.url)
|
|
|
|
nuxt.hook('vite:extendConfig', async (viteInlineConfig) => {
|
|
|
|
viteInlineConfig.plugins = viteInlineConfig.plugins || []
|
|
|
|
viteInlineConfig.plugins.push({
|
|
|
|
name: 'vite-emoji-mart-translation',
|
|
|
|
enforce: 'pre',
|
|
|
|
resolveId(id) {
|
|
|
|
if (id === virtual)
|
|
|
|
return resolvedVirtual
|
|
|
|
},
|
|
|
|
async load(id) {
|
|
|
|
if (id === resolvedVirtual) {
|
|
|
|
const locales = await Promise.all(
|
|
|
|
Array
|
2024-02-24 19:46:14 +03:00
|
|
|
.from(new Set((currentLocales).map(l => l.code.split('-')[0])))
|
2023-02-15 22:15:11 +03:00
|
|
|
.map(async (l) => {
|
|
|
|
const exists = await isFile(resolver.resolve(`../node_modules/@emoji-mart/data/i18n/${l}.json`))
|
|
|
|
return [l, exists] as [code: string, exists: boolean]
|
2024-03-05 17:48:58 +03:00
|
|
|
}),
|
|
|
|
)
|
2023-02-15 22:15:11 +03:00
|
|
|
.then(l => l.filter(l => l[1]).map(l => l[0]))
|
|
|
|
const switchStmt = locales.filter(l => l[1]).map((l) => {
|
|
|
|
return `
|
|
|
|
case "${l}":
|
|
|
|
return import("@emoji-mart/data/i18n/${l}.json").catch(() => {});`
|
|
|
|
})
|
|
|
|
return `export default function(lang) {
|
|
|
|
switch(lang) {
|
|
|
|
${switchStmt.join('')}
|
|
|
|
default:
|
|
|
|
return import("@emoji-mart/data/i18n/en.json").catch(() => {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
async function isFile(path: string) {
|
|
|
|
return new Promise<boolean>((resolve) => {
|
2024-09-11 17:37:59 +03:00
|
|
|
lstat(path, (err, stats) => {
|
2023-02-15 22:15:11 +03:00
|
|
|
if (err)
|
|
|
|
resolve(false)
|
|
|
|
else
|
|
|
|
resolve(stats.isFile())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|