Multi Language Projects

Also referred to in .net as Globalization and Localization

Globalization is how the application is produced so that it will load different resources depending on the users setup

Localization is the reverse of globalization (sort of) After the application has been globalized, then the process of preparing language specific resources is then called localization.

References

Google for Globalization and Localization gets loads of results, but most I found were pretty useless - mainly referring to ASP.NET and web development.

This was the best one I found

FIXME Please add some more references if you find any good ones

How it affects us

Hopefully not much. :-)

If we get into the habit of setting all our forms to Localizable and NEVER hard-code strings but always fetch them from the resource file then I hope that would be the bulk of our work done.

Each resource file is XML, so hopefully we would just need to share the resulting XML resource files with someone in Trimble who can then get them translated to the various languages.

There will probably be some issues to do with keeping resource files in sync and so on, but hopefullly Trimble will have something in place to help with this.

Forms

The form properties has a Localizable property which is set to false by default.

When this is set to true, the system will extract all the strings used on the form (labels, captions etc) to a resource file FormName.resx. When the Localizable property is first set to true, the Language property will be set to Default.

To add resource files for a different language, change the Language property to the one to be created.

Unfortunately each control text property must now be changed to the correct value in the new label.

New controls can only be added to the form when the language is set to Default

Strings

In the code, all hard-coded strings must be replaced by resource strings.

So this

sText="Hello World"

Must be replaced by this

Dim sBase As String = "ProjectName.ResourceName"
Dim rm As Resources.ResourceManager 
       = New System.Resources.ResourceManager(sBase,
         System.Reflection.Assembly.GetExecutingAssembly())
sText=rm.GetString("HelloWorld")

Obviously before this works a resource file has to be created containing the HelloWorld string resource. I think the best way to do this might be to have a single resource file containing the strings. This can then be duplicated for the various languages.

From the Solution Explorer right click and add a new resource file.

The name of this file is important. It will form the name for all the resource files shipped with the product. If it is called strings.resx then the localized versions will be strings.en-GB.resx for GB, strings.fr-FR.resx for France, and so on

Open up the resource file and add a string with the key “HelloWorld” and the value “Hello World!”. When the application runs, it will look in the resource files to find strings with the key “HelloWorld”. It will look in the language specific files first (eg strings.en-GB.resx), and default back to the base resource file strings.resx if it can't find anything.

The code snippet now looks like this (with the resource file named strings.resx)

Dim sBase As String = "ProjectName.strings"
Dim rm As Resources.ResourceManager 
       = New System.Resources.ResourceManager(sBase,
         System.Reflection.Assembly.GetExecutingAssembly())
sText=rm.GetString("HelloWorld")

Testing

It is possible to change the locale settings in order to test the code.

This must be done before the form is loaded. It needs to go in the FormName.designer.vb code. Modify the New() sub and place the code to change the language before the call to InitializeComponent.

This code snip just gets the locale from the command line, so run as “Myapplication.exe cy-GB” to run it in Welsh

    Public Sub New()
        Dim s() As String = System.Environment.GetCommandLineArgs
        If s.Length() > 1 Then
            Dim sLoc As String = s(s.Length() - 1)
            ' This call is required by the Windows Form Designer.
            ' Set the culture and UI culture before 
            ' the call to InitializeComponent.
            Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo(sLoc)
            Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(sLoc)
        End If
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
 
    End Sub
 
devnews/multi_language_projects.txt · Last modified: 2009/01/22 08:32 (external edit)
Recent changes RSS feed Creative Commons License Driven by DokuWiki