1
0
elk/config/i18n.ts

159 lines
3.3 KiB
TypeScript
Raw Normal View History

import type { NuxtI18nOptions } from '@nuxtjs/i18n'
import type { DateTimeFormats, NumberFormats, PluralizationRule, PluralizationRules } from '@intlify/core-base'
import type { LocaleObject } from '#i18n'
interface LocaleObjectData extends LocaleObject {
numberFormats?: NumberFormats
dateTimeFormats?: DateTimeFormats
pluralRule?: PluralizationRule
}
// @ts-expect-error dir is there, ts complaining
const locales: LocaleObjectData[] = [
{
code: 'en-US',
file: 'en-US.json',
name: 'English (US)',
},
{
code: 'en-GB',
file: 'en-GB.json',
name: 'English (UK)',
},
{
code: 'de-DE',
file: 'de-DE.json',
name: 'Deutsch',
},
{
code: 'zh-CN',
file: 'zh-CN.json',
name: '简体中文',
},
2022-12-31 13:07:11 +09:00
{
code: 'zh-TW',
file: 'zh-TW.json',
name: '繁体中文',
},
{
code: 'ja-JP',
file: 'ja-JP.json',
name: '日本語',
},
2023-01-05 05:10:09 +09:00
{
code: 'nl-NL',
file: 'nl-NL.json',
name: 'Nederlands',
},
{
code: 'es-ES',
file: 'es-ES.json',
name: 'Español',
},
{
code: 'fr-FR',
file: 'fr-FR.json',
name: 'Français',
},
2022-12-15 06:19:38 +09:00
{
code: 'cs-CZ',
file: 'cs-CZ.json',
name: 'Česky',
},
{
code: 'ar-EG',
file: 'ar-EG.json',
name: 'العربية',
dir: 'rtl',
pluralRule: (choice: number) => {
const name = new Intl.PluralRules('ar-EG').select(choice)
return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
},
2022-12-15 06:19:38 +09:00
},
].sort((a, b) => a.code.localeCompare(b.code))
const datetimeFormats = Object.values(locales).reduce((acc, data) => {
const dateTimeFormats = data.dateTimeFormats
if (dateTimeFormats) {
acc[data.code] = { ...dateTimeFormats }
delete data.dateTimeFormats
}
else {
acc[data.code] = {
2023-01-04 02:24:39 +09:00
shortDate: {
dateStyle: 'short',
},
short: {
dateStyle: 'short',
timeStyle: 'short',
},
long: {
dateStyle: 'long',
timeStyle: 'medium',
},
}
}
return acc
}, <DateTimeFormats>{})
const numberFormats = Object.values(locales).reduce((acc, data) => {
const numberFormats = data.numberFormats
if (numberFormats) {
acc[data.code] = { ...numberFormats }
delete data.numberFormats
}
else {
acc[data.code] = {
percentage: {
style: 'percent',
maximumFractionDigits: 1,
},
smallCounting: {
style: 'decimal',
maximumFractionDigits: 0,
},
kiloCounting: {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 1,
},
millionCounting: {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 2,
},
}
}
return acc
}, <NumberFormats>{})
const pluralRules = Object.values(locales).reduce((acc, data) => {
const pluralRule = data.pluralRule
if (pluralRule) {
acc[data.code] = pluralRule
delete data.pluralRule
}
return acc
}, <PluralizationRules>{})
export const i18n: NuxtI18nOptions = {
locales,
strategy: 'no_prefix',
detectBrowserLanguage: false,
langDir: 'locales',
defaultLocale: 'en-US',
vueI18n: {
fallbackLocale: 'en-US',
2022-12-03 02:23:47 +09:00
fallbackWarn: false,
missingWarn: false,
datetimeFormats,
numberFormats,
pluralRules,
},
lazy: true,
}