Metronic Mega Update!Tailwind 4, React 19, Next.js 15, KtUI, ReUI, eCommerce, User Management Apps and more
Explore Update

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
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (121)


Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.
mau777 slot



Whether you are involved in affiliate marketing, selling items through ClickBank, or promoting your own personal business and website, ads are an important part of one's success. Some people make a recurring income simply by placing advertisements on the websites that get a lot of traffic.
casino online



Internet search engine optimization experts apply the modern analytics service, which has a positive affect a website. SEO companies are facing great competition in the SEO field. However, they introduce guaranteed SEO services to manage with the competition.
alexistogel



Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...
austin gutter cleaning



his is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.
bandar36



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



Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
[url=



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



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:

import { TranslateService } from "@ngx-translate/core";


2. Inject the `TranslateService` in your component's constructor:

constructor(public translateService: TranslateService) { }


3. Use the `translateService.instant()` method to translate the text programmatically:

const translatedMessage = this.translateService.instant("AUTH.VALIDATION.INVALID");


4. In your template, assign the translated message to the `message` property:

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


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:


"AUTH.VALIDATION.INVALID": "{{controlname}} is not valid"


2. Update the message property in your ngTemplateOutletContext to use the translation key instead of the hardcoded string:


<ng-container [ngTemplateOutlet]="formError" [ngTemplateOutletContext]="{
validation: "invalid",
message: "AUTH.VALIDATION.INVALID",
control: loginForm.controls["email"],
params: { controlname: "Email" }
}"></ng-container>


This will use the translation value for the key AUTH.VALIDATION.REQUIRED as the message.

This will replace {{controlname}} 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:
<img src=" alt="image" border="0">

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

1. Import the TranslationService into the LoginComponent.
2. Use the following method to set the language: this.translationService.setLanguage('en'); (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
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(