Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 26f18f8

Browse files
committed
fix(packages/core): i18n module should support numeric parmaeters
Fixes #3384
1 parent dffb4aa commit 26f18f8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/core/src/util/i18n.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export function fromMap(map: Record<string, string>) {
3131
return map[getLocale()] || map[getLocale2()] || map[defaultLocale]
3232
}
3333

34-
export default (plugin: string, namespace = 'resources'): ((key: string, ...parameters: string[]) => string) => {
34+
export default (
35+
plugin: string,
36+
namespace = 'resources'
37+
): ((key: string, ...parameters: (string | number)[]) => string) => {
3538
// eslint-disable-next-line @typescript-eslint/no-var-requires
3639
const defaultStrings: Record<string, string> = require(`@kui-shell/${plugin}/i18n/${namespace}_en_US.json`)
3740

@@ -55,10 +58,16 @@ export default (plugin: string, namespace = 'resources'): ((key: string, ...para
5558

5659
const _strings = i18n(locale)
5760

58-
return function(key: string, ...parameters: string[]): string {
59-
const str = _strings[key] || defaultStrings[key] || key
61+
return function(key: string, ...parameters: (string | number)[]): string {
62+
const str: string = _strings[key] || defaultStrings[key] || key
6063

6164
if (!parameters) return str
62-
else return parameters.reduce((str, param, idx) => str.replace(new RegExp(`\\{${idx}\\}`, 'g'), param), str) // e.g. replace all occurrences of {0} in the str
65+
else
66+
return parameters
67+
.map(_ => _.toString()) // only needed due to https://github.com/microsoft/TypeScript/issues/7014
68+
.reduce((str: string, param, idx) => {
69+
// e.g. replace all occurrences of {0} in the str
70+
return str.replace(new RegExp(`\\{${idx}\\}`, 'g'), param.toString())
71+
}, str)
6372
}
6473
}

0 commit comments

Comments
 (0)