KEYCLOAK-13740 use children as Msg parameters

This commit is contained in:
Erik Jan de Wit 2020-04-07 09:41:36 +02:00 committed by Stan Silvert
parent 9389332675
commit 3987ce7d94
2 changed files with 18 additions and 6 deletions

View file

@ -258,7 +258,9 @@ class SigningInPage extends React.Component<SigningInPageProps, SigningInPageSta
<Title headingLevel={TitleLevel.h3} size='2xl'>
<strong id={`${credContainer.type}-cred-title`}><Msg msgKey={credContainer.displayName}/></strong>
</Title>
<span id={`${credContainer.type}-cred-help`}><Msg msgKey={credContainer.helptext}/></span>
<span id={`${credContainer.type}-cred-help`}>
{credContainer.helptext && <Msg msgKey={credContainer.helptext}/>}
</span>
</DataListCell>,
]}/>

View file

@ -19,7 +19,7 @@ import * as React from 'react';
declare const l18nMsg: {[key: string]: string};
export interface MsgProps {
readonly msgKey: string | undefined;
readonly msgKey: string;
readonly params?: string[];
}
@ -30,14 +30,24 @@ export class Msg extends React.Component<MsgProps> {
}
public render(): React.ReactNode {
if (this.props.children) {
return Msg.localizeWithChildren(this.props.msgKey, this.props.children);
}
return (
<React.Fragment>{Msg.localize(this.props.msgKey, this.props.params)}</React.Fragment>
);
}
private static localizeWithChildren(msgKey: string, children: React.ReactNode): React.ReactNode {
const message: string = l18nMsg[this.processKey(msgKey)];
const parts = message.split(/\{\{param_\d*}}/);
const count = React.Children.count(children);
return React.Children.map(children, (child, i) =>
[parts[i], child, count === i + 1 ? parts[count] : '']
);
}
public static localize(msgKey: string | undefined, params?: string[]): string {
if (msgKey === undefined) return '';
public static localize(msgKey: string, params?: string[]): string {
let message: string = l18nMsg[this.processKey(msgKey)];
if (message === undefined) message = msgKey;
@ -47,7 +57,7 @@ export class Msg extends React.Component<MsgProps> {
message = message.replace('{{param_'+ index + '}}', value);
})
}
return unescape(message);
}