Home > Back-end >  React Localization Dynamic String
React Localization Dynamic String

Time:10-11

I have a problem. I want to add total value in local string dynamicly.There is a space in local string.

page_title: `Toplamda    adet ******* eğitmeni listeleniyor.`,

I have a total value.

let total = teachers.length

I want to add total value in localization string's space.

<p className="mt-0 font-bold text-center max-w-lg mx-auto px-5">
    {`${strings.teachers.teachers_list.page_title}`}
 </p>

How can I do it?

CodePudding user response:

Based on your explanation you need to insert value into string at specific index. You can find here example Insert a string at a specific index

The other way is very primitive but it will work in your case. As I see you have 3-4 blank spaces together, you can split string by spaces and then join them back together.

const message = 'Hello   world in person'
let test = message.split('   ')
let num = 5
let result = test[0]   " "   num   " "   test[1]
console.log(result)

You can modify you existing variable strings.teachers.teachers_list.page_title or you can make new one from it and work on it. Its up to you.

  • Related