【问题标题】:Changing Culture on Drop Down Selection改变下拉选择的文化
【发布时间】:2017-08-24 15:26:12
【问题描述】:

我创建了一个应用程序,我需要在下拉选择中更改文化。

这是我的操作方法代码。

 public ActionResult SetCulture(string lang)
        {
            if (lang == "en")
                return RedirectToAction("Index");

            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang.Trim()); //.TextInfo.IsRightToLeft;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang.Trim());

            List<Agent> lstMainAgent = new List<Agent>();
            List<Agent> lstAgent = db.Agents.ToList();

            for (int i = 0; i < lstAgent.Count(); i++)
            {
                lstAgent[i].AddressCity = Resources.Resource.AddressCity;
                lstAgent[i].AddressCountry = Resources.Resource.AddressCountry;
                lstAgent[i].AddressPostcode =Resources.Resource.AddressPostcode;
                lstAgent[i].AddressStreet = Resources.Resource.AddressStreet;
                lstAgent[i].Name = Resources.Resource.Name;
                lstAgent[i].PhoneNumber = Resources.Resource.PhoneNumber;

                lstMainAgent.Add(lstAgent[i]);
            }
            return View("Index", lstMainAgent);

        }

这似乎有效,但我有动态值列表,其值未添加到资源文件中,并且我在视图中获得空白属性值。我需要打印视图中的所有值。我怎样才能做到这一点?

提前致谢

【问题讨论】:

  • 我不太明白你想在 for 循环中做什么。你能详细说明一下吗?
  • 我不确定我的代码是否正确。使用 for 循环我想将资源值传递给列表中的视图?对吗?
  • 您可以访问视图中的资源。你不需要这个 for 循环。像这样访问@Resources.Resource.AddressCountry

标签: asp.net-mvc localization globalization


【解决方案1】:

如果它不在资源文件中,它将是空白的。但是,您可以拥有一个默认资源文件和专门的资源文件。如果它具有值,则使用专用的(如果不是默认值)填充。

public ActionResult SetCulture(string culture)
{
    try
    {
         // set a default value
        if (string.IsNullOrEmpty(culture))
        {
            culture = "en-US";
        }

        // set the culture with the chosen name
        var cultureSet = CultureInfo.GetCultureInfo(culture);
        Thread.CurrentThread.CurrentCulture =cultureSet;
        Thread.CurrentThread.CurrentUICulture = cultureSet;

        // set a cookie for future reference
        HttpCookie cookie = new HttpCookie("culture")
        {
            Expires = DateTime.Now.AddMonths(3),
            Value = culture
        };
        HttpContext.Response.Cookies.Add(cookie);

        List<Agent> lstAgent = db.Agents.ToList();

        foreach (Agent item in lstAgent)
        {
            item.AddressCity = Resources.Resource.AddressCity;
            item.AddressCountry = Resources.Resource.AddressCountry;
            item.AddressPostcode = Resources.Resource.AddressPostcode;
            item.AddressStreet = Resources.Resource.AddressStreet;
            item.Name = Resources.Resource.Name;
            item.PhoneNumber = Resources.Resource.PhoneNumber;
        }

        return View("Index", lstAgent);

    }
    catch (Exception ex)
    {
        // if something happens set the culture automatically
        Thread.CurrentThread.CurrentCulture = new CultureInfo("auto");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("auto");
    }

    return View("Index");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多