refactor: replace import glob with static imports to prevent playwright from dying

This commit is contained in:
Bryan Galdámez 2025-06-06 18:17:55 -06:00
parent bfb609fe97
commit b0c122f984
No known key found for this signature in database
4 changed files with 45 additions and 24 deletions

View file

@ -3,7 +3,8 @@ import { icon, library } from '@fortawesome/fontawesome-svg-core'
import { faSort, faSortDown, faSortUp } from '@fortawesome/free-solid-svg-icons' import { faSort, faSortDown, faSortUp } from '@fortawesome/free-solid-svg-icons'
import Xmark from '~/icons/XmarkIcon.astro' import Xmark from '~/icons/XmarkIcon.astro'
import { type ZenTheme } from '~/mods' 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 // Add icons to the library
library.add(faSort, faSortUp, faSortDown) library.add(faSort, faSortUp, faSortDown)

View file

@ -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. * Returns the releases object, injecting checksums dynamically.

View file

@ -442,29 +442,53 @@ export const i18nSchema = type
export type I18nType = typeof i18nSchema.I18n.infer export type I18nType = typeof i18nSchema.I18n.infer
const languages: Record<string, I18nType> = Object.fromEntries( export type Locale = (typeof locales)[number]['value']
Object.entries(import.meta.glob('~/i18n/**/translation.json', { eager: true })).map(
([key, value]) => {
const result = i18nSchema.I18n(value)
if (result instanceof type.errors) { async function importI18n(
throw new Error(`Invalid translation file (${key}):\n${' '.repeat(2)}${result.summary}`) locale: Locale,
promise: Promise<{ default: unknown }>
): Promise<[Locale, unknown]> {
const result = await promise
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 [/i18n\/([A-z]{2})\/translation.json/.exec(key)?.[1], result] return [locale, parsed]
} })
) ) as Record<Locale, I18nType>
)
export const i18n = { export const i18n = {
DEFAULT_LOCALE: 'en', DEFAULT_LOCALE: 'en',
LOCALES: [ LOCALES: locales.map(locale => {
{ label: 'English', value: 'en', ui: languages['en'], intl: 'en-US' }, return {
{ label: '日本語', value: 'ja', ui: languages['ja'], intl: 'ja-JP' }, ...locale,
{ label: 'Español', value: 'es', ui: languages['es'], intl: 'es-ES' }, ui: languages[locale.value],
], }
}),
} as const } as const
export const getIntlLocale = (locale: string) => { export const getIntlLocale = (locale: string) => {
return i18n.LOCALES.find(l => l.value === locale)?.intl return locales.find(({ value }) => {
return value === locale
})?.intl
} }

View file

@ -1,12 +1,7 @@
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 } from '~/constants/i18n' import { type I18nType, type Locale } from '~/constants/i18n'
/**
* Represents the available locales in the application
*/
export type Locale = (typeof locales)[number]
/** /**
* Generates a localized path by prefixing the locale if necessary * Generates a localized path by prefixing the locale if necessary