ModernWpf icon indicating copy to clipboard operation
ModernWpf copied to clipboard

How to change the language of the controls?

Open gileli121 opened this issue 3 years ago • 10 comments

Hi, I could not find anywhere on code/issues how I can change the display language.

For example, In my case I have these: image

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.

gileli121 avatar Mar 23 '21 04:03 gileli121

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");

ghost1372 avatar Mar 23 '21 07:03 ghost1372

Thanks but It doesn't work. I already have it in the code. I still see "On"/"Off"

gileli121 avatar Mar 27 '21 08:03 gileli121

@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");
        }
    }

ghost1372 avatar Mar 27 '21 08:03 ghost1372

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.

HadriMX avatar Mar 27 '21 19:03 HadriMX

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. image

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 avatar Apr 08 '21 21:04 luojunyuan

@luojunyuan Thanks. I copied your code and hped that it will work but I get compile issue: image

Is there alternative way to write this code?

gileli121 avatar Apr 10 '21 20:04 gileli121

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 avatar Apr 10 '21 21:04 luojunyuan

@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 avatar Apr 11 '21 05:04 gileli121

@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

ghost1372 avatar Apr 11 '21 07:04 ghost1372

.NET Core requires the user to install more 130-150 MB I will never understand why I need this

gileli121 avatar Apr 17 '21 14:04 gileli121