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.
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 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
The ApplicationLanguage markup extension is the solution for tat problem 😁
This is the way WPF is designed.
Did you also bound the language to the ApplicationLanguage?
yes, I also have another question, why is Regex used here?
Goji/Goji/TranslationProviderTypeConverter.cs
Lines 62 to 72 in 41ac11b
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?
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:
-
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.
-
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 used: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