ModernWpf
ModernWpf copied to clipboard
How to change the language of the controls?
Hi, I could not find anywhere on code/issues how I can change the display language.
For example, In my case I have these:
I want to change these to chinese also so instead of "On" will be written chinese word of "On"
I tried to guess it. According to your source code here: https://github.com/Kinnara/ModernWpf/blob/a23d5bcc6e8c857161f7c7dfe6b3d18d548259fe/test/ModernWpfTestApp/App.xaml.cs#L37
I tried to guess and did this:
MainWindow.Language = XmlLanguage.GetLanguage("zh-cn");
But it did not woked
Any idea how to do it? And I want to do it at runtime.
Thanks.
I do not know if there is an internal method for this or not, but you can change the language this way
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
Thanks but It doesn't work. I already have it in the code. I still see "On"/"Off"
@gileli121 Did you write in App.cs ctor?
public partial class App : Application
{
public App()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
}
In my case when I was deploying my app, I was deleting the lang resources folders (es, de, fr...). I don't think this is your problem, but I'm just dropping that and hopefully it'll help somebody.
try Thread.CurrentThread.CurrentCulture.ToString()
to see which culture info you are.
public partial class App : Application
{
public App()
{
Language.Strings.Culture = Thread.CurrentThread.CurrentCulture.ToString() switch
{
"zh-CN" => new System.Globalization.CultureInfo("zh-Hans"),
"zh-Hans" => new System.Globalization.CultureInfo("zh-Hans"),
_ => new System.Globalization.CultureInfo(""),
};
}
}
This work for me. The Language
is my localization project, "zh-Hans" is the resource tag.
make sure your target culture info like "zh-Hans", is also included in the build folder. there will be a folder called zh-hans/
with a ModernWpf.Controls.resources.dll
file in it.
@luojunyuan
Thanks.
I copied your code and hped that it will work but I get compile issue:
Is there alternative way to write this code?
I think ghost1372 has already told you what you need to do
Trace.WriteLine(Thread.CurrentThread.CurrentCulture.ToString()); // zh-CN
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
// or
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-TW");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-TW");
repo: https://github.com/luojunyuan/ModernWpfControlLocalization
@luojunyuan It worked. My mistake was to use the nuget package https://github.com/Fody/Costura
If you install this package it will embed all resources in the executable file.
@ghost1372 Do you know if it is possible to make it work while the resources embedded in the executable file?
The other option is maybe to configure somehow to not embed the zh-Hant
folder
@gileli121 I suggest you use .NET Core, You can use SingleFile feature in Publish. However, if you are still using fody, there seems to be an option to ignore language resources https://github.com/Fody/Costura#ignoresatelliteassemblies
.NET Core requires the user to install more 130-150 MB I will never understand why I need this