Using $translateProvider.useUrlLoader

NoobDeveloper picture NoobDeveloper · Feb 2, 2015 · Viewed 9k times · Source

I am using latest version of angularjs and angular-translate.

For some reason, I don't want to store my translations in static .json files (en_US.json or ru_RU.json)

Is there any API in angular-translate which calls a REST API which in turn returns json data for translation. REST API will pull the translations store in database.

Answer

Martin picture Martin · Feb 2, 2015

Tale a look at:

http://angular-translate.github.io/docs/#/api/pascalprecht.translate.$translateUrlLoader

By default it will use the querystring param ?lang= to pass in the desired language.

you would use it like:

$translateProvider.useUrlLoader("/path/to/my/endpoint");
$translationProvider.defaultLanguage("en");

Angular-translate will then make a call to /path/to/my/endpoint?lang=en

If you do not want a query string parameter, but would rather a path parameter, you can create your own loader based on $translateUrlLoader. Looking at the source, it looks very trivial:

https://github.com/angular-translate/angular-translate/blob/master/src/service/loader-url.js