x
This commit is contained in:
1 parent
62db477dbd
commit
feeda6dc60
7 files changed
+263
-83
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { TranslatableString } from '@homaroma/types'
|
||||
import type { Category, TranslatableString } from '@homaroma/types'
|
||||
|
||||
export function resolveLocalized(value: TranslatableString | null | undefined, lang = 'th'): string {
|
||||
if (!value) return ''
|
||||
@@ -14,3 +14,16 @@ export function toRecord(value: unknown): Record<string, unknown> {
|
||||
export function localizedFromRecord(payload: Record<string, unknown>, key: string, lang = 'th'): string {
|
||||
return resolveLocalized(payload[key] as TranslatableString | null | undefined, lang)
|
||||
}
|
||||
|
||||
export function resolveCategoryName(category: Pick<Category, 'name' | 'translations'>, lang = 'th'): string {
|
||||
if (category.translations?.length) {
|
||||
const translated = category.translations.find((tr) => tr.langCode === lang)
|
||||
?? category.translations.find((tr) => tr.langCode === lang.slice(0, 2))
|
||||
?? category.translations.find((tr) => tr.langCode === 'th')
|
||||
?? category.translations[0]
|
||||
|
||||
if (translated?.name) return translated.name
|
||||
}
|
||||
|
||||
return resolveLocalized(category.name, lang)
|
||||
}
|
||||
Reference in new issue
Block a user