feat(i18n): add country variants support (#1370)
This commit is contained in:
parent
9d94a09319
commit
804f66f203
14 changed files with 1700 additions and 1945 deletions
|
@ -33,16 +33,18 @@ type RequiredWebManifestEntry = Required<WebManifestEntry & Pick<ExtendedManifes
|
|||
export const createI18n = async (): Promise<LocalizedWebManifest> => {
|
||||
const { env } = await getEnv()
|
||||
const envName = `${env === 'release' ? '' : `(${env})`}`
|
||||
const { pwa } = await readI18nFile('en-US.json')
|
||||
const { pwa } = await readI18nFile('en.json')
|
||||
|
||||
const defaultManifest: Required<WebManifestEntry> = pwa.webmanifest[env]
|
||||
|
||||
const locales: RequiredWebManifestEntry[] = await Promise.all(
|
||||
pwaLocales
|
||||
.filter(l => l.code !== 'en-US')
|
||||
.map(async ({ code, dir = 'ltr', file }) => {
|
||||
// read locale file
|
||||
const { pwa, app_name, app_desc_short } = await readI18nFile(file!)
|
||||
.map(async ({ code, dir = 'ltr', file, files }) => {
|
||||
// read locale file or files
|
||||
const { pwa, app_name, app_desc_short } = file
|
||||
? await readI18nFile(file)
|
||||
: await findBestWebManifestData(files, env)
|
||||
const entry: WebManifestEntry = pwa?.webmanifest?.[env] ?? {}
|
||||
if (!entry.name && app_name)
|
||||
entry.name = dir === 'rtl' ? `${envName} ${app_name}` : `${app_name} ${envName}`
|
||||
|
@ -157,3 +159,55 @@ async function readI18nFile(file: string) {
|
|||
await readFile(resolve(`./locales/${file}`), 'utf-8'),
|
||||
).toString())
|
||||
}
|
||||
|
||||
interface PWAEntry {
|
||||
webmanifest?: Record<string, {
|
||||
name?: string
|
||||
short_name?: string
|
||||
description?: string
|
||||
}>
|
||||
}
|
||||
|
||||
interface JsonEntry {
|
||||
pwa?: PWAEntry
|
||||
app_name?: string
|
||||
app_desc_short?: string
|
||||
}
|
||||
|
||||
async function findBestWebManifestData(files: string[], env: string) {
|
||||
const entries: JsonEntry[] = await Promise.all(files.map(async (file) => {
|
||||
const { pwa, app_name, app_desc_short } = await readI18nFile(file)
|
||||
return { pwa, app_name, app_desc_short }
|
||||
}))
|
||||
|
||||
let pwa: PWAEntry | undefined
|
||||
let app_name: string | undefined
|
||||
let app_desc_short: string | undefined
|
||||
|
||||
for (const entry of entries) {
|
||||
const webmanifest = entry?.pwa?.webmanifest?.[env]
|
||||
if (webmanifest) {
|
||||
if (pwa) {
|
||||
if (webmanifest.name)
|
||||
pwa.webmanifest![env].name = webmanifest.name
|
||||
|
||||
if (webmanifest.short_name)
|
||||
pwa.webmanifest![env].short_name = webmanifest.short_name
|
||||
|
||||
if (webmanifest.description)
|
||||
pwa.webmanifest![env].description = webmanifest.description
|
||||
}
|
||||
else {
|
||||
pwa = entry.pwa
|
||||
}
|
||||
}
|
||||
|
||||
if (entry.app_name)
|
||||
app_name = entry.app_name
|
||||
|
||||
if (entry.app_desc_short)
|
||||
app_desc_short = entry.app_desc_short
|
||||
}
|
||||
|
||||
return { pwa, app_name, app_desc_short }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue