0

I want to access values stored in Cookie in config phase of angularjs, how can I do it? Is it possible? I currently have the below code inside app.config(function(..., $translateProvider){...}) which is working perfectly fine:

var defaultLang = 'de-DE';
var moduleName = { moduleName: 'UICaption.Resources' };

    $translateProvider.useLoader('customLocalizationLoaderFactory',
    {        
        moduleName, url: resourceUrl
    });

    $translateProvider.preferredLanguage(defaultLang);

I want to achieve something like(below code) inside app.config(), but I think only providers are registered and accessed during configuration phase, so how should I access the values stored in cookies in config phase via $cookies service or any other way:

 var defaultLang = $cookies.get('ASPNET_CULTURE' === 'de-DE') ? 'de-DE' : 'en-US';
    var moduleName = { moduleName: 'UICaption.Resources' };

    $translateProvider.useLoader('customLocalizationLoaderFactory',
    {
        moduleName, url: resourceUrl
    });

    $translateProvider.preferredLanguage(defaultLang);

Please guide if any other approach is there for the same?

Thank you.

Pranay
  • 432
  • 4
  • 14
  • 1
    [This](http://stackoverflow.com/questions/20588114/how-to-use-cookiesprovider-in-angular-config) old answer might help you. – Redd Sanso May 15 '17 at 09:58
  • for constants, one way is to have them in seperate module that can be injected in current module's config/controller/anywhere else as mentioned in [this answer](http://stackoverflow.com/a/28416656/4315380) – tanmay May 15 '17 at 10:00
  • one of the answer (Injecting cookies manually) in the link shared by Redd works for me. Thank you. – Pranay May 15 '17 at 14:23

1 Answers1

0

This old answer might help you.

More generally, $cookies are meant to be retrieved in the .run() phase. I suggest you to load all your $translate partials and languages in the .config() and the set an active language using your cookie in your .run().

Community
  • 1
  • 1
Redd Sanso
  • 51
  • 4