none
How to use external resource (string) files?

    Question

  • I am writing a Windows Forms application in C# 2.0. I'd like to make my application easily localizeable by using resource files. I've managed to use the ResourceManager to grab embedded resources, i.e.:

    d.Text = ScriptTease.Properties.Resources.document_Text;

    However, what I'd like to do is not embed the resource files, and then use cultureinfo to locate and use the appropriate resource. For example, I'd like to ship my application with the en-US.resx file intact. Then if someone in Germany wants to translate the app into German, then they should be able to just add a de-DE.resx file, and have the application dynamically use it based on their cultureinfo. In this case, the de-DE.resx file would not be embedded, obviously.

    I'm having a heck of a time finding an example of how to do this. I'd also like to make that the programs uses en-US.resx if the appropriate *.resx doesn't exist, and/or provide a "default value" to use if the resource key doesn't exist.

    Can anyone point me to an example of how to do this?

    Tuesday, August 08, 2006 12:46 AM

Answers

  • It's possible to read culture information from external file (without embedding them as part of the executable). For this, you have to follow the following steps.

    1. Open a DOS command window and type the following command in it (assuming that .NET has been installed on C:):

    SET PATH=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin

    2. Change your directory to C:\MyApp\resources using the same DOS console issueing a CD DOS command (you may also choose other directory)

    3. Create a text file (with key=value pairs of culture information within it), let's name it 'resource.en-US.txt' for Englinsh language. Assuming that you have done this uinder C:\MyApp\resource folder (after minimizing the DOS console)

    4. In the same DOS console, type the following command:

    RESGEN resource.en-US.txt resource.en-US.resources

    You may repeat the steps 3 and 4 to create additional resouce files for other cultures; for example for German, you may create a resource text file called 'resource.de-DE.txt and issue the follwing RESGEN command:

    RESGEN resource.de-DE.txt resource.de-DE.resources

    Now if you wish to debug your appliclation, put those .resource file under /bin/debug folder. Alternatively, you may also put them under /bin/release folder (or just /bin folder) under production environment.

    After you've done all, your application code to read specific culture info looks like the following:

    private void btnReadCulture_Click(object sender, System.EventArgs e)
    {
    ResourceManager objManager = ResourceManager.CreateFileBasedResourceManager("resource",
    Application.ExecutablePath + Path.DirectorySeparatorChar, null);

    // the following code will read German culture by providing
    // hard-coded culture info..
    //MessageBox.Show(objManager.GetString("com.myapp.button.submit", new CultureInfo("de-DE")));

    // the following code will read culture info from current thread's culture..
    MessageBox.Show(objManager.GetString("com.myapp.button.submit"));
    }


    Hope this may help.

    Cheers

    Tuesday, August 08, 2006 12:26 PM

All replies

  • It's possible to read culture information from external file (without embedding them as part of the executable). For this, you have to follow the following steps.

    1. Open a DOS command window and type the following command in it (assuming that .NET has been installed on C:):

    SET PATH=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin

    2. Change your directory to C:\MyApp\resources using the same DOS console issueing a CD DOS command (you may also choose other directory)

    3. Create a text file (with key=value pairs of culture information within it), let's name it 'resource.en-US.txt' for Englinsh language. Assuming that you have done this uinder C:\MyApp\resource folder (after minimizing the DOS console)

    4. In the same DOS console, type the following command:

    RESGEN resource.en-US.txt resource.en-US.resources

    You may repeat the steps 3 and 4 to create additional resouce files for other cultures; for example for German, you may create a resource text file called 'resource.de-DE.txt and issue the follwing RESGEN command:

    RESGEN resource.de-DE.txt resource.de-DE.resources

    Now if you wish to debug your appliclation, put those .resource file under /bin/debug folder. Alternatively, you may also put them under /bin/release folder (or just /bin folder) under production environment.

    After you've done all, your application code to read specific culture info looks like the following:

    private void btnReadCulture_Click(object sender, System.EventArgs e)
    {
    ResourceManager objManager = ResourceManager.CreateFileBasedResourceManager("resource",
    Application.ExecutablePath + Path.DirectorySeparatorChar, null);

    // the following code will read German culture by providing
    // hard-coded culture info..
    //MessageBox.Show(objManager.GetString("com.myapp.button.submit", new CultureInfo("de-DE")));

    // the following code will read culture info from current thread's culture..
    MessageBox.Show(objManager.GetString("com.myapp.button.submit"));
    }


    Hope this may help.

    Cheers

    Tuesday, August 08, 2006 12:26 PM
  • Mohammad,

    Thanks for that info, it was very helpful. It appears from what you wrote above that in order to use the external resources, I would still need to process those files to some extent (using ResGen), is that correct? So "Joe Average" (and end user, not a developer) could not simply download my program, write a de-DE.resx, toss it in the appropriate folder and have it work?

    I know that when I work with ASP.NET, it's a pretty simple process, and everything is a text file. I was hoping that in Windows forms, it could be just as simple.

    Thank you again for the time spent in answering my question, I really appreciate it.

    -Todd

    Tuesday, August 08, 2006 12:35 PM