0

I have a angulerjs / grunt / gettext / node.js application. I have generated pot files but how can I make my program translate automatically? I see you can upload them using pootle, zanata etc. but have not found enough online help with this. Thank you in advance

Lance
  • 19
  • 5

1 Answers1

0

I don't know Pootle or Zanata. But I'll tell you a way to translate it automatically based on user-agent once you have the translation files on .json format.

You can have a Lang service in angular like this: http://jsfiddle.net/e5nhttcf/

module.factory('Lang',['$q', '$http', function($q){
    var lang = {};

    lang.literals = {};
    lang.current = navigator.language;
    lang.getLiterals = function(){
        var deferred = $q.defer();
        $http.get(location.origin + "/getlang/"+lang.current).success(function(data, status, headers, config){
            if(data){
                lang.literals = data;
                deferred.resolve();
            }           
        });

        return deferred.promise;
    };

    return lang;

}
}]); 

First, you call getLiterals when initialising the application.

Then, you just need to inject the factory in the controller and assign the literals to a local variable. Then in html you have a reference to localized literals:

<div> {{literals.greetings}} </div>

Note: I did not test the code, use only as a reference

Gerard
  • 196
  • 6
  • Thank you but I don't see how the words get translated – Lance Jan 26 '15 at 12:07
  • The app doesn't translate the strings, just gets one language's strings or the others. Imagine this situation: In the app: lang.greetings - (A string that welcomes the user) In the server: lang/en.json lang/es.json en.json: { "greetings": "Hello user!" } es.json: { "greetings": "Hola usuario!" } So, when the app initializes, this Lang service gets the user's language strings. That way, Where in the app you wrote {{lang.greetings}} You'll see instead the string translated. If you still don't understand I can try to make a fiddle – Gerard Jan 26 '15 at 15:49
  • Yes I understand how to do that but my question is how can I get those Strings translated automatically and then update the json language files? – Lance Jan 30 '15 at 14:40