chore: remove unnecessary mapping

This commit is contained in:
Bryan Galdámez 2025-06-06 18:28:02 -06:00
parent b0c122f984
commit 35a7cc3fcc
No known key found for this signature in database
2 changed files with 13 additions and 27 deletions

View file

@ -453,18 +453,18 @@ async function importI18n(
return [locale, result.default] return [locale, result.default]
} }
export const locales = [ const locales = [
{ label: 'English', value: 'en', intl: 'en-US' }, { label: 'English', value: 'en', intl: 'en-US' },
{ label: '日本語', value: 'ja', intl: 'ja-JP' }, { label: '日本語', value: 'ja', intl: 'ja-JP' },
{ label: 'Español', value: 'es', intl: 'es-ES' }, { label: 'Español', value: 'es', intl: 'es-ES' },
] as const ] as const
const languages = Object.fromEntries( export const languages = Object.fromEntries(
( (
await Promise.all([ await Promise.all([
importI18n('en', import('~/i18n/en/translation.json')), importI18n('en', import('~/i18n/en/translation.json', { with: { type: 'json' } })),
importI18n('ja', import('~/i18n/ja/translation.json')), importI18n('ja', import('~/i18n/ja/translation.json', { with: { type: 'json' } })),
importI18n('es', import('~/i18n/es/translation.json')), importI18n('es', import('~/i18n/es/translation.json', { with: { type: 'json' } })),
]) ])
).map(([locale, result]) => { ).map(([locale, result]) => {
const parsed = i18nSchema.I18n(result) const parsed = i18nSchema.I18n(result)

View file

@ -1,7 +1,12 @@
import { type AstroGlobal, type GetStaticPaths } from 'astro' import { type AstroGlobal, type GetStaticPaths } from 'astro'
import { CONSTANT } from '~/constants' import { CONSTANT } from '~/constants'
import { type I18nType, type Locale } from '~/constants/i18n' import { languages, type I18nType, type Locale } from '~/constants/i18n'
/**
* List of all supported locales
*/
export const locales = CONSTANT.I18N.LOCALES.map(({ value }) => value)
/** /**
* Generates a localized path by prefixing the locale if necessary * Generates a localized path by prefixing the locale if necessary
@ -45,11 +50,6 @@ export const getLocale = (Astro: AstroGlobal): Locale => {
return Astro.currentLocale as Locale return Astro.currentLocale as Locale
} }
/**
* List of all supported locales
*/
export const locales = CONSTANT.I18N.LOCALES.map(({ value }) => value)
/** /**
* List of locales excluding the default locale * List of locales excluding the default locale
*/ */
@ -121,26 +121,12 @@ export function deepMerge(defaultObj: I18nType, localeObj: Partial<I18nType> = {
return result return result
} }
const defaultUI = CONSTANT.I18N.LOCALES.find(({ value }) => { const defaultUI = languages[CONSTANT.I18N.DEFAULT_LOCALE]
return value === CONSTANT.I18N.DEFAULT_LOCALE
})?.ui
if (!defaultUI) { if (!defaultUI) {
throw new Error('Default UI translation is missing!') throw new Error('Default UI translation is missing!')
} }
const localesMap: Record<Locale, I18nType> = {} as Record<Locale, I18nType>
for (const locale of locales) {
const maybeLocale = CONSTANT.I18N.LOCALES.find(({ value }) => {
return value === locale
})
if (maybeLocale) {
localesMap[locale] = maybeLocale.ui
}
}
/** /**
* Retrieves UI translations for a given locale * Retrieves UI translations for a given locale
* @param {Locale} [locale] - The target locale for translations * @param {Locale} [locale] - The target locale for translations
@ -149,7 +135,7 @@ for (const locale of locales) {
export const getUI = (locale?: Locale): I18nType => { export const getUI = (locale?: Locale): I18nType => {
const validLocale = locale && locales.includes(locale) ? locale : CONSTANT.I18N.DEFAULT_LOCALE const validLocale = locale && locales.includes(locale) ? locale : CONSTANT.I18N.DEFAULT_LOCALE
return localesMap[validLocale] ?? defaultUI return languages[validLocale] ?? defaultUI
} }
/** /**