Skip to content

Commit ad1bbc1

Browse files
authored
feat(i18n): localize support to provide dynamic replace text (#186)
* feat(i18n): localize support to provide dynamic replace text * fix(scrollable): remove warning about scrollable div key
1 parent c2fb3d5 commit ad1bbc1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/components/scrollable/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ export function Scrollable(props: IScrollbarProps) {
6363
ref={scroller}
6464
{...(custom as any)}
6565
wrapperProps={{
66-
renderer: ({ elementRef, style, ...restProps }) => {
66+
renderer: ({ elementRef, style, key, ...restProps }) => {
6767
const currentTop = scroller.current?.scrollTop || 0;
6868
return (
69-
<>
69+
<React.Fragment key={key}>
7070
<div
7171
{...restProps}
7272
ref={elementRef}
@@ -79,7 +79,7 @@ export function Scrollable(props: IScrollbarProps) {
7979
isShowShadow && currentTop > 0 && 'active'
8080
)}
8181
/>
82-
</>
82+
</React.Fragment>
8383
);
8484
},
8585
}}

src/i18n/localize.tsx

+29-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,37 @@ export interface ILocalizeProps {
99
defaultValue?: string;
1010
}
1111

12-
export function localize(sourceKey: string, defaultValue: string): any {
13-
const localizedValue = container
12+
const LOCALIZE_REPLACED_WORD = '${i}';
13+
14+
/**
15+
* Returns the international text located by source key,or the default value if it is not find
16+
* For examples:
17+
* ```ts
18+
* localize('id','default value'); // hello ${i}, ${i}
19+
* localize('id','default value', 'world'); // hello world, ${i}
20+
* localize('id','default value', 'world', 'molecule'); // hello world, molecule
21+
* ```
22+
* @param sourceKey The key value located in the source internaional text
23+
* @param defaultValue The default value to be used when not find the international text
24+
* @param args If provided, it will used as the values to be replaced in the international text
25+
* @returns
26+
*/
27+
export function localize(
28+
sourceKey: string,
29+
defaultValue: string,
30+
...args: string[]
31+
): any {
32+
let localizedValue = container
1433
.resolve(LocaleService)
1534
.localize(sourceKey, defaultValue);
35+
if (args.length) {
36+
args.forEach((replacedVal) => {
37+
localizedValue = localizedValue.replace(
38+
LOCALIZE_REPLACED_WORD,
39+
replacedVal
40+
);
41+
});
42+
}
1643
return localizedValue;
1744
}
1845

0 commit comments

Comments
 (0)