How To Translate Dynamic Strings In Angular 10

Nov 6 · 2 min read
Image for post
Image for post

I am using some dynamic values to create a string and sending that to sweetalert2 configuration.

There are a lot of ways we can translate the title text before passing to sweetalert2 but it is getting difficult when we have dynamic values in the title itself.

I have added the below solution which solves our issue.

Let’s say in our component we have the following status and amount coming dynamically and we want to translate that values before sending to the configurations method.

let status = '';
switch (action?.innerText) {
case 'Accept': {
status = 'status_accepted';
case 'Approve': {
status = 'status_approve';
case 'Delete': {
status = 'status_delete';
case 'Reject': {
status = 'status_rejected';

//You have ${status} a Request for $${}
let transalteTitle;
this.translate.get(status).pipe(switchMap(translatedStatus =>
this.translate.get('title', {
status: translatedStatus, val:
}))).subcribe(translatedTilte => {
const configval = {
title: transalteTitle,
text: 'Do you want to continue?',
showCancelButton: true,
icon: 'warning',
confirmButtonText: 'YES',
cancelButtonText: 'NO'

That’s it. Just add values in JSON as a parameter and it will translate our title.

"title": "You have ${status} a Request for ${val}"
"status_accepted": "accepeted",
"status_rejected": "rejected"
}`enter code here`

Now the question is what if we want to translate sweetlert2 configuration values too? I will add my solution here shortly.

Are you preparing for interviews? Here are frequently asked interview questions in Angular. It covers the Latest interview questions for Angular and Frontend development. Let’s check how many of these questions you can answer?

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store