mirror of
https://github.com/zen-browser/www.git
synced 2025-07-08 01:10:02 +02:00
refactor: replace import glob with static imports to prevent playwright from dying
This commit is contained in:
parent
bfb609fe97
commit
b0c122f984
4 changed files with 45 additions and 24 deletions
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -442,29 +442,53 @@ export const i18nSchema = type
|
|||
|
||||
export type I18nType = typeof i18nSchema.I18n.infer
|
||||
|
||||
const languages: Record<string, I18nType> = 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 [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 = {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue