2

Recently, I decided to add 4 languages to my application. I read about how to do it and I succeed.

But there are two problems/questions I would like to ask.

First question: Is there a better way to change the text of each control instead

private System.Resources.ResourceManager rm;

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
rm = new System.Resources.ResourceManager(typeof(MainForm));

and then for each control to write this line:

aboutToolStripMenuItem.Text = rm.GetString("aboutToolStripMenuItem.Text");
addTaskToolStripMenuItem.Text = rm.GetString("addTaskToolStripMenuItem.Text");
addTaskToolStripMenuItem1.Text = rm.GetString("addTaskToolStripMenuItem1.Text");
...

Second Question: lets say the text of label1 is "test" and in other language its "testest" then the size of the label will change, which is ok. but If I got label2 that his location is near label1, label1 might be on the top of label2. How can I move the label1 compare with label2 so no matter how long the text in label1 will be, label2's location will be relative to label1. I dont want to use calculations in the program, I want to know if there's other way like property in one of the controls.

EDIT: after long thinking, I decided to use XML for my multilanguage. this way I can let the people translate it and upload it for me plus I can use it on runtime instead or reload the programs.

About the relative pos of controls I will use FlowLayoutPanel or TableLayoutPanel I will check further which is better.

Ron
  • 3,975
  • 17
  • 80
  • 130
  • You do understand that you actually have to translate those words if you want them to be in French right? – Security Hound Feb 14 '11 at 19:35
  • 1
    Yes I know and I already did a new resx with french... I said that I succeed... – Ron Feb 14 '11 at 19:41
  • I wouldn't know what you said because your question is not clear. If you want to adjust the positon of TextBox B based on the position of TextBoxA make the location dependent upon the size of TextboxA ( adjust the X location of the control ). – Security Hound Feb 14 '11 at 19:48

3 Answers3

3

Satellite assemblies is what you are looking for.

Q1: In VS, set your form's Localizable property to true. Then select the language property accordingly and type your translations in the designer. That way, you simply need to set the thread culture at start-up and .NET will load the correct language for you. No need to add extra code.

Q2: Again, after you selected the language in the designer, just move the controls around: Their new location/size is part of the translation and will be handled automatically by .NET.

Community
  • 1
  • 1
Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
2

It's not a very good idea change the GUI to a different culture while it's running, it's better to say something like you need to restart the application to see changes.

Although if you need to do it, you need to reload all the resources from the new culture (more or less the same than the InitializeComponen does), not only the text, because the location, size and so on, may be changed too. Also you need to change the thread culture in order to the errors, message and the new controls have the correct culture too (to show them in the correct language too).

You can set your application culture with:

CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr");
Thread.CurrentThread.CurrentCulture = appCulture; 
Thread.CurrentThread.CurrentUICulture = appCulture;

You need an specific culture to use it on formating and parsing.

Borja
  • 2,188
  • 1
  • 18
  • 21
  • I dont need to change the GUI culture while its running. I didnt find how to do it with your way.. can u give me example / tutorial? – Ron Feb 14 '11 at 19:46
  • If you can set the culture on the init (before you create the forms) you can do it using the "Thread.CurrentThread.CurrentCulture = yourCulture" and "Thread.CurrentThread.CurrentUICulture = yourCulture". Also if you create new thread you need to set the culture to them too. If the thread culture is ok, all error message, forms and so on will be show with your specified culture. – Borja Feb 14 '11 at 19:54
  • I got form called "MainForm" and I did french resource for this form. on the constructor function > MainForm() I placed your 3 lines code (before the InitializeComponent();) and compiled the code. Nothing happened... – Ron Feb 14 '11 at 20:09
  • 1
    Do you have the fr neutral culture or a subculture? are your resx .fr.resx or fr-xx.resx? You need the fr resx, to use this specific culture. Could you check which resource is loading after fix the culture to the thread (on the InitializeComponent)? – Borja Feb 14 '11 at 22:15
  • neutral > MainForm.fr.resx MessageBox.Show(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()); sais fr-FR oO I want fr. not fr-FR... – Ron Feb 14 '11 at 22:26
  • fr-FR is a subculture of fr as you have the fr.resx should work (as there is not the fr-FR its parent culture, the fr culture, is loaded). You need to specify an specify culture because the fr culture doesn't have info about formating and parsing so you can't set it to the thread culture. – Borja Feb 14 '11 at 22:33
  • Borja, I changed to fr-FR, and changed the CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr"); to CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr-FR");, the currentculture is fr-FR but still the buttons and etc are in english, they dont use the resource I made... – Ron Feb 14 '11 at 23:26
1

In answer to your first question:

If you really want to follow that scheme maybe using Reflection or automatic code generation is an alternative for easier management. I usually write my own GetString method that takes a default english string as an argument (used if no resource can be loaded dynamically for the current language). But I am not sure if it is the best solution either...


In answer to your second question:

In Winforms use a TableLayoutPanel or FlowLayoutPanel or another layout component to relatively position the controls. It is possible to specify if the Label fits to its content (AutoSize) for example or if it shall Dock and if yes with what Alignment. There is nearly no use case that would require a tedious self management or computation.

Link: http://msdn.microsoft.com/en-us/library/z9w7ek2f(v=VS.100).aspx

jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • In this case I'll have to add controls (flowLayourPanles) and they act like table as I understand. Yes, It will solve my problem thank you. Edit: I read Serge - appTranslator answer and in my opinion his way is better so I will use his answer. (I didnt know the location of each control is part of the culture resource. – Ron Feb 14 '11 at 19:52
  • 2
    If you do not have many controls and make the translations yourself it might sound like a good option. In any other case (a lot of strings) or automatic string building (from content in your app) then a really dynamic approach is the only 100% good solution. I predict that you will hate shifting around controls after a while :-) – jdehaan Feb 14 '11 at 20:15