0

I am working on localization within my angular project. It reads a JSON file which is having key and it's localized value in string.

Like: text1:"Localized text"

This prints "Localized text" on page correctly. But when I need to add some dynamic text in the string, like:

text1: "Showing page {{cur_page_num}} of {{total_pages}} pages"

where the cur_page_num and total_pages values will come from controller. I have tried

"Showing page {{cur_page_num}} of {{total_pages}} pages"

but it's printing {{cur_page_num}} and {{total_pages}} as it is without evaluating it.

4
  • Is it about AngularJS (1.x), or Angular (2+). In any case, why are you reinventing this wheel instead of using what already exists? Commented Apr 17, 2017 at 11:53
  • Using Angular1.5 Commented Apr 17, 2017 at 11:55
  • 1
    Use angular-translate: angular-translate.github.io. Don't reinvent the wheel. Commented Apr 17, 2017 at 13:18
  • Thanks @JBNizet, this is perfect! Commented Apr 20, 2017 at 10:24

3 Answers 3

2

use ng-bind-html

EX:

<p ng-bind-html="obj.text1"></p>

At this point you may get an error.

attempting to use an unsafe value in a safe context error

you need to either use ngSanitize or $sce to resolve that.

Sign up to request clarification or add additional context in comments.

2 Comments

The string is written in JSON so I can not use text1: "Showing page " + {{cur_page_num}}
@Gaurav this might be your solution.
1

try storing the json values in an array . iterate it on html & render it there .

Comments

0

if it is from template then your code should be like this,

text1: "Showing page " + {{cur_page_num}} + " of " + {{total_pages}} + " pages"

or if it is from controller then it can be like below,

text1: "Showing page " + $scope.cur_page_num + " of " + $scope.total_pages + " pages"

2 Comments

It is from JSON file.
can you share sample json?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.