-1

There is NG-module $translate.

There is method $translate.use(locale), but it changes locale at all, of course.

I need to get a word from other locales without locale switching. Is it possible?

UPD. Resolved with adding provider:

.provider('vocabularies', function($translateProvider) {
    this.$get = () => $translateProvider.translations();
})

So, in controller I can just add:

function(vocabularies) {
    console.log(vocabularies['en-US']['key.of.needed.words']);
}

Thanks everyone!

Alx Aux
  • 53
  • 7

1 Answers1

0

As was mentioned here $translate.use() is a getter and a setter, which means that you can store current locale in a variable, change it to a new one, extract a word and restore the locale to the original one.

Another solution would be to create a factory that holds all the translations and use it to both configure the $translateProvider, as well as access it later without changing the locale. This is generally a better solution I'd say.

Community
  • 1
  • 1
Voreny
  • 765
  • 6
  • 13
  • You mean smth like this: let lang = $translate.use(); $translate.use('ru-RU'); let word = $translate.instant('needed.word.key'); $translate.use(lang); ? If yes, this decision doesn't work properly, it leaves not «lang» but 'ru-RU'. I miss smth? – Alx Aux Jan 29 '16 at 12:01
  • @Alx.Aux yes, that's what I meant. I don't have much experience with Angular Translate, but I guessed that's how it should work. In this case you should create another factory that would consist of all the translations and call it directly when you need it, as well as fill the translateProvider with it – Voreny Jan 29 '16 at 14:01
  • @Alx.Aux check this answer http://stackoverflow.com/a/23170915/3375424 for a working example, the `$translate.use` method is asynchronous – Andrei Cojea May 10 '16 at 13:54