Some questions about multi language

661 views Asked by At

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.

3

There are 3 answers

2
jdehaan On BEST ANSWER

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

7
Borja On

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.

1
Serge Wautier On

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.