【问题标题】:How to Apply changes to resource.resx file during runtime(for changing language)如何在运行时将更改应用到 resource.resx 文件(用于更改语言)
【发布时间】:2015-11-10 06:55:20
【问题描述】:

如何在运行时将更改应用到 resource.resx 文件我的代码如下, 我的问题是资源文件是 resorce.resx 正在改变,但只有在第二次重新加载后才得到它

            string xmlPath = Server.MapPath("~/XMLFile1.xml");
            string resourcePath = Server.MapPath("~/LocalResource/Resource.sv-SE.resx");
            System.Xml.XmlTextReader reader = new XmlTextReader(xmlPath);

            ResXResourceWriter writer = new ResXResourceWriter(resourcePath);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "string")
                    writer.AddResource(reader.GetAttribute("name"), reader.ReadString());
            }

            writer.Generate();
            writer.Close();



   //here I set it to Thread.CurrentThread.CurrentUICulture = new CultureInfo("sv-SE");     
        CultureHelper.CurrentCulture = id;
        //
        // Cache the new current culture into the user HTTP session. 
        //
        Session["CurrentCulture"] = id;
        //
        // Redirect to the same page from where the request was made! 
        //

        return Redirect(Request.UrlReferrer.ToString());
    }

【问题讨论】:

    标签: c# asp.net-mvc-4 model-view-controller localization globalization


    【解决方案1】:

    您不必更改文件。
    您必须创建多个具有不同文化的资源文件
    例如 Message.resx 、 Message.fa.resx 、 Message.fr.resx
    Visual Studio 可以检测当前文化并加载合适的文件
    要实现多语言功能,您有两种方法。
    1- 使用基本控制器和继承

    public class BaseController : Controller
    {
        private const string LanguageCookieName = "MyLanguageCookieName";
        protected override void ExecuteCore()
        {
            var cookie = HttpContext.Request.Cookies[LanguageCookieName];
            string lang;
            if (cookie != null)
            {
                lang = cookie.Value;
            }
            else
            {
                lang = ConfigurationManager.AppSettings["DefaultCulture"] ?? "fa-IR";
                var httpCookie = new HttpCookie(LanguageCookieName, lang) { Expires = DateTime.Now.AddYears(1) };
                HttpContext.Response.SetCookie(httpCookie);
            }
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
            base.ExecuteCore();
        }
    }
    


    2-使用属性

    public class LocalizationActionFilterAttribute : ActionFilterAttribute
    {
        private const string LanguageCookieName = "MyLanguageCookieName";
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var cookie = filterContext.HttpContext.Request.Cookies[LanguageCookieName];
            string lang;
            if (cookie != null)
            {
                lang = cookie.Value;
            }
            else
            {
                lang = ConfigurationManager.AppSettings["DefaultCulture"] ?? "fa-IR";
                var httpCookie = new HttpCookie(LanguageCookieName, lang) { Expires = DateTime.Now.AddYears(1) };
                filterContext.HttpContext.Response.SetCookie(httpCookie);
            }
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
            base.OnActionExecuting(filterContext);
        }
    }
    

    culture code lists

    【讨论】:

    • 嗨,一切对我来说检测语言、转换语言都工作正常,但我尝试从 xml 填充 resource.resx 即表。如上所示。从 xml 到资源表的绑定也有效很好,但问题是在运行时如果我更改 xml,资源表正在更新。但是资源表中映射的语言仅是第二次映射,即重新加载后
    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2015-12-04
    • 1970-01-01
    • 2013-12-19
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多