在 ASP.NET Core 1.0 MVC 中使用本地化

Working with localization in ASP.NET Core 1.0 MVC

我正在尝试在我们的应用程序中实现本地化,但我就是无法让它工作。我的问题是如何正确设置文化以及如何根据文化集访问本地化字符串。

我正在尝试从登录页面的下拉列表设置文化并从数据库中提取资源。

启动文件

public void ConfigureServices(IServiceCollection services)
    {

        services.AddSqlLocalization(options => options.UseTypeFullNames = true);

        services.AddMvc(o =>
        {
            o.Filters.Add(new LanguageActionFilter());
        })
        .AddViewLocalization()
        .AddDataAnnotationsLocalization()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());


        services.Configure<RequestLocalizationOptions>(
             options =>
             {
                 var supportedCultures = new List<CultureInfo>
                     {
                         new CultureInfo("de-DE"),
                        new CultureInfo("en-US"),
                        new CultureInfo("de-DE"),
                       new CultureInfo("fr-FR"),

                     };

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

    }

控制器代码:- 从下拉索引调用更改为 ajax。

[HttpPost]
    public IActionResult SetLanguage(string culture)
    {

            AppTenant tenant = HttpContext.Session.GetObjectFromJson<AppTenant>("TenantInfo");

            if (tenant != null)
            {
                tenant.LoggedInCulture = strdata[0];
                HttpContext.Session.SetObjectAsJson("TenantInfo", tenant);

                string cookieFormat = "culture-name";
                string cookieVal = cookieFormat.Replace("culture-name", tenant.LoggedInCulture);

                Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cookieVal)),
                new CookieOptions { Expires = DateTimeOffset.UtcNow.AddMonths(3) }
               );
            }


        List<SelectListItem> items = new List<SelectListItem>();
        items = HttpContext.Session.GetObjectFromJson<List<SelectListItem>>("LanguageData");

        foreach (var item in items)
        {
            if (item.Value == culture)
            {
                item.Selected = true;
            }
            else
            {
                item.Selected = false;
            }
        }
        var viewModel = new LMS_User { ReturnUrl = string.Empty, LanguageList = items };

        return View("Login", viewModel);

    }

资源过滤器:- 在这里,我尝试在 OnResourceExecutingOnResourceExecuted 方法中设置区域性,如下所示。

public void OnResourceExecuted(ResourceExecutingContext context)
    {
       AppTenant tenant = context.HttpContext.Session.GetObjectFromJson<AppTenant>("TenantInfo");

        CultureInfo.CurrentCulture = new CultureInfo(_culture);
        CultureInfo.CurrentUICulture = new CultureInfo(_culture);
    }

在资源过滤器中,我们应该在哪里设置文化,因为我没有在 OnResourceExecuting 中获得当前下拉值,而是通过会话进入 OnResourceExecuted

我的SqlStringLocalizerFactoryclass从数据库中提取资源

public class SqlStringLocalizerFactory : IStringLocalizerFactory, IStringExtendedLocalizerFactory
{
public IStringLocalizer Create(Type resourceSource)
    {

        SqlStringLocalizer sqlStringLocalizer;

        _culture = CultureInfo.CurrentCulture.ToString();
}

这里我想获取选择的文化,这是我没有得到的,它是 'en-US'

感谢任何帮助!

您必须将当前线程 Culture 和 UICulture 属性更改为所需的文化。将以下代码添加到您的 SetLanguage 操作中,这应该可以解决您的问题:

CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentCulture = new  System.Globalization.CultureInfo(cultureInfo ); 
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cultureInfo );