Angular i18n translation for Dynamic component

半城伤御伤魂 提交于 2019-12-13 16:07:29

问题


I have been reading up a lot on Angular 2+ i18n and have been looking for the best way to try and build dynamic components.

Problem:

  1. i18n cannot translate a variables text into an xlf file for dynamic
  2. i18n makes it hard to make dynamic components with text using i18n attribute

Possible Solution:

Even though we cannot use dynamic strings in variables for i18n to pick up when generating the language resource file we should still be able to create components and pass text to them. For the example below is one form using <app-error> to display one inputs error message as well as animations. There will be other forms so the <app-error> component has to be reusable.

With reusability in mind for other form components I have done something similar to the following. This way allows passing text and using transclusion to inject the already translated text into the error message <app-error> component.

form.component.html

<app-error
  i18n="Error | form validation">
  Please enter a first name.
</app-error>

app-error.component.html

<li>
  <ng-content></ng-content>
</li>

I was able to come up with the above solution to work correctly as i18n will pick it up and translate it as well as the text gets injected properly with <ng-content>. So this looks like a great solution right?

My thought is there must be a better way for passing translated text to components or be able to build dynamic components utilizing i18n. Is there a possible solution for this?

来源:https://stackoverflow.com/questions/50742292/angular-i18n-translation-for-dynamic-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!