Marko Apfel - Afghanistan/Belgium/Germany

Management, Architecture, Programming, QA, Coach, GIS, EAI

  Home  |   Contact  |   Syndication    |   Login
  187 Posts | 2 Stories | 201 Comments | 4 Trackbacks

News



Twitter | LinkedIn | Xing

Archives

Post Categories

BizTalk

C#

Enterprise Library

SAP

SQL Server

Technologie

The .NET-Frameworks offers a good support for application which needs to be localizable.
For each of my C#-projects i create three ressources under the properties-folder.

  • Images.resx
    contains images for the application
    (mostly not needed to be localized, only for simple resource-access)

  • Messages.resx
    contains strings of messages for users
    (such as contents of MessageBoxes)

  • Resources.resx (normally created from VS by default)
    contains GUI-relevant stuff
    (such as label- and button-texts)

For this example we use a simple message text in Messages.resx. The name of the localizable message text is MsgSampleText.


In the code this text can easily accessed:

   1: string messageText = Messages.MsgSampleText;
   2: MessageBox.Show(messageText);

To organanize resources for additional languages a new folder Additional Resources is used. In this Folder i create the resources files for other languages (in this sample we use en-GB- and de-DE-language support). In the filename i encode the purpose. So we have now

  • Messages.de-DE.resx
  • Messages.en-GB.resx

Both of these resources should not be an embedded resource. So this must be changed to None

 

For each resource a so-called satellite assembly must be build. For that two steps we need:

  • compile the resource with ResGen.exe
  • link this resourse in the satellite assembly with AL.exe

For efficiency this is done by a post-build which calls a batch named CreateSatelliteAssemblies.bat under the folder Additional Resources.

   1: ECHO PARAMETER1=%1
   2: ECHO PARAMETER2=%2
   3:  
   4: REM en-GB
   5: CD %2
   6: IF NOT EXIST en-GB mkdir en-GB
   7: CD en-GB
   8:  
   9: "%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\RESGEN.EXE" %1\Messages.en-GB.resx
  10: "%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\AL.EXE" /t:lib /embed:%1\Messages.en-GB.resources,EsriDe.Samples.Localization.Properties.Messages.en-GB.resources /culture:en-GB /out:EsriDe.Samples.Localization.resources.dll
  11:  
  12: REM de-DE
  13: CD %2
  14: IF NOT EXIST de-DE mkdir de-DE
  15: CD de-DE
  16:  
  17: "%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\RESGEN.EXE" %1\Messages.de-DE.resx
  18: "%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin\AL.EXE" /t:lib /embed:%1\Messages.de-DE.resources,EsriDe.Samples.Localization.Properties.Messages.de-DE.resources /culture:de-DE /out:EsriDe.Samples.Localization.resources.dll

And the post build for calling the batch and injecting the parameters %1 and %2 is:

   1: "$(ProjectDir)Additional Resources\CreateSatelliteAssemblies.bat" "$(ProjectDir)Additional Resources" "$(TargetDir)"

After compiling the application there should the following folder-structure which contains the satellite assemblies

 

 

Henceforth the ressource-manager use the satellite assembly which fits to the language environment. And if no know language environement is used the default language included in the main assembly is used.

posted on Saturday, November 1, 2008 10:11 AM

Feedback

# re: Multilanguage with C# 2/25/2009 5:39 AM Danny
Very nice article, however I miss one important thing :

I want to change the language of my application during runtime. How do i do this? My application is in 3 languages, English (default), Dutch and Russian. I've set all the english values in the standard Messages.resx, this works perfectly. I put all the translated values in respectively Additional Resources/Messages.nl-NL.resx and Additional Resources/Messages.ru-RU.resx and created the batch file. After I build, I see the DLL's are created on the right place, so untill this point; everything works fine.

Now I have one problem, I have added a button to my application, when I click that button, the language of the application has to change. How do I do this?

# re: Multilanguage with C# 3/4/2009 7:57 AM Marko Apfel
you could change the language in your up simply by:
switch (comboBox1.Text)
{
case "neutral":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("");
break;
case "en-GB":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
break;
case "de-DE":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
break;
}

string messageText = Messages.MsgSampleText;
MessageBox.Show(messageText);

# re: Multilanguage with C# 3/9/2009 4:57 AM Galina
Hello,

I've done this example, and also has the dlls in the right places, however it desn't put the right string in the message box. I've even added the following line before getting the string: Messages.Culture = Thread.CurrentThread.CurrentUICulture;

but this influenced only the culture of the Messages, and did no influence on the MsgSampleTex.

Thanks

# re: Multilanguage with C# 3/11/2009 6:12 AM Marko Apfel
Hello Galina,

i write a internal documentation for our developers how multilanguage applications should be coded.

Maybe in future this documentation is a new post or article here. Today i can send you a draft and some development-tree extracts for that.

If you are interested in this, then please contact me over the contact site & send me your mail-addy.

Regards, Marko

# re: Multilanguage with C# 4/26/2009 7:20 PM Kuroro
i write a internal documentation for our developers how multilanguage applications should be coded.

Maybe in future this documentation is a new post or article here. Today i can send you a draft and some development-tree extracts for that.

If you are interested in this, then please contact me over the contact site & send me your mail-addy.

>>> can you share this documentation you mentioned to me please... i really need help regarding this...
thanks a lot

# re: Multilanguage with C# 5/9/2009 3:45 PM Gert-Jan van der Kamp
This sounds like a lot of work, why not just create a couple of textfiles called en_US.txt (and put them into a cab) and then suck them up in a Dictionary<string, string>?

That way you can also easily change the language at runtime by loading another languagefile. When you name the dictionary Messages, calling for a certain message would just be Messages["MessageKey"].

You could also store the messages in a table.

Shouldn't this stuff be a in refactoring tool or something? I'm surprised how poor the tool support is for this, sounds pretty easy.

Regards GJ

# re: Multilanguage with C# 6/19/2009 7:13 AM SArfu
well i will be thankful if somebody can guide me how do i compile that Batch file .
i get a lot of errors when i do that on command line passing the path of directiories .
i will be glad if somebody can instruct me.
Thanks
&
Regards
Sarfu

# re: Multilanguage with C# 7/1/2009 2:34 AM Marko Apfel
Hello together!

@Gert-Jan:
Replacing texts is not all what you muss do in the localization. Maybe pictures should replaced, then the text-length could affect the size and placing of GUI-stuff (Remember: for all forms a localizable variant could have own properties!) and so on. Next pro: the ressource-manager works without any assistance of a developer.
So its clear that you need in complex localization scenarios definitively this buildin technique.

@Sarfu:
I know that the building of the batch is not a trivial step. Mostly the schema of your folderstructure is the critical point. Have a look at the VS-Output folder and verify the steps there. In the VS-options you could define the level of verbosity.

Regards
Marko



# re: Multilanguage with C# 1/7/2011 12:28 PM Mark Aitken
Great article, has helped me no end. Important to note that I had to add
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

in order to get the UI to choose from the appropriate culture. Is that a WPF thing?

Mark.

# re: Multilanguage with C# 3/15/2012 6:05 PM auraham
two questions:

1. What is the purpose of using CreateSatelliteAssemblies.bat file?

2. You said: "Both of these resources should not be an embedded resource. So this must be changed to None"..why None?


# re: Multilanguage with C# 2/15/2013 12:57 AM Greg J.
Sattelite assemblis!
www.tbs-apps.com/lsacreator

# re: Multilanguage with C# 9/11/2017 1:31 PM Smith
Did you set all forms Localize properties to true, or this is another way to localize?

Post A Comment
Title:
Name:
Email:
Comment:
Verification: