Using ng2-translate within a TypeScript component file

In the home.component.ts file, I have the following code:

notificationId.innerHTML = message 

This code displays a message as a notification on the site.

I am trying to figure out how to translate the message and display the translated version on the site. Here is what I have tried:

this.translate.instant(message)
notificationId.innerHTML = message 

Unfortunately, this approach did not work. I have all the translations for the messages stored in a json file.

Thank you in advance for any help or suggestions!

Answer №1

When using the instant method, it is important to ensure that the language package is loaded in order for the translation to work. A better option for translating inside a component's TypeScript file is to use the get method, as you can subscribe to its value. Here is an example:

this.translate.get(message).subscribe(translatedMsg => notificationId.innerHTML = translatedMsg);

However, it is advisable not to use innerHTML. It is recommended to use two-way binding in the template instead, like

<div class="notification">{{notificationMessage}}</div>
, and then implement this variable in the TypeScript file. Also, make sure that you have correctly loaded the language package in the app.module.ts.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating complex types using Knockout.js observables is a straightforward process

Currently, I am delving into the world of knockout.js and finding it quite challenging to figure out how to create nested complex types within it. For instance, in my backend model, I have defined: class Person { public string Name {get; set;} public ...

Is there a way to use jq to divide a JSON stream of objects into individual files according to the values of a specific object property?

Dealing with a hefty file (20GB+ compressed) named input.json, filled with a stream of JSON objects like so: { "timestamp": "12345", "name": "Some name", "type": "typea" } { "timestamp": "12345", "name": "Some name", "type": "typea ...

Ways to resolve the issue of not depending on “@angular/core” and/or “rxjs” in Ionic

Attempting to launch my Ionic project on a local server but encountering issues. Executing the ionic serve command in cmd prompts an error message. Even after attempting to run npm link, I continue to face errors. Which specific commands should be execut ...

Error: Unable to import Scss font file into Angular application

My font won't load and isn't displaying on the webpage. I've tried numerous solutions, but this stubborn font just won't show up. Appreciate any help in advance! fonts.scss : @font-face { font-family: Montserrat, sans-serif; src: u ...

How should one properly handle JSON parsing in PHP?

After receiving a JSON string from a Node JS Service, I encountered an issue when trying to use json_decode() in PHP. The var_dump showed the string with extra characters and caused a syntax error. To resolve this, I attempted to replace some of the probl ...

Instructions on how to retrieve a JSON string from a complex string using a shell script

Consider the given string below: arn:aws:secretsmanager:us-east-1:3264873466873:secret:foo/bar 1564681234.974 foo/bar {"username":"admin","password":"admin123","secret_key":"KASJDFJHAKHFKAHASDF"} 4e397333-3797-4f0b-ad7e-8c1cc0ed041c VERSIONSTAGES AWSCURRE ...

The compilation time of Webpack and Angular 2

My compile time is currently at 40 seconds and I'm looking for ways to speed it up. I attempted setting the isolatedModules flag to true in the configuration but encountered an error: error TS1208: Cannot compile namespaces when the '--isolated ...

Design an element that stretches across two navigation bars

Currently, I have implemented two Navbars on my website as shown in the image below: https://i.stack.imgur.com/4QmyW.png I am now looking to include a banner that clearly indicates that this site is a test site. In addition, I would like to incorporate a ...

Transform Jupiter Tessellation (JT) documents into JSON format for display in THREE.js

Looking for guidance on rendering .JT files in THREE.js. Explored different options but haven't found a solution yet: Checked various loaders in Three.js but couldn't find one for JT files. Please advise if there's something I've ov ...

Identifying the category of a value through a conditional check on another value

I am looking for my code editor to automatically determine the type of extraData based on the value of error, which is being narrowed down by an if statement: export enum ErrorCodes { Unknown = 'UNKWN', BadRequest = 'BDREQ', } int ...

How to integrate a static website hosted on an Azure Storage Account into an Asp.net MVC application?

Looking to integrate a React application into a dotnet MVC project? I have a straightforward React app that consists of a KendoUI grid. I took the React app, did a build, and uploaded it as a static website using an Azure Storage Account. Then, I set ...

Guide to activating the submit button when a radio button is selected

Here is the code snippet for an edit form <form [formGroup]="editForm" (ngSubmit)="saveUser()" novalidate> <div class="form-group"> <label class="block">Gender</label> <div class="clip-radio radio-primary"> &l ...

Sending data from a parent component to a child component via reference

I am facing an issue with my 2 components: parentComponent and childComponent. The parentComponent contains a FormGroup named parentForm, which is passed to the childComponent through @input: export class parentComponent { ... parentForm: FormGroup; .. ...

Error in Typescript: 2 arguments were provided instead of the expected 0-1 argument

When attempting to run a mongoose schema with a timestamp, I encountered an error: error TS2554: Expected 0-1 arguments, but got 2. { timestamps: true } Below is the schema code: const Schema = mongoose.Schema; const loginUserSchema = new Schema( { ...

Is there a way to automatically populate the result input field with the dynamic calculation results from a dynamic calculator in Angular6?

My current challenge involves creating dynamic calculators with customizable fields. For example, I can generate a "Percentage Calculator" with specific input fields or a "Compound Interest" Calculator with different input requirements and formulas. Succes ...

What are the steps to set up ChartJS on a personal computer?

Currently, I am working on creating charts with ChartJS using the CDN version. However, I would like to have it installed directly on my website. After downloading ChartJS v4.1.1, I realized that it only contains typescript files. Since I cannot use TS fil ...

Using ngIf in Angular and eliminating components in Angular 6

Currently, I am faced with the challenge of removing the header and footer components in one specific component, but I'm unsure about how to proceed. In my approach, I consider the app component as the index.html component. Whenever I create a new com ...

Utilizing Mongoose Schema Enums Alongside TypeScript Enums

In our Typescript-based NodeJs project utilizing Mongoose, we are seeking the right approach to define an enum field on a Mongoose schema that aligns with a Typescript enum. To illustrate, consider the following enum: enum StatusType { Approved = 1, ...

Extracting data from a JSON object using the BeautifulSoup library

Context I am trying to extract data from this website. Specifically, I need to retrieve the name of each product, its price, and image. However, when I inspected the page's HTML structure, I couldn't find the div elements that contain this infor ...

Working with button loops in react.js

Just started learning react.js and I'm trying to display a list of names as buttons. const exampleComponent: React.FC<IProps> = () => { const renderButtons= () => { for(let i=0; i<names.length; i++){ <TextButt ...