Best Practice to Make a Multi Language Application in C#/Winforms

How to make multi-language app in Winforms?

Using Localizable and Language Property of Form

Form class have Localizable and Language Property. If you set Localizable property to true, you can add controls to form for default language and set properties for default language. Then you can select another languages and change properties for those languages. This way, value or localizable properties will store in separate resource files for different cultures.

Note: A property is considered as localizable if it's decorated with [Localizable(true)] attribute. For example BackColor property is not localizable, but Text property is localizable.

Localizing Messages and Images using Resx Resource Files

The project has a Rseources.Resx file under Properties folder which you can use for localizing images and messages. Also you can add .resx Resource files to project. For example you can create a Strings.resx file and add some string key and values to it, then copy it as strings.en.resx and strings.fa.resx and edit values for those languages. Then you can use those resource values, For example:

MessageBox.Show(Properties.Resources.AreYouSure);

Will show the value of AreYouSure from Resources.Resx file with the current UI culture language.

If a resource key not found for a culture or the specified culture not found for the resource file, value of the key in neutral culture of the Resx file will be used.

Change the language at Run-time

You can set the culture of a application to Persian using:

System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.GetCultureInfo("fa");

System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fa");

You should put the above code at start of your application or before showing a form.

More information

For more information and Example:

  • Globalizing Windows Forms
  • Walkthrough: Localizing Windows Forms
  • How to: Set the Culture and UI Culture for Windows Forms Globalization

Multilanguage Support In C#

Create Resources files for each language you want to give support for mentioned below.

alt text http://geekswithblogs.net/images/geekswithblogs_net/dotNETPlayground/resx.gif

Based on the language/currentculture of the user, read values from respective Language Resource file and display in label or MessageBox. Here's some sample code:

public static class Translate

{

public static string GetLanguage()

{

return HttpContext.Current.Request.UserLanguages[0];

}

public static string Message(string key)

{

ResourceManager resMan = null;

if (HttpContext.Current.Cache["resMan" + Global.GetLanguage()] == null)

{

resMan = Language.GetResourceManager(Global.GetLanguage());

if (resMan != null) HttpContext.Current.Cache["resMan" + Global.GetLanguage()] = resMan;

}

else

resMan = (ResourceManager)HttpContext.Current.Cache["resMan" + Global.GetLanguage()];

if (resMan == null) return key;

string originalKey = key;

key = Regex.Replace(key, "[ ./]", "_");

try

{

string value = resMan.GetString(key);

if (value != null) return value;

return originalKey;

}

catch (MissingManifestResourceException)

{

try

{

return HttpContext.GetGlobalResourceObject("en_au", key).ToString();

}

catch (MissingManifestResourceException mmre)

{

throw new System.IO.FileNotFoundException("Could not locate the en_au.resx resource file. This is the default language pack, and needs to exist within the Resources project.", mmre);

}

catch (NullReferenceException)

{

return originalKey;

}

}

catch (NullReferenceException)

{

return originalKey;

}

}

}

In asn asp.net application, you'd use it as following:

<span class="label">User:</span>

You now would put:

<span class="label"><%=Translate.Message("User") %>:</span>

Multilingual winforms application

If you want the users to edit the translations through your application while keeping things simple and quick, resource file is best. If you don't like it, the second best option is XML file.

Still, to answer you question on how to do it best with a text file, it is pretty straight forward: You just make sure that your unique identifier (int probably) are in order (validate before using the file). Then to search quickly, you use the technique of the halves.

You look for number X, so you go to the file's middle line. If id > x, to go to ¼ of the file, etc.

You cut in two until you get to the right line. This is the fastest know research method.

NOTE: Beware of the things that are external to the application but need translation: External file items, information contained in a database, etc.

How can I convert a C# .NET application to support multiple languages?

Yes! It's called resource (.resx) files. What you do is this:

  1. Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
  2. Create your program in one language, let's say English.
  3. Next, change all your forms to another language like so:
    1. Change the Language property of the form to the other language, let's say Spanish.
    2. Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
    3. Swap back and forth as needed during development.
  4. When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.

And voilà! You're done!

Some questions about multi language

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

Convert solution to multi language

if u want to use DB for localize then a can give advise:

Create 4 tables
1.ProjectKeyWords(KW_ID, LNGID, Value);
2.Pages (PageID, PageName);
3.Labels (LabelID, LabelName, KW_ID);
4.Application_Labels(PageID, LabelID);
5.Langs (ID, NAME);

sample:
ProjectKeyWords 1, 1, FirstName (In English)
2, 1, LastName (In English)
1, 2, Имя(In Russian)
2, 2, Фамилия(In Russian)
1, 1, Adı(In Azerbaijan)
2, 1, Soyadı(In Azerbaijan)
Pages --> 1, Form1
2, Form2

Labels --> 1, lblFirstName ,1
1, lblLastName ,2

Application_Labels --> 1,1
1,2
2,1
2,2

Langs --> 1, Eng
2, Rus
3, Az

And in Form_Load() event u can Create SQLQuery as "

SELECT L.LabelName, PKW.Value
FROM Labels L, Pages P, Application_Labels A_L, ProjectKeyWords PKW
WHERE P.NAME=
AND PKW.LNG_ID=

AND A_L.PageID=P.ID
AND A_L.LabelID=L.LabelID

"

create iteration and find All controls with L.LabelName with FindControl method and set PKW.Value to this label Text Property



Related Topics



Leave a reply



Submit