3

i want to implement a localization for views (should include the body, too). I've done this before by loading a JSON File and than iterate trough the keys. The keys are class names. Than i simply assign the value of the key to the element with the class.

Language File (JSON)

".Header-Title" :   "My Title",
".Header-Text"  :   "Lorem ipsum vehicula interdum."

Code Example

$.load("./Content/Text/main-en.json", function(data) {
   for (key in data) {
     $(key).html(data[key]);
   }
});

I liked this because the text is separated from html and script. Like to know can i accomplish this in AngularJS. My thoughts are to extend the route provider by a additional parameter that takes the path to the JSON Language File. After loading a view a function should be called that's assign the values like in the code example. Just have started with AngularJS. Any ideas or help ?

1 Answer 1

4

You can use the Localization Service by Coding SmackDown TV

1) Load the service, and include your i18n file. For instance, take this

// /i18/en/dictionary_en.js
[
    {
        "key":"_More_",
        "value":"More",
        "description":"More button"
    }
]

// In the localize service
$http({ method:"GET", url:url, cache:false }).success(localize.successCallback).error(function () {
    // the request failed set the url to the default resource file
    var url = '/i18n/en/dictionary_en.js';
    localize.language = 'en';
    // request the default resource file
    $http({ method:"GET", url:url, cache:false }).success(localize.successCallback);
});

2) In your views use the i18 filter or through ng-bind

<button data-i18n="_More_">
Sign up to request clarification or add additional context in comments.

Comments

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.