The New Way to Build with AI! Introducing ReUI — the developer platform for agentic UI with shadcn/ui
Browse ReUI Pro

angular translate


Hi,

I want to translate below "message" according to the selected language, but I dont find a way to do so? Can I do it in the html file itself?

using "{{ "AUTH.VALIDATE.REQUIREDMESSAGE" | translate }}" gives error to me.


<ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{
validation: 'required',
message: 'Password is required',
control: loginForm.controls['password']
}"></ng-container>


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (26)

Deleted comment
Deleted comment
Deleted comment
Deleted comment

I am often to blogging and that i actually appreciate your posts. This article has truly peaks my interest. I am going to bookmark your internet site and keep checking for first time data
bandar36


Deleted comment
Deleted comment

Hi Rohan,

Apologies for the confusion earlier. Here's the corrected approach to translate text programmatically in Angular using the TranslateService:

  1. Import the necessary modules and services:
<pre> import { TranslateService } from '@ngx-translate/core'; </pre>
  1. Inject the TranslateService in your component's constructor:
<pre> constructor(public translateService: TranslateService) { } </pre>
  1. Use the translateService.instant() method to translate the text programmatically:
<pre> const translatedMessage = this.translateService.instant('AUTH.VALIDATION.INVALID'); </pre>
  1. In your template, assign the translated message to the message property:
<pre> <ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{ validation: 'required', message: translatedMessage, control: loginForm.controls['email'] }"></ng-container> </pre>

Thanks

Hi Rohan,

To move the message "Email is required" into the translation file, you can follow these steps:

  1. Create a key-value pair for the message in your translation file. For example, in your en.json file, add the following. You can also use parameterized translation in the following way:
<pre> "AUTH.VALIDATION.INVALID": "{{controlname}} is not valid" </pre>
  1. Update the message property in your ngTemplateOutletContext to use the translation key instead of the hardcoded string:
<pre> <ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{ validation: 'invalid', message: 'AUTH.VALIDATION.INVALID', control: loginForm.controls['email'], params: { controlname: 'Email' } }"></ng-container> </pre>

This will use the translation value for the key <code>AUTH.VALIDATION.REQUIRED</code> as the message.

This will replace <code>{{controlname}}</code> in the translated string with the value "Email".

Thanks


Hi Faizal,

<a target="_blank" href=" 1</a>

<a target="_blank" href=" 2</a>

<a target="_blank" href=" 3</a>

Please open above images in browser, I am not able to attach them in the editor here.

And as you could see, I have done required changes as you suggested, but still its not working for validation message. Please let me know if you want further details from my side.

Good news is: the label "Email" is already coming from translation file, but not the validation message.

Please help bro.


Hi Rohan,

The translation functionality should work on the login page. However, please note that the language selection option is available in the dashboard. You can refer to this screenshot for the location of the language selection: <a href="https://ibb.co/TBgfyjg"><img src="https://i.ibb.co/xS7kZ97/image.png" alt="image" border="0"></a>

If you want to change the language programmatically from the login page, you can use the following steps:

  1. Import the <code>TranslationService</code> into the <code>LoginComponent</code>.
  2. Use the following method to set the language: <code>this.translationService.setLanguage('en');</code> (replace 'en' with the desired language code).
  3. Please note that the available languages in the demo are: en, zh, es, ja, de, fr.

Let me know if you have any further questions or concerns.

Thanks


Hey Faizal,

Sorry if I am not clearly stating my problem.

I am able to localize the label control but not able to localize validation messages for the email text box on login page.


<label class="form-label fs-6 fw-bolder text-dark">{{ "AUTH.INPUT.EMAIL" | translate }}</label>
<input class="form-control bg-transparent" type="email" name="email" formControlName="email" autocomplete="off"
[ngClass]="{
"is-invalid": loginForm.controls["email"].invalid,
"is-valid": loginForm.controls["email"].valid
}" />
<ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{
validation: "required",
message: "Email is required",
control: loginForm.controls["email"]
}"></ng-container>
</div>


So, for example above I am able to apply AUTH.INPUT.EMAIL for translation.
My question is, how can I achieve similar translation for message: 'Email is required' ?
I want "Email is required" to be supplied from the language file. (also is it possible to have parameterized translation like "{{controlname}} is not valid" ?)



Hi Rohan,

To use Angular Translate, you need to first make sure you have installed the necessary packages and set up the translation files for the languages you want to use.

Once you have done that, you can use the translate pipe in your HTML files like this:


{{ "AUTH.VALIDATE.REQUIREDMESSAGE" | translate }}


This assumes that you have a translation key AUTH.VALIDATE.REQUIREDMESSAGE defined in your translation files for the currently selected language. If the translation key is not found in the current language file, it will fallback to the default language file.

Make sure you have imported the TranslateModule in your app module and initialized the translation service. You can also set the default language and the fallback language in the forRoot() method when importing the TranslateModule.

If you are still having issues, please provide more information about your setup and any error messages you are receiving.

Thanks



That translate format is not working in the below file and place:

src\app\modules\auth\components\login\login.component.html

<ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{
validation: 'required',
message: 'Email is required',
control: loginForm.controls['email']
}"></ng-container>

I want "message: 'Email is required'" to come from translation file.


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
buy metronic keenthemes mega bundle

ReUI

Open-source React components, blocks and templates built on shadcn/ui and Tailwind CSS by Keenthemes.