Attempted Localization Always Shows English on Windows Form App - c#

I am trying to set up a Windows Form login screen that uses the user's language to set either German or English for a class. I have set up my .resx files within the Properties of my project, but I cannot figure out why the form always shows in English.
I reviewed How to use localization in C# and How to make multi language app in winforms, which is where I found the information on setting up the Strings.resx and Strings.de.resx files, but the text when I set my UI Language to German always shows in English. On the LoginForm Properties, I have Localizable set to True.
As I want the app to load the screen with the correct language, I placed all my code in the Load Event:
private void LoginForm_Load(object sender, EventArgs e)
{
// Check system language
CultureInfo Language = CultureInfo.CurrentUICulture;
// Check if system language is set to German or English, and display
// login screen elements as appropriate
if (Language.Name.ToString() == "Deutsch")
{
LoginLabel.Text = Properties.Strings.LoginLabel;
UserLabel.Text = Properties.Strings.UserLabel;
PasswordLabel.Text = Properties.Strings.PasswordLabel;
LoginButton.Text = Properties.Strings.LoginButton;
ExitButton.Text = Properties.Strings.ExitButton;
}
}
I know I'm likely missing something simple, but I can't figure it out. If I need to, I can manually set the Text fields to the German version, but I'd rather have it separated in the .resx files if possible.

Changed Language to a string, and removed the if loop, and it's now appearing in German when set that way.

Related

Multilanguage WPF Application

I'm working multi-language (Arabic & English)
WPF application with resources in two different files (Resources.en-GB & Resources.ar-LY),
I declared a new method in the app.xaml
public void LanguageSwitcher(string _currentUICulture)
{
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(_currentUICulture);
}
In the MainWindow and after Initialize Component I put
((App)Application.Current).languageSwitcher("ar-LY");
this.FlowDirection =
CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ?
FlowDirection.RightToLeft :
FlowDirection.LeftToRight;
In the language switcher button, I call the method and pass the en-GB culture that I want
((App)Application.Current).languageSwitcher("en-GB");
The Problem: When I hit the language button to change UI to English, The culture changed and the window flow direction changed to LeftToRight BUT the text does not change, It seems that the Resources.en-GB does not load!
What I miss here?!
You need to reload every label component after changing the culture.
You can try to open new window after changing the culture and check that the created components are changed.

Inherit properties to a WPF Page element

This may be a very simple question, so my apologies.
My problem here is that I want to build a Multilingual WPF App using C#, but I don't know how to make my different Page elements inherit the same method which makes my MainWindow translate to different languages. The app is done, I'm just translating it to English (My native language is Spanish).
I'm using Resource files to translate it.
Code for the language translation:
private void Languages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This is the combobox in which you select the language to display the app in
Set_Language();
}
//This is the method to invoke the Resource Manager and all the stuff from the resource file.
private void Set_Language()
{
if (!boolInit)
{
strLanguage = "LeitProjekteV2._0.Languages." + ((ComboBoxItem)LanguageSel.SelectedItem).Name.ToString();
ResourceManager LocRm = new ResourceManager(strLanguage, typeof(MainWindow).Assembly);
//Menu buttons
lblMenu.Content = LocRm.GetString("strMainMenu"); //The names inside the "" are the names of the resource in the Resource file which, depending on the language selected(Spanish, English and German)
//Change the text of whatever I choose; in this case, a Label named 'lblMenu'
MapButt.Content = LocRm.GetString("strMapButt");
BuscButt.Content = LocRm.GetString("strBusButt");
AgeButt.Content = LocRm.GetString("strAgeButt");
ComButt.Content = LocRm.GetString("strComButt");
InfButt.Content = LocRm.GetString("strInfButt");
LoginButt.Header = LocRm.GetString("strLoginButt");
RegisterButt.Header = LocRm.GetString("strRegisterButt");
ContacButt.Header = LocRm.GetString("strContacButt");
MasButt.Header = LocRm.GetString("strMoreButt");
//Here go the names of everything the Pages contain that I want to translate, just like above
//Have no idea how to inherit this method to all the pages
}
}
Now, I have several pages embedded in the same MainWindow.xaml, so that you click the button "Map", a Frame changes it's content to a Page named Map.xaml, and so on for other buttons.
But how do I make those Pages also translate?
Since the Set_Language() method takes the string value of the Combobox in order to select the correct Resource File, I don't want to create one combobox for every Page that I have, albeit that would eliminate my problem.
Any help? Sorry for the horrible way of asking, I'm still getting the hint here.
Thanks.
Use the below:
var wnd = Window.GetWindow(this); //get current window
Cast it to your window class and expose your language as a public property
Use your page to get the property by finding the current window
You may create a parent page class that do the above, and inheriting it for all your pages so you dont repeat code
I can see the difficulty you are facing is that you can't find a way to share the combobox across the main window and the pages hosted in the frame.
You can set a global variable that is accessible from the whole application, a good place is in application settings. Then when you make a selection with the Combobox, you just update the selected value to that variable.
Then call each page's Set_Language() method when they are being loaded into the Frame. In the Set_Language() method of each page, you can query what is been set to the variable stored in the application settings.
If you want to quick solution, create a static class to hold the selected language is also OK.
static class UserSelections
{
public static string Language { get; set; }
}

Localisation via resx in winforms

There are many questions about it but still haven't read a solution for me
I have all my resources in a seperate project and this project has a Resource folder with 3 languages in it.
In any control I have in the constructor a call to InitializeLanguage()
where I set the right text for the right language like this:
public void InitializeLanguage()
{
bOK.Text = Lastenboek.Language.Ok;
cbSM.Text = Lastenboek.Language.SamenvattendeMeetstaat;
cbDM.Text = Lastenboek.Language.DetailMeetstaat;
cbDMHeadingStyles.Text = Lastenboek.Language.KopStijlen;
cbExportArtikelsZonderMeetstaat.Text = Lastenboek.Language.ArtikelsZonderMeetstaat;
cbSMHeadingStyles.Text = Lastenboek.Language.KopStijlen;
cbRamingsPrijs.Text = Lastenboek.Language.Ramingsprijs;
cbBeknopteOmschrijving.Text = Lastenboek.Language.BeknopteOmschrijving;
cbFaseSM.Text = Lastenboek.Language.GesplitstOpFase;
groupBox3.Text = Lastenboek.Language.Filter;
label2.Text = Lastenboek.Language.Tot;
label1.Text = Lastenboek.Language.DatumVan;
gbPrijzen.Text = Lastenboek.Language.Prijzen;
label3.Text = Lastenboek.Language.Account;
cbTotaalSM.Text = Lastenboek.Language.Totaal;
groupBox4.Text = Lastenboek.Language.Template;
label4.Text = Lastenboek.Language.Layout;
Text = Lastenboek.Language.ExporteerMeetstaat;
}
Everything works fine but now sometimes the label is too small for the text in other languages. Is there a good way to change the language at designtime and change the dimensions for different languages?
Yes. And you don't even need to programmatically set localized texts/images and other values.
Just set Localizable property on your form to true and then you can switch languages at design time and edit everything for that language at design time.
When you start the application the form will use the Windows language. If you want to change it programmatically just set Thread.CurrentThread.CurrentUICulture property. You might want to repoen your form to apply the language or call InitializeComponent() explicitly.
The problem is that you have dynamic texts for controls, but you want to have "hardcode" fixed control sizes for every language.
with this approach maintaining can be complicated. Every change in translation should be tested in UI - is it fit or not in the control.
Another approach will be support "AutoSize" for controls. Winforms have TableLayoutPanel control, which can be useful for controls with dynamic texts.

Setting spellcheck language for textbox WITHOUT changing keyboard language possible?

I am using C# & .Net 4.0 and want to set the a differnet spellcheck language for a textbox without changing the keyboard layout.
I found out by reading great postings here that it is possible to change InputLanguage.CurrentInputLanguage for changing the SpellCheck language of my textbox. But this has the sideeffect, that the keyboard layout/language was changed too. But I need always an EN SpellCheck for my textbox without crashing the users keyboard settings. Is there any way for this case?
Thanks in Advance for every hint that can help me to solve this case.
I have found out, how to solve it.
You have to know, that I use the WPF TextBox in a Winforms project. Here is a sample code:
System.Windows.Forms.Integration.ElementHost elementHost1 = new System.Windows.Forms.Integration.ElementHost();
System.Windows.Controls.TextBox textBox = new System.Windows.Controls.TextBox();
textBox.Language = System.Windows.Markup.XmlLanguage.GetLanguage("en-GB");
textBox.SpellCheck.IsEnabled = true;
elementHost1.Child = textBox;
Setting the language using textBox.Language didn't work for me.
There seems to be an edge case where spell checking is completely disabled if the user's presently selected input language doesn't have the spelling dictionary installed for that language (i.e. the feature-on-demand language pack is missing for basic typing) - see here https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/features-on-demand-language-fod
Take the following example:
• the user's current selected language/keyboard is fr-FR
• the user doesn't have the "basic typing" feature on demand installed for fr-Fr (no dictionary is therefore available)
• the user does have en-GB installed as well as "basic typing", so the dictionary is available for en-GB.
I wanted to force my textbox to spell check in en-GB, even though the user's keyboard was set to fr-FR. (The reason for this is I'm actually piggy-backing off the WPF spell check feature to spell check an underlying document).
However, just setting the Language attribute in the textbox wasn't achieving that. i.e. even when specifying en-GB for Language, the spell check wasn't available.
In the end, I had transiently to set the input language to the desired language, create the textbox, and then change it back to the user's existing input language. This is therefore transparent to the user (i.e. as far as the user is concerned, it doesn't disturb their current input language).
InputLanguage original_language = InputLanguage.CurrentInputLanguage;
System.Globalization.CultureInfo my_culture = new System.Globalization.CultureInfo("en-GB");
InputLanguage c = InputLanguage.FromCulture(my_culture);
InputLanguage.CurrentInputLanguage = c;
System.Windows.Controls.TextBox testBox = new System.Windows.Controls.TextBox();
testBox.SpellCheck.IsEnabled = true;
elementHost1.Child = testBox;
InputLanguage.CurrentInputLanguage = original_language;
Note that, once the textbox has been created and bound to the elementHost, it doesn't matter if you change the input language back - the language of that textBox is permanently set to what it was when it was created.
Therefore, in the above example, the user would end up with the spell check working in British English, but still have his or her keyboard set to French.
In practice, there's extra code to the above to check that the language you're setting (en-GB in this case) is actually installed.

Globalizing application dynamically

In My application I have 2 Resource dictionaries (Spanish and English) that contains all strings of the application. In the main window I have a combobox from where the user can select their desired language. What I wanted to do is, when the user clicks on a specified language it should change the language of the entire application, not just the current window. I have searched here for a simple solution but couldn't find any. Can anyone suggest a way to achieve this?
Currently I use this way to change the current form's language
on the combobox selection changed event
private void Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ResourceDictionary dictionary = new ResourceDictionary();
string lang = e.AddedItems[0].ToString();
dictionary.Source = new Uri(#"/Resources/Languages/"+lang+".xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(dictionary);
}
PS: I don't want to close and re open the window to the changes to be applied
The WPF Localization Extension provides on-the-fly language changing without restarting the application.
Please note, that this approach uses *.resx-files for localization and not WPF specific ResourceDictionaries.
You can follow the common guides on the link above. Additionally, you can read through this guide.

Categories