Skip to content

Instantly share code, notes, and snippets.

@hediet
Created June 15, 2020 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hediet/7df4e2a8842538234bac604e2a85adc4 to your computer and use it in GitHub Desktop.
Save hediet/7df4e2a8842538234bac604e2a85adc4 to your computer and use it in GitHub Desktop.
Dynamic Translation
/**
* Renders a translation that cannot be extracted statically.
* Avoid using this component as much as possible!
*/
export class TransMsgDynamic extends React.Component<{
keyAndDefaultTranslation: string;
/**
* A static id that represents the pool of all possible keys.
* If this id is not used in the code base anymore,
* all keys associated with this id can be removed.
*/
keyPoolId: string;
data?: I18nData;
}> {
render() {
const { keyAndDefaultTranslation, data } = this.props;
const doc = parseGemlMarkupString(keyAndDefaultTranslation);
return <>{evaluateGemlMarkupStringToReact(doc, data || {})}</>;
}
}
/**
* Translates text that cannot be extracted statically.
* Avoid using this component as much as possible!
*/
export function transStrDynamic(
keyAndDefaultTranslation: string,
options: {
/**
* A static id that represents the pool of all possible keys.
* If this id is not used in the code base anymore,
* all keys associated with this id can be removed.
*/
keyPoolId: string;
data?: I18nData;
}
): string {
const doc = parseGemlMarkupString(keyAndDefaultTranslation);
return evaluateGemlMarkupStringToString(doc, options.data || {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment