feat(app): support locale plural rules (#40370)
Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
26aefa68f5
commit
0c380f31ac
@@ -123,6 +123,8 @@ const dict: Record<string, string> = {
|
||||
"prompt.example.25": "What should we test next?",
|
||||
}
|
||||
|
||||
const plurals = new Intl.PluralRules("en-US")
|
||||
|
||||
function render(template: string, params?: Record<string, unknown>) {
|
||||
if (!params) return template
|
||||
return template.replace(/\{\{([^}]+)\}\}/g, (_, key: string) => {
|
||||
@@ -140,5 +142,9 @@ export function useLanguage() {
|
||||
t(key: string, params?: Record<string, unknown>) {
|
||||
return render(dict[key] ?? key, params)
|
||||
},
|
||||
plural(key: string, count: number, params?: Record<string, unknown>) {
|
||||
const value = dict[`${key}.${plurals.select(count)}`] ?? dict[`${key}.other`] ?? key
|
||||
return render(value, { ...params, count })
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user