dify/web/app/components/plugins/marketplace/templates/template-language.ts
Coding On Star cb6c04637b
feat(web): add marketplace creator profiles and homepage redesign (#41538)
Co-authored-by: zxhlyh <jasonapring2015@outlook.com>
Co-authored-by: fatelei <fatelei@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: 姜涵煦 <hanxujiang@jianghanxudeMacBook-Pro-2.local>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
2026-09-04 08:09:45 +00:00

68 lines
2.6 KiB
TypeScript

import type { MarketplaceTemplate } from '@dify/contracts/marketplace'
export const LANGUAGE_OPTIONS = [
{ value: 'en', label: 'English', nativeLabel: 'English' },
{ value: 'zh-Hans', label: 'Simplified Chinese', nativeLabel: '中文' },
{ value: 'ja', label: 'Japanese', nativeLabel: '日本語' },
{ value: 'other', label: 'Other', nativeLabel: 'Other' },
] as const
export function parseListParam(value?: string | string[]) {
if (!value) return []
const parts = Array.isArray(value) ? value : value.split(',')
return parts.map((part) => part.trim()).filter(Boolean)
}
const getLanguagePrefix = (locale: string) => locale.toLowerCase().split(/[-_]/)[0] ?? ''
function getSearchLanguagesForLocale(locale: string) {
const requestedLanguage = getLanguagePrefix(locale)
if (!requestedLanguage) return ['en']
if (requestedLanguage === 'zh') return ['zh-Hans']
const knownOption = LANGUAGE_OPTIONS.find(
(option) => option.value !== 'other' && getLanguagePrefix(option.value) === requestedLanguage,
)
if (knownOption) return [knownOption.value]
return [requestedLanguage]
}
export function resolveTemplateSearchLanguages(selectedLanguages: string[], locale: string) {
return selectedLanguages.length > 0 ? selectedLanguages : getSearchLanguagesForLocale(locale)
}
/**
* Keeps the templates matching the requested locale's language. Templates
* without language metadata are treated as language-agnostic and always kept.
* When no template matches the requested language, the list explicitly falls
* back to English templates (and finally to the unfiltered list) so locales
* such as German render real content instead of an empty state.
*/
export function filterTemplatesForLocale<
T extends Pick<MarketplaceTemplate, 'preferred_languages'>,
>(templates: T[], locale: string) {
const requestedLanguage = getLanguagePrefix(locale)
const filterByLanguage = (languagePrefix: string) =>
templates.filter((template) => {
const preferredLanguages = template.preferred_languages ?? []
if (preferredLanguages.length === 0) return true
return preferredLanguages.some((language) => getLanguagePrefix(language) === languagePrefix)
})
const requestedMatches = filterByLanguage(requestedLanguage)
if (requestedMatches.length > 0) return requestedMatches
const englishMatches = requestedLanguage === 'en' ? [] : filterByLanguage('en')
if (englishMatches.length > 0) return englishMatches
return templates
}
export function getTemplateCollectionText(value: Record<string, string>, locale: string) {
const localeKey = locale.replace('-', '_')
return value[localeKey] || value.en_US || Object.values(value)[0] || ''
}