I have been searching about how to change the language of a Form that has the Localizable
attribute set to true.
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.110).aspx
This is to set the language of the form, but this needs to be set before we instantiate the form. This cannot be called after this event.
Searching for information, I have seen the following question: https://stackoverflow.com/a/11738932/3286975 but, as a comment said, I have controls inside of a TabControl and a MenuStrip, so they aren't affected.
I have tried to modify this, by getting all the controls of the Form without luck.
In this menu I call the following callback:
private void englishToolStripMenuItem_Click_1(object sender, EventArgs e)
{
string lang = (string) ((ToolStripMenuItem) sender).Tag;
base.Culture = CultureInfo.CreateSpecificCulture(lang);
}
private void spanishToolStripMenuItem_Click(object sender, EventArgs e)
{
string lang = (string) ((ToolStripMenuItem) sender).Tag;
base.Culture = CultureInfo.CreateSpecificCulture(lang);
}
I change the Culture by using the Tag.
When I click it nothing happens. Also, I have modified a little bit the ApplyResources method from the mentioned answer.
private void ApplyResources(Control parent, CultureInfo culture)
{
this.resManager.ApplyResources(parent, parent.Name, culture);
foreach (Control ctl in parent.IterateAllChildren())
{
//this.ApplyResources(ctl, culture);
this.resManager.ApplyResources(ctl, ctl.Name, culture);
}
}
Where IterateAllChildren is the following: https://stackoverflow.com/a/16725020/3286975
Also, I tried with (System.LINQ): Controls.OfType<Label>()
(because I have one Label to test this) without luck...
But when I select the Spanish language, no text is changed.
So maybe, I'm failling with the childrens. Or maybe by calling the method CreateCulture
, I don't know.
Thanks in advance!
EDIT:
I have tested to get the Resource Manager of my form by the Culture Info and it returns the default one everytime:
ResourceSet resourceSet = new ResourceManager(typeof(frmCredentials)).GetResourceSet(new CultureInfo(lang), true, true);
foreach (DictionaryEntry entry in resourceSet)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value; //resourceSet.GetString(resourceKey);
if (resource.GetType().Equals(typeof(string)))
Console.WriteLine("Key: {0}
Value: {1}
", resourceKey, (string) resource);
}
Where new CultureInfo(lang)
, I haved tested also: new CultureInfo("es")
& Thread.CurrentThread.CurrentCulture
(CurrentUICulture) without luck. Is like it never exists or is replaced, but in my design and file explorer I can see the files.
EDIT2:
Maybe is because I'm using ILMerge to merge all dlls in a unique one. I'm reviewing this: Single-assembly multi-language Windows Forms deployment (ILMerge and satellite assemblies / localization) - possible?
Reply to EDIT2:
Yep, deleting ILMerge the problem is solved, and the first solution I gave resolves this. But for some reason, Spanish language is taken as the default language, and when I tried to get the resourceset from it, it didn't return me nothing.
Aso, I have set the Localizable attribute to false, and it didn't created a default resx file with values. I don't know if this is a good practice.
I will try something new...
See Question&Answers more detail:
os