angyLe
5/10/2017 - 8:16 AM

Custom localization provider

Custom localization provider

in startup  

add this                 
//options.RequestCultureProviders.Insert(1, new CustomLocalizationProvider());

services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new[]
                {
                     new CultureInfo("nb"),
                     new CultureInfo("nb-NO"),
                     new CultureInfo("en"),
                     new CultureInfo("en-US"),
                 
                };

              
                options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");

              
                options.SupportedCultures = supportedCultures;

               
                options.SupportedUICultures = supportedCultures;
                
                ....add here

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Http;


namespace ForumWeb.Controllers.Helpers
{
    public class CustomLocalizationProvider : IRequestCultureProvider
    {
       
        public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            var pathSegments = httpContext.Request.Path.Value.Split('/');

            string bla = httpContext.Request.Headers["Accept-Language"];
           
            string bllla = httpContext.Request.Headers["AcceptLanguage"];
            string culture = "ru-RU"; //pathSegments.FirstOrDefault(x => x.StartsWith("custom-culture-"))?.Substring("custom-culture-".Length);
            string uiCulture = "ru-RU";  pathSegments.FirstOrDefault(x => x.StartsWith("custom-ui-culture-"))?.Substring("custom-ui-culture-".Length);

            var result = new ProviderCultureResult(culture, uiCulture);

            return Task.FromResult(result);
            
        }
    }
}