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.
C# how to change multi language in all windows form?
First you should make the ChangeLanguage
method to allow calling it on any control or form.
Here is a version which is recursive and which takes a Control
as a parameter.
Note: Forms
are Controls
, too.
So you can call this method to change all forms you have a handle of..
public ComponentResourceManager resources;
private void ChangeLanguage(Control ctl, string lang)
{
resources.ApplyResources(ctl, ctl.Name, new CultureInfo(lang));
foreach (Control c in ctl.Controls) ChangeLanguage(c, lang);
}
Next you need to keep a list of your open forms in some way. A List<Form>
is a good way.
Instead of calling the ChangeLanguage
function on only the current window you call a ChangeLanguageOnAllforms
function. So if you have maybe a List<Form>
called myFormsList
you can do:
void ChangeLanguageOnAllforms( string lang )
{
foreach (Form f in myFormsList)
{
if (f != null)
{
resources = new ComponentResourceManager(typeof(f));
ChangeLanguage(f, languageString);
}
}
}
You should also keep a public variable to hold the current language!
public string language = "en";
And you should also upon opening any new Form say form7
- add it to the list and
- call the ChangeLanguage( form7, language )
You should think about keeping the list of forms uptodate by removing closed forms from it.
If you make it a public property the closing form can remove itself from it, if it has a reference to your main form..
How can I convert a C# .NET application to support multiple languages?
Yes! It's called resource (.resx) files. What you do is this:
- Change the
Localizable
property of your localizable forms totrue
. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them. - Create your program in one language, let's say English.
- Next, change all your forms to another language like so:
- Change the
Language
property of the form to the other language, let's say Spanish. - Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
- Swap back and forth as needed during development.
- Change the
- 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!
Related Topics
File I/O with Streams - Best Memory Buffer Size
How to Kill a Thread Instantly in C#
Most Elegant Xml Serialization of Color Structure
How to Convert String to Double with Proper Cultureinfo
Reading Excel File Using Oledb Data Provider
How to Get Client Date and Time in ASP.NET
What Does "Datetime" Mean in C#
How to Stop Backgroundworker Correctly
Enabling C# 7 in a ASP.NET Application
Find Recursive Group Membership (Active Directory) Using C#
Datagridview Bound to a Dictionary
Exception from Hresult: 0X800A03Ec Error
Splitting a String/Number Every Nth Character/Number
Sqlite Database Locked Exception
Difference Between Parameters.Add(String, Object) and Parameters.Addwithvalue