如何从本地化中获取价值

本文关键字:获取 本地化 | 更新日期: 2023-09-27 18:15:07

在我的项目中,我添加了3个全局本地化文件:Main.en-US。resx + Main.ru-Ru.resx + Main.uk-UA。

在母版页上,我将区域性设置为会话,如下所示:

protected void SetUSA_Click(object sender, ImageClickEventArgs e)
        {
            Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-US");
            Server.Transfer(Request.Url.LocalPath);  
        }

和在某些类I init culture中:

public class BaseForm : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            SetCulture();
            base.InitializeCulture();
        }
        private void SetCulture()
        {
            if (Session["myCulture"] != null) 
            {
                CultureInfo culture = (CultureInfo)Session["myCulture"];
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;                
            }
        }

一些页面继承这个类:

public partial class ProductType : WrapperObjects.BaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }

在代码中我转换到get value:

string ColumnName = CommonUtils.GetValueField("Main", "ProductTypeNameTitle", (CultureInfo)HttpContext.Current.Session["MyCulture"]);

public static string GetValueField(string nameFileResources, string nameField, CultureInfo currentCulture)
        {
            try
            {
                if (currentCulture == null) currentCulture = new CultureInfo("ru-RU");
                object fieldValue = HttpContext.GetGlobalResourceObject(nameFileResources, nameField, currentCulture);
                return fieldValue == null ? nameField : fieldValue.ToString();
            }
            catch (Exception ex)
            {
                return nameField;
            }
        }

但它不起作用。我找不到我的错误

如何从本地化中获取价值

您似乎混淆了会话变量名称"myCulture""MyCulture"