Skip to content

Commit d171b38

Browse files
userquindanielroe
andauthored
fix(i18n): update i18n logic (#434)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent b26b3c2 commit d171b38

5 files changed

Lines changed: 18 additions & 14 deletions

File tree

app/pages/settings.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ const { locale, locales, setLocale } = useI18n()
55
const colorMode = useColorMode()
66
const { currentLocaleStatus, isSourceLocale } = useI18nStatus()
77
8-
const availableLocales = computed(() =>
9-
locales.value.map(l => (typeof l === 'string' ? { code: l, name: l } : l)),
10-
)
11-
128
// Escape to go back (but not when focused on form elements)
139
onKeyStroke('Escape', e => {
1410
const target = e.target as HTMLElement
@@ -211,7 +207,7 @@ defineOgImageComponent('Default', {
211207
class="w-full sm:w-auto min-w-48 bg-bg border border-border rounded-md px-3 py-2 text-sm text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 cursor-pointer"
212208
@change="setLocale(($event.target as HTMLSelectElement).value as typeof locale)"
213209
>
214-
<option v-for="loc in availableLocales" :key="loc.code" :value="loc.code">
210+
<option v-for="loc in locales" :key="loc.code" :value="loc.code" :lang="loc.code">
215211
{{ loc.name }}
216212
</option>
217213
</select>

config/i18n.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ export const countryLocaleVariants: Record<string, (LocaleObjectData & { country
7878
],*/
7979
}
8080

81-
const locales: (Omit<LocaleObjectData, 'code'> & { code: string })[] = [
81+
const locales: (LocaleObjectData | (Omit<LocaleObjectData, 'code'> & { code: string }))[] = [
8282
{
8383
code: 'en',
8484
file: 'en.json',
8585
name: 'English',
8686
},
8787
{
88-
code: 'ar-EG',
88+
code: 'ar',
8989
file: 'ar.json',
9090
name: 'العربية',
9191
dir: 'rtl',
9292
pluralRule: (choice: number) => {
9393
const name = new Intl.PluralRules('ar-EG').select(choice)
9494
return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
9595
},
96-
} satisfies LocaleObjectData,
96+
},
9797
/*{
9898
code: 'ckb',
9999
file: 'ckb.json',
@@ -103,7 +103,7 @@ const locales: (Omit<LocaleObjectData, 'code'> & { code: string })[] = [
103103
const name = new Intl.PluralRules('ckb').select(choice)
104104
return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
105105
},
106-
} satisfies LocaleObjectData,
106+
},
107107
{
108108
code: 'fa-IR',
109109
file: 'fa-IR.json',
@@ -113,7 +113,7 @@ const locales: (Omit<LocaleObjectData, 'code'> & { code: string })[] = [
113113
const name = new Intl.PluralRules('fa-IR').select(choice)
114114
return { zero: 0, one: 1, two: 2, few: 3, many: 4, other: 5 }[name]
115115
},
116-
} satisfies LocaleObjectData,
116+
},
117117
{
118118
code: 'ca',
119119
file: 'ca.json',
@@ -177,7 +177,7 @@ const locales: (Omit<LocaleObjectData, 'code'> & { code: string })[] = [
177177
const name = new Intl.PluralRules('ru-RU').select(choice)
178178
return { zero: 2, one: 0, two: 1, few: 1, many: 2, other: 3 }[name]
179179
},
180-
} satisfies LocaleObjectData,
180+
},
181181
/*{
182182
code: 'ru-RU',
183183
file: 'ru-RU.json',
@@ -278,6 +278,8 @@ const locales: (Omit<LocaleObjectData, 'code'> & { code: string })[] = [
278278
},*/
279279
]
280280

281+
const lunariaJSONFiles: Record<string, string> = {}
282+
281283
function buildLocales() {
282284
const useLocales = Object.values(locales).reduce((acc, data) => {
283285
const locales = countryLocaleVariants[data.code]
@@ -289,10 +291,12 @@ function buildLocales() {
289291
name: l.name,
290292
files: [data.file as string, `${l.code}.json`],
291293
}
294+
lunariaJSONFiles[l.code] = l.country ? (data.file as string) : `${l.code}.json`
292295
delete entry.file
293296
acc.push(entry)
294297
})
295298
} else {
299+
lunariaJSONFiles[data.code] = data.file as string
296300
acc.push(data as LocaleObjectData)
297301
}
298302
return acc
@@ -303,6 +307,8 @@ function buildLocales() {
303307

304308
export const currentLocales = buildLocales()
305309

310+
export { lunariaJSONFiles }
311+
306312
export const datetimeFormats = Object.values(currentLocales).reduce((acc, data) => {
307313
const dateTimeFormats = data.dateTimeFormats
308314
if (dateTimeFormats) {

lunaria/lunaria.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLunaria } from '@lunariajs/core'
22
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
33
import { Page } from './components.ts'
4-
import { prepareJsonFiles } from './prepare-json-files.ts'
4+
import { lunariaJSONFiles, prepareJsonFiles } from './prepare-json-files.ts'
55
import type { I18nStatus } from '../shared/types/i18n-status.ts'
66

77
await prepareJsonFiles()
@@ -61,7 +61,7 @@ const jsonStatus: I18nStatus = {
6161
}
6262

6363
const completedKeys = totalKeys - missingKeys.length
64-
const localeFilePath = `i18n/locales/${locale.lang}.json`
64+
const localeFilePath = `i18n/locales/${lunariaJSONFiles[locale.lang]!}.json`
6565

6666
return {
6767
lang: locale.lang,

lunaria/prepare-json-files.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import type { Locale } from '@lunariajs/core'
22
import type { LocaleObject } from '@nuxtjs/i18n'
33
import * as path from 'node:path'
44
import * as fs from 'node:fs/promises'
5-
import { currentLocales } from '../config/i18n.ts'
5+
import { currentLocales, lunariaJSONFiles } from '../config/i18n.ts'
66
import { deepCopy } from '@intlify/shared'
77

88
const destFolder = path.resolve('lunaria/files')
99
const localesFolder = path.resolve('i18n/locales')
1010

1111
const defaultLocale = currentLocales.find(l => l.code === 'en-US')!
1212
/** @public */
13+
export { lunariaJSONFiles }
14+
/** @public */
1315
export const sourceLocale = {
1416
label: defaultLocale.name,
1517
lang: defaultLocale.code,

0 commit comments

Comments
 (0)