From 35a7cc3fcc232016d0f10c912ff5506160a50dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bryan=20Gald=C3=A1mez?= Date: Fri, 6 Jun 2025 18:28:02 -0600 Subject: [PATCH] chore: remove unnecessary mapping --- src/constants/i18n.ts | 10 +++++----- src/utils/i18n.ts | 30 ++++++++---------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/constants/i18n.ts b/src/constants/i18n.ts index c43f2f0..56d35c7 100644 --- a/src/constants/i18n.ts +++ b/src/constants/i18n.ts @@ -453,18 +453,18 @@ async function importI18n( return [locale, result.default] } -export const locales = [ +const locales = [ { label: 'English', value: 'en', intl: 'en-US' }, { label: '日本語', value: 'ja', intl: 'ja-JP' }, { label: 'Español', value: 'es', intl: 'es-ES' }, ] as const -const languages = Object.fromEntries( +export const languages = Object.fromEntries( ( await Promise.all([ - importI18n('en', import('~/i18n/en/translation.json')), - importI18n('ja', import('~/i18n/ja/translation.json')), - importI18n('es', import('~/i18n/es/translation.json')), + importI18n('en', import('~/i18n/en/translation.json', { with: { type: 'json' } })), + importI18n('ja', import('~/i18n/ja/translation.json', { with: { type: 'json' } })), + importI18n('es', import('~/i18n/es/translation.json', { with: { type: 'json' } })), ]) ).map(([locale, result]) => { const parsed = i18nSchema.I18n(result) diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index da332a5..13d9a6c 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -1,7 +1,12 @@ import { type AstroGlobal, type GetStaticPaths } from 'astro' 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 @@ -45,11 +50,6 @@ export const getLocale = (Astro: AstroGlobal): 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 */ @@ -121,26 +121,12 @@ export function deepMerge(defaultObj: I18nType, localeObj: Partial = { return result } -const defaultUI = CONSTANT.I18N.LOCALES.find(({ value }) => { - return value === CONSTANT.I18N.DEFAULT_LOCALE -})?.ui +const defaultUI = languages[CONSTANT.I18N.DEFAULT_LOCALE] if (!defaultUI) { throw new Error('Default UI translation is missing!') } -const localesMap: Record = {} as Record - -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 * @param {Locale} [locale] - The target locale for translations @@ -149,7 +135,7 @@ for (const locale of locales) { export const getUI = (locale?: Locale): I18nType => { const validLocale = locale && locales.includes(locale) ? locale : CONSTANT.I18N.DEFAULT_LOCALE - return localesMap[validLocale] ?? defaultUI + return languages[validLocale] ?? defaultUI } /**