MartinKuschnik / Goji

A framework to localize WPF applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change language by Culture

ghost1372 opened this issue · comments

can you provide a way for change the language without the need for {ApplicationLanguage}?

There is no need to use the ApplicationLanguage markup extension. You can set the Language property of the window by your own.

// ...
mainWindow.Language = new CultureInfo("en");
// ...

You can also use your own view model.

I do not want to use language at all
I prefer change by thread culture
Anyway, thanks for the reply

You can bind the Language of your windows/controls to your thread culture.
The issue with that is, that the framework need a mechanism to detect that the language has changed. The Thread class does not provide an event or any other mechanism for that.

@MartinKuschnik Sorry, but the code you said does not work and Language only accepts XmlLanguage
I meant to have a property called CurrentCulture in LocalizationProperties.cs and change the language by changing it:

LocalizationProperties.CurrentCulture = new CultureInfo("en");

There is a library that does it, But I could not implement what it did on Goji.

https://github.com/itabaev/wpflocalization/blob/2bbce8fc138b93be43d9ffdb43b76fcecd62ac92/WpfLocalization/Localization/LocalizationManager.cs#L24-L35

Can you help and add this feature as well?

Can you please explain your whole use case and why you don't like to use the ApplicationLanguage markup extension?

Just setting the thread culture does not work for all cases. Converters will still use the culture provided by the used control and not from the current thread.

I think using the ApplicationLanguage is not interesting and I do not want language change to be dependent on control.
If ApplicationLanguage is not used, the language will not change

1

2

I did not understand the problem, but maby the following code is solving the problem for you:

window.Language = XmlLanguage.GetLanguage("en");

// Or

window.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName);

@MartinKuschnik thank you this code work but There is still a problem
When the language is dependent on the control, I have to set the language separately for all windows
I just want to set the language once and change the whole program
3

The ApplicationLanguage markup extension is the solution for tat problem 😁

This is the way WPF is designed.

It only works once and will not be updated if we changing the language and open the window again
Can you provide change the language without the need for control?
3

Did you also bound the language to the ApplicationLanguage?

yes, I also have another question, why is Regex used here?

Regex regex = new Regex("^([^;]+);([^;]+)$");
MatchCollection matches = regex.Matches(valueParts[1]);
if (matches.Count != 1)
{
throw new FormatException(string.Format("'{0}' i not a valid {1} translation provider path. The path needs to have the following format: '{1}{2}Assembly;FullClassName'.", value as string, valueParts[0], typeDelimiter));
}
string assemblyName = matches[0].Groups[1].Value;
string path = matches[0].Groups[2].Value;

Is there a specific reason?
I changed it as follows and it works without any problems (And the provider will be easier "WpfApp;Folder.Resource")

var assemblyName = provider.Substring(0, provider.IndexOf(";"));
string path = provider.ToString().Replace(";", ".");
return new ResxLocalizationProvider(path, Assembly.Load(assemblyName));

I created a small demo app (GojiDemo.zip). It works for me like expected.

The regex should ensure that the format used for defining the translations is always valid.
Your code works in the case that the format is correct. If not, there is an exception in the designer that is not rely helpful.

Thanks, the problem was solved
The problem was that I used MainWindow.Language to change the language but you used Application.Current 😀
Thanks, most of my questions were answered, only 2 questions left😁
First, In my system, I have 2 resources Resource.resx and Resource.fa.resx I expect Resource.resx to be the default language but Resource.fa.resx is selected by default. Is it because the region is on Iran?
Untitled

And the second question is whether it is possible to change the language in the design mode?

I will try to answer these two questions:

  1. In my system, I have 2 resources Resource.resx and Resource.fa.resx I expect Resource.resx to be the default language but Resource.fa.resx is selected by default. Is it because the region is on Iran?

    Yes, windows will always start your application with your system culture. You can check that by having a look to the culture of the current thread directly after application start.

  2. And the second question is whether it is possible to change the language in the design mode?

    Yes, you can use d:XXX="yyy" to set properties specific to design time.
    You will use d:Language="{Your Design Time Language}" to set the language during design time.

<Window x:Class="GojiDemo.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:local="clr-namespace:GojiDemo"
       mc:Ignorable="d"
       d:Language="de"
       Language="{ApplicationLanguage}"
       LocalizationProperties.TranslationProvider="resx://GojiDemo;GojiDemo.Translations" >
   
</Window>

@MartinKuschnik excellent ❤ It seems to be more than 2 questions 😁 I create a new issue