更改 CurrentUICulture 时无法加载正确的资源
Can't load correct ressource when I change the CurrentUICulture
我正在开发 WPF 应用程序。初始化主 Window 时,我获取用户的系统语言并更改 CurrentCulture :
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA");
我在资源文件夹中有 3 个 .resx 文件(resource.fr-BE.resx、resource.nl-BE.resx、resource.resx)。
在我的XAML中,我指的是默认的ressource.resx:
xmlns:r="clr-namespace:Renoir.UserInterface.Resources"
并在我的控件的资源文件中使用密钥:
Title="{x:静态 r:resource.MAINWINDOW_TITLE}"
但是应用程序无法加载正确的资源文件(例如,当我将 Culture 更改为法语时 (fr-BE),它不会加载 resource.fr-BE.resx文件,它总是显示默认资源文件中的字符串(resource.resx)
但是在后面的代码中,如果在更改 CurrentCulture 之后,我尝试从 resx 中获取字符串,我收到了正确的字符串:
ResourceManager rm = null;
rm = new ResourceManager("Renoir.UserInterface.Resources.resource", Assembly.GetExecutingAssembly());
MessageBox.Show(rm.GetString("DATAGRID_AUTHOR"));
有人有想法吗?
提前致谢!
您正试图在 window 初始化后更改文化信息。
在 window
初始化之前,您需要更改 app.xaml.cs 中的文化信息
public partial class App
{
public App()
{
}
protected override void OnStartup(StartupEventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
base.OnStartup(e);
}
}
好的,现在可以了,所以我想我找到了解决方案。在后面的代码中,在初始化 windows 之后(感谢 Valera),我使用用户系统的语言代码更改了应用程序的文化信息。
为了使用正确的资源文件,我必须这样精确:
Renoir.UserInterface.Resources.Resources.Culture = new CultureInfo("nl-BE");
尝试在 window class 中执行以下操作。
this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(culture.IetfLanguageTag)
我正在开发 WPF 应用程序。初始化主 Window 时,我获取用户的系统语言并更改 CurrentCulture :
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA");
我在资源文件夹中有 3 个 .resx 文件(resource.fr-BE.resx、resource.nl-BE.resx、resource.resx)。
在我的XAML中,我指的是默认的ressource.resx:
xmlns:r="clr-namespace:Renoir.UserInterface.Resources"
并在我的控件的资源文件中使用密钥:
Title="{x:静态 r:resource.MAINWINDOW_TITLE}"
但是应用程序无法加载正确的资源文件(例如,当我将 Culture 更改为法语时 (fr-BE),它不会加载 resource.fr-BE.resx文件,它总是显示默认资源文件中的字符串(resource.resx)
但是在后面的代码中,如果在更改 CurrentCulture 之后,我尝试从 resx 中获取字符串,我收到了正确的字符串:
ResourceManager rm = null;
rm = new ResourceManager("Renoir.UserInterface.Resources.resource", Assembly.GetExecutingAssembly());
MessageBox.Show(rm.GetString("DATAGRID_AUTHOR"));
有人有想法吗?
提前致谢!
您正试图在 window 初始化后更改文化信息。 在 window
初始化之前,您需要更改 app.xaml.cs 中的文化信息public partial class App
{
public App()
{
}
protected override void OnStartup(StartupEventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
base.OnStartup(e);
}
}
好的,现在可以了,所以我想我找到了解决方案。在后面的代码中,在初始化 windows 之后(感谢 Valera),我使用用户系统的语言代码更改了应用程序的文化信息。
为了使用正确的资源文件,我必须这样精确:
Renoir.UserInterface.Resources.Resources.Culture = new CultureInfo("nl-BE");
尝试在 window class 中执行以下操作。
this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(culture.IetfLanguageTag)