Marzouk
Marzouk

Reputation: 2714

Identityserver3 get and change localization in runtime

I used IdentityServer3.Contrib.Localization to provide translation to the identityserver.

IdentityServer3.Contrib.Localization provides only localization for scopes, messages, events but still there are missing texts to translate in login page and etc.

I think you should provide a custom views for every language using IViewService but i don't know if this is the correct path.

For example in order to provide a localization for a specific language i register this in startup class configuration:

  // Register the localization service

            idServerServiceFactory.Register(
               new Registration<ILocalizationService>(r => new GlobalizedLocalizationService(
               new LocaleOptions { Locale = "de-DE" })));

but now i want to change the language based on the language that user input or based on the browser accept-language, how can i change the localization for (scopes, events, messages, views) in run time.

some one mention that i can use OwinEnvironementService and inject it to the localization service to get the language but is there any example?

Also i think that i can provide an owin middleware in order to provide the needed change in localization based on the language but any suggestions?

Upvotes: 0

Views: 803

Answers (1)

John Korsnes
John Korsnes

Reputation: 2275

The IdentityServer3.Localization(nuget.org) package can now do this:

var opts = new LocaleOptions
{
    LocaleProvider = env =>
    {
        var owinContext = new OwinContext(env);
        var owinRequest = owinContext.Request;
        var headers = owinRequest.Headers;
        var accept_language_header = headers["accept-language"].ToString();
        var languages = accept_language_header
                                .Split(',')
                                .Select(StringWithQualityHeaderValue.Parse)
                                .OrderByDescending(s => s.Quality.GetValueOrDefault(1));
        var locale = languages.First().Value;

        return locale;
    }
};

var factory = new IdentityServerServiceFactory();
factory.Register(new Registration<LocaleOptions>(opts));
factory.LocalizationService = new Registration<ILocalizationService, GlobalizedLocalizationService>();

=> Link to sample here.

Upvotes: 1

Related Questions