Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Localization

$
0
0

I added a route to every request of my site like:

routes.MapRoute(
                    name: null,
                    template: "{culture}",
                    defaults: new { controller = "Home", action = "Index", culture = "en" });

                routes.MapRoute(name: null, template: "{culture}/{controller}/{action}");

and I have a dropdown that adds this route for different languages I have. Then in startup:

 services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

            var supportedCultures = new[]
            {
                new CultureInfo("en"),
                new CultureInfo("fa"),
                new CultureInfo("ar"),
            };

            var options1 = new RequestLocalizationOptions()
            {
                DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures,
            };
            options1.RequestCultureProviders = new[]
            {
                new RouteDataRequestCultureProvider() { Options = options1 } // RouteDataRequestCultureProvider
            };
            services.AddSingleton(options1);

a Middeware:

using Microsoft.AspNetCore.Builder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SportsStore.Infrastructure
{
    public class LocalizationPipeline
    {
        public void Configure(IApplicationBuilder app, RequestLocalizationOptions options)
        {
            app.UseRequestLocalization(options);
        }
    }
}

and in the controller:

[Authorize]
    [MiddlewareFilter(typeof(LocalizationPipeline))]
    public class AccountController : Controller
    {
        private readonly IStringLocalizer<AccountController> _localizer;

        public AccountController(UserManager<AppUser> userMgr,
                IStringLocalizer<AccountController> localizer)
        {
            _localizer = localizer;
        }
 [AllowAnonymous]
        public IActionResult Login(string returnUrl, string culture) // do moshkel localizer not working and query string hefz nemishavad during postback
        {
            ViewBag.returnUrl = returnUrl;
            ViewBag.culture = culture;
            ViewBag.User = _localizer["User"];
            ViewBag.Password = _localizer["Password"];
            return View();
        }

finally I added resource files to resources folder: like: Controllers.AccountController.en.resx and other languages as well. and when I use for example @ViewBag.User in the Login view that I passed ViewBag.User = _localizer["User"]; I got English text only while i see "fa" in my url: http://localhost:53406/fa/Account/Login?returnUrl=%2Ffa%2FIndex

I also renamed all resource files to something like: AccountController.en.resx with no change in results.

How do I correct this?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>