patterncsharpMinor
Automatic translation of forms
Viewed 0 times
automatictranslationforms
Problem
I want my application to support multiple UI-languages (aka i18n). To do so, I have built the static class below, to automatically translate the form and all its contents to the desired language. It looks into a resource file for the user's Culture, and replaces the .Text properties of the controls with the strings it finds there, or falls-back to the initial strings, which is English.
Usage is calling TranslateForm(this) from each form's constructor.
Two concerns about my code:
* The overloading of the "Translate" method. I didn't find any way to overcome this.
* The special handling of different controls, which seems unnecessary.
I'm not a professional programmer, so any correction / enhancement / fix to my code is more than welcomed!
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
namespace blahblah
{
static class TranslationHelper
{
static private ResourceManager rm = null;
static private CultureInfo default_ci = null;
static private CultureInfo lang_ci = null;
///
/// translate control into the specific lang, or leave it untranslated if no translation string found
///
///
///
static private void Translate(Control ctrl, string lang)
{
string str = TranslateString(ctrl.Name, lang);
if (str != null)
ctrl.Text = str;
}
static private void Translate(ToolStripMenuItem o, string lang)
{
string str = TranslateString(o.Name, lang);
if (str != null)
o.Text = str;
}
static private void Translate(ToolStripItem o, string lang)
{
string
Usage is calling TranslateForm(this) from each form's constructor.
Two concerns about my code:
* The overloading of the "Translate" method. I didn't find any way to overcome this.
* The special handling of different controls, which seems unnecessary.
I'm not a professional programmer, so any correction / enhancement / fix to my code is more than welcomed!
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
namespace blahblah
{
static class TranslationHelper
{
static private ResourceManager rm = null;
static private CultureInfo default_ci = null;
static private CultureInfo lang_ci = null;
///
/// translate control into the specific lang, or leave it untranslated if no translation string found
///
///
///
static private void Translate(Control ctrl, string lang)
{
string str = TranslateString(ctrl.Name, lang);
if (str != null)
ctrl.Text = str;
}
static private void Translate(ToolStripMenuItem o, string lang)
{
string str = TranslateString(o.Name, lang);
if (str != null)
o.Text = str;
}
static private void Translate(ToolStripItem o, string lang)
{
string
Solution
There are built in mechanisms for automatically switching an entire form to use text from culture-specific resource files and it is more powerful than just setting the text property (ToolTips for example). The only times you should need to manually load from the resource file are for things like message boxes, but unless you need your application to block while displaying an alert you might as well use balloon tips which are localizable as well. This is an easy place to start. It will also be more efficient as the controls themselves are localization aware so there's no need to loop through anything.
Context
StackExchange Code Review Q#721, answer score: 5
Revisions (0)
No revisions yet.