From b0c122f98433f47fec7cbc7747eb970e40826599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bryan=20Gald=C3=A1mez?= Date: Fri, 6 Jun 2025 18:17:55 -0600 Subject: [PATCH] refactor: replace import glob with static imports to prevent playwright from dying --- src/components/ModsList.astro | 3 +- src/components/download/release-data.ts | 3 +- src/constants/i18n.ts | 56 ++++++++++++++++++------- src/utils/i18n.ts | 7 +--- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/components/ModsList.astro b/src/components/ModsList.astro index 0296a18..aa50114 100644 --- a/src/components/ModsList.astro +++ b/src/components/ModsList.astro @@ -3,7 +3,8 @@ import { icon, library } from '@fortawesome/fontawesome-svg-core' import { faSort, faSortDown, faSortUp } from '@fortawesome/free-solid-svg-icons' import Xmark from '~/icons/XmarkIcon.astro' import { type ZenTheme } from '~/mods' -import { getPath, type Locale } from '~/utils/i18n' +import { getPath } from '~/utils/i18n' +import { type Locale } from '~/constants/i18n' // Add icons to the library library.add(faSort, faSortUp, faSortDown) diff --git a/src/components/download/release-data.ts b/src/components/download/release-data.ts index 93274b0..b2eceda 100644 --- a/src/components/download/release-data.ts +++ b/src/components/download/release-data.ts @@ -1,4 +1,5 @@ -import { getUI, type Locale } from '~/utils/i18n' +import { type Locale } from '~/constants/i18n' +import { getUI } from '~/utils/i18n' /** * Returns the releases object, injecting checksums dynamically. diff --git a/src/constants/i18n.ts b/src/constants/i18n.ts index 2248173..c43f2f0 100644 --- a/src/constants/i18n.ts +++ b/src/constants/i18n.ts @@ -442,29 +442,53 @@ export const i18nSchema = type export type I18nType = typeof i18nSchema.I18n.infer -const languages: Record = Object.fromEntries( - Object.entries(import.meta.glob('~/i18n/**/translation.json', { eager: true })).map( - ([key, value]) => { - const result = i18nSchema.I18n(value) +export type Locale = (typeof locales)[number]['value'] - if (result instanceof type.errors) { - throw new Error(`Invalid translation file (${key}):\n${' '.repeat(2)}${result.summary}`) - } +async function importI18n( + locale: Locale, + promise: Promise<{ default: unknown }> +): Promise<[Locale, unknown]> { + const result = await promise - return [/i18n\/([A-z]{2})\/translation.json/.exec(key)?.[1], result] + return [locale, result.default] +} + +export 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( + ( + await Promise.all([ + importI18n('en', import('~/i18n/en/translation.json')), + importI18n('ja', import('~/i18n/ja/translation.json')), + importI18n('es', import('~/i18n/es/translation.json')), + ]) + ).map(([locale, result]) => { + const parsed = i18nSchema.I18n(result) + + if (parsed instanceof type.errors) { + throw new Error(`Invalid translation file (${locale}):\n${' '.repeat(2)}${parsed.summary}`) } - ) -) + + return [locale, parsed] + }) +) as Record export const i18n = { DEFAULT_LOCALE: 'en', - LOCALES: [ - { label: 'English', value: 'en', ui: languages['en'], intl: 'en-US' }, - { label: '日本語', value: 'ja', ui: languages['ja'], intl: 'ja-JP' }, - { label: 'Español', value: 'es', ui: languages['es'], intl: 'es-ES' }, - ], + LOCALES: locales.map(locale => { + return { + ...locale, + ui: languages[locale.value], + } + }), } as const export const getIntlLocale = (locale: string) => { - return i18n.LOCALES.find(l => l.value === locale)?.intl + return locales.find(({ value }) => { + return value === locale + })?.intl } diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index 9a6b800..da332a5 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -1,12 +1,7 @@ import { type AstroGlobal, type GetStaticPaths } from 'astro' import { CONSTANT } from '~/constants' -import { type I18nType } from '~/constants/i18n' - -/** - * Represents the available locales in the application - */ -export type Locale = (typeof locales)[number] +import { type I18nType, type Locale } from '~/constants/i18n' /** * Generates a localized path by prefixing the locale if necessary