diff --git a/libs/components/src/i18n/i18n.component.ts b/libs/components/src/i18n/i18n.component.ts index 170f36ca133..dddca19028e 100644 --- a/libs/components/src/i18n/i18n.component.ts +++ b/libs/components/src/i18n/i18n.component.ts @@ -53,6 +53,12 @@ export class I18nComponent implements AfterContentInit { @Input("key") translationKey: string; + /** + * Optional arguments to pass to the translation function. + */ + @Input() + args: (string | number)[] = []; + @ContentChildren(I18nTagDirective) templateTags: QueryList; @@ -61,7 +67,14 @@ export class I18nComponent implements AfterContentInit { constructor(private i18nService: I18nService) {} ngAfterContentInit() { - this.translationParts = this.parseTranslatedString(this.i18nService.t(this.translationKey)); + const translatedText = this.i18nService.t( + this.translationKey, + this.args[0], + this.args[1], + this.args[2] + ); + + this.translationParts = this.parseTranslatedString(translatedText); // Assign any templateRefs to the translation parts this.templateTags.forEach((tag, index) => { this.translationParts.forEach((part) => { diff --git a/libs/components/src/i18n/i18n.mdx b/libs/components/src/i18n/i18n.mdx index ed85da1d6fe..26e070980fa 100644 --- a/libs/components/src/i18n/i18n.mdx +++ b/libs/components/src/i18n/i18n.mdx @@ -32,12 +32,12 @@ will be rendered as is. The entire sentence can be <2>translated as a whole and re-arranged according to each language's grammar rules." --> - {{ text }} - {{ text }} + {{ text }} + {{ text }} + - {{ text }} @@ -80,10 +80,30 @@ rendered as is. The entire sentence can be <2>translated as a whole and re-arranged according to each language's grammar rules." --> - {{ text }} - {{ text }} + {{ text }} + {{ text }} ``` + +## Missing Template Example + +You can also pass arguments to the `i18nService.t()` method via the `[args]` input attribute; + + + +
+ +### Source + +```html + + + + {{ text }} + +``` diff --git a/libs/components/src/i18n/i18n.stories.ts b/libs/components/src/i18n/i18n.stories.ts index f98abcd8da9..5c1f842da53 100644 --- a/libs/components/src/i18n/i18n.stories.ts +++ b/libs/components/src/i18n/i18n.stories.ts @@ -24,6 +24,7 @@ export default { otherExample: ` This is another example with <1>bold tags to show that tag order does not matter and the <0>link tags are after.`, + argExample: (arg1: string) => `This is an example with <0>link tags and ${arg1}.`, }); }, }, @@ -63,6 +64,18 @@ export const AttributeSelector: Story = { }), }; +export const ArgsExample: Story = { + render: (args) => ({ + props: args, + template: ` +

+ {{ text }} + {{ text }} +

+ `, + }), +}; + export const MissingTemplate: Story = { render: (args) => ({ props: args,