改变文化只会改变模型项目的语言
Changing culture only changing the language of model items
我已经在我的 Asp.net 应用程序中实现了文化。但我想要的是改变整个页面的语言,但它只改变模型项目的语言。
CultureHelper.cs
public class CultureHelper
{
protected HttpSessionState session;
public CultureHelper(HttpSessionState httpSessionState)
{
session = httpSessionState;
}
public static int CurrentCulture
{
get
{
switch (Thread.CurrentThread.CurrentUICulture.Name)
{
case "en": return 0;
case "hi-IN": return 1;
default: return 0;
}
}
set
{
if (value == 0)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
}
else if(value==1)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
}
else
{
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
}
}
}
BaseController.cs
protected override void ExecuteCore()
{
int culture = 0;
if (this.Session == null || this.Session["CurrentCulture"] == null)
{
int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Culture"], out culture);
this.Session["CurrentCulture"] = culture;
}
else
{
culture = (int)this.Session["CurrentCulture"];
}
// calling CultureHelper class properties for setting
CultureHelper.CurrentCulture = culture;
base.ExecuteCore();
}
protected override bool DisableAsyncSupport
{
get { return true; }
}
我的模型class
[Display(Name="Name",ResourceType =typeof(Resource))]
仅针对模型 class 属性更改语言。但我也想更改 static/nonmodel 属性的语言。就像我也想更改按钮文本一样。我已经在资源文件中添加了所有内容。我怎样才能做到这一点?
为您想要支持的每种文化添加一个资源文件,例如
Resources.en.resx
Resources.hi-IN.resx
框架将根据集合CurrentCulture
决定使用哪个文件。
Resources.resx
(没有文化名称)将在找不到文化特定文件时用作回退。
使用资源文件检索翻译后的字符串,例如在你的 View.cshtml:
@using MyProject.Resources
<button type="submit">
@Resources.Submit @* this retrieves the translation for the key "Submit" *@
</button>
我已经在我的 Asp.net 应用程序中实现了文化。但我想要的是改变整个页面的语言,但它只改变模型项目的语言。
CultureHelper.cs
public class CultureHelper
{
protected HttpSessionState session;
public CultureHelper(HttpSessionState httpSessionState)
{
session = httpSessionState;
}
public static int CurrentCulture
{
get
{
switch (Thread.CurrentThread.CurrentUICulture.Name)
{
case "en": return 0;
case "hi-IN": return 1;
default: return 0;
}
}
set
{
if (value == 0)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
}
else if(value==1)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
}
else
{
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
}
}
}
BaseController.cs
protected override void ExecuteCore()
{
int culture = 0;
if (this.Session == null || this.Session["CurrentCulture"] == null)
{
int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Culture"], out culture);
this.Session["CurrentCulture"] = culture;
}
else
{
culture = (int)this.Session["CurrentCulture"];
}
// calling CultureHelper class properties for setting
CultureHelper.CurrentCulture = culture;
base.ExecuteCore();
}
protected override bool DisableAsyncSupport
{
get { return true; }
}
我的模型class
[Display(Name="Name",ResourceType =typeof(Resource))]
仅针对模型 class 属性更改语言。但我也想更改 static/nonmodel 属性的语言。就像我也想更改按钮文本一样。我已经在资源文件中添加了所有内容。我怎样才能做到这一点?
为您想要支持的每种文化添加一个资源文件,例如
Resources.en.resx
Resources.hi-IN.resx
框架将根据集合CurrentCulture
决定使用哪个文件。
Resources.resx
(没有文化名称)将在找不到文化特定文件时用作回退。
使用资源文件检索翻译后的字符串,例如在你的 View.cshtml:
@using MyProject.Resources
<button type="submit">
@Resources.Submit @* this retrieves the translation for the key "Submit" *@
</button>