feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
138
packages/i18n/src/shared.ts
Normal file
138
packages/i18n/src/shared.ts
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
// packages/i18n/src/shared.ts
|
||||
// SSOT i18n 공유 타입, 메타데이터, 포맷팅 유틸 (React 및 번들 JSON 비의존)
|
||||
|
||||
import type ko from './locales/ko.json'
|
||||
|
||||
/** 마스터 키에서 자동 추출된 번역 키 유니온 */
|
||||
export type TranslationKey = keyof typeof ko
|
||||
|
||||
/** 지원 로케일 */
|
||||
export type Locale =
|
||||
| 'ko' | 'en' | 'ja' | 'zh' | 'zh-TW'
|
||||
| 'es' | 'fr' | 'de' | 'pt' | 'ru' | 'vi' | 'th'
|
||||
|
||||
export type Translations = Record<string, string>
|
||||
|
||||
export type TFunction = (key: TranslationKey, params?: Record<string, string | number>) => string
|
||||
|
||||
export const LOCALE_CODES: readonly Locale[] = [
|
||||
'ko', 'en', 'ja', 'zh', 'zh-TW', 'es', 'fr', 'de', 'pt', 'ru', 'vi', 'th',
|
||||
]
|
||||
|
||||
const LOCALE_CODE_SET: ReadonlySet<string> = new Set<string>(LOCALE_CODES)
|
||||
|
||||
/** 언어 선택 UI에 표시할 메타데이터 (각 언어로 된 자국어명) */
|
||||
export const LOCALE_META: ReadonlyArray<{ code: Locale; nativeName: string; englishName: string }> = [
|
||||
{ code: 'ko', nativeName: '한국어', englishName: 'Korean' },
|
||||
{ code: 'en', nativeName: 'English', englishName: 'English' },
|
||||
{ code: 'ja', nativeName: '日本語', englishName: 'Japanese' },
|
||||
{ code: 'zh', nativeName: '简体中文', englishName: 'Chinese (Simplified)' },
|
||||
{ code: 'zh-TW', nativeName: '繁體中文', englishName: 'Chinese (Traditional)' },
|
||||
{ code: 'es', nativeName: 'Español', englishName: 'Spanish' },
|
||||
{ code: 'fr', nativeName: 'Français', englishName: 'French' },
|
||||
{ code: 'de', nativeName: 'Deutsch', englishName: 'German' },
|
||||
{ code: 'pt', nativeName: 'Português', englishName: 'Portuguese' },
|
||||
{ code: 'ru', nativeName: 'Русский', englishName: 'Russian' },
|
||||
{ code: 'vi', nativeName: 'Tiếng Việt', englishName: 'Vietnamese' },
|
||||
{ code: 'th', nativeName: 'ไทย', englishName: 'Thai' },
|
||||
]
|
||||
|
||||
export function isValidLocale(value: string): value is Locale {
|
||||
return LOCALE_CODE_SET.has(value)
|
||||
}
|
||||
|
||||
export function resolveTranslationWithFallback(
|
||||
key: string,
|
||||
primary: Translations | undefined,
|
||||
enFallback: Translations | undefined,
|
||||
koFallback: Translations | undefined,
|
||||
params?: Record<string, string | number>,
|
||||
): string {
|
||||
// fallback 체인: 현재 로케일 → en → ko → key 자체
|
||||
let text = primary?.[key] ?? enFallback?.[key] ?? koFallback?.[key] ?? key
|
||||
|
||||
if (params) {
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
text = text.replaceAll(`{{${k}}}`, String(v))
|
||||
}
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
export function toBcp47(locale: Locale): string {
|
||||
const map: Partial<Record<Locale, string>> = {
|
||||
zh: 'zh-CN',
|
||||
'zh-TW': 'zh-TW',
|
||||
}
|
||||
return map[locale] ?? locale
|
||||
}
|
||||
|
||||
export interface I18nStorage {
|
||||
load: () => Promise<string | null> | string | null
|
||||
save: (locale: Locale) => void | Promise<void>
|
||||
}
|
||||
|
||||
export const noopI18nStorage: I18nStorage = {
|
||||
load: () => null,
|
||||
save: () => {},
|
||||
}
|
||||
|
||||
export function safeLoadStoredLocale(storage: I18nStorage): Promise<string | null> {
|
||||
try {
|
||||
return Promise.resolve(storage.load()).catch(() => null)
|
||||
} catch {
|
||||
return Promise.resolve(null)
|
||||
}
|
||||
}
|
||||
|
||||
export function safeSaveStoredLocale(storage: I18nStorage, locale: Locale): void {
|
||||
try {
|
||||
void Promise.resolve(storage.save(locale)).catch(() => {})
|
||||
} catch {
|
||||
// 어댑터 동기 저장 실패 시에도 UI 상태 변경이 중단되거나 미처리 거부로 이어지지 않게 한다.
|
||||
}
|
||||
}
|
||||
|
||||
export function createFormatDate(locale: Locale) {
|
||||
const bcp = toBcp47(locale)
|
||||
return (date: Date | number, options?: Intl.DateTimeFormatOptions): string => {
|
||||
const d = typeof date === 'number' ? new Date(date) : date
|
||||
if (!Number.isFinite(d.getTime())) return ''
|
||||
return new Intl.DateTimeFormat(bcp, options).format(d)
|
||||
}
|
||||
}
|
||||
|
||||
export function createFormatNumber(locale: Locale) {
|
||||
const bcp = toBcp47(locale)
|
||||
return (num: number, options?: Intl.NumberFormatOptions): string => {
|
||||
return new Intl.NumberFormat(bcp, options).format(num)
|
||||
}
|
||||
}
|
||||
|
||||
export function createFormatRelativeDate(locale: Locale, t: TFunction) {
|
||||
return (ts: number): string => {
|
||||
const d = new Date(ts)
|
||||
if (!Number.isFinite(d.getTime())) return ''
|
||||
d.setHours(0, 0, 0, 0)
|
||||
const today = new Date()
|
||||
today.setHours(0, 0, 0, 0)
|
||||
const yesterday = new Date(today)
|
||||
yesterday.setDate(yesterday.getDate() - 1)
|
||||
|
||||
if (d.getTime() === today.getTime()) return t('date.today')
|
||||
if (d.getTime() === yesterday.getTime()) return t('date.yesterday')
|
||||
|
||||
const bcp = toBcp47(locale)
|
||||
return new Intl.DateTimeFormat(bcp, { month: 'short', day: 'numeric' }).format(d).toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
export function createFormatTime(locale: Locale) {
|
||||
const bcp = toBcp47(locale)
|
||||
return (ts: number): string => {
|
||||
const d = new Date(ts)
|
||||
if (!Number.isFinite(d.getTime())) return ''
|
||||
return new Intl.DateTimeFormat(bcp, { hour: '2-digit', minute: '2-digit' }).format(d)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue