Question

My application is not to be localized yet but I have to print a report in different languages. So, i think i will create different resources for report text but how to set the particular resource for selected language.

like for Spanish language how PrintResource.es-ES.resx will be set.

do i have to use if..else..if

Was it helpful?

Solution

You should not have to do anything beyond just ensuring you're running with the right culture at the time you produce the report.

Here's an example:

  1. Create a new Console project
  2. Add a Resource file to it, call it Strings.resx
  3. Rename the "String1" key to "DisplayName", and give it a value, like "English"
  4. Add another resource file, call it Strings.nb-NO.resx
  5. Rename the "String1" key in the norwegian file to "DisplayName" as well, and give it a different value, like "Norwegian"

Then in the main Program.cs, paste in the following code:

using System;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(Strings.DisplayName);

            Thread.CurrentThread.CurrentUICulture =
                CultureInfo.GetCultureInfo("nb-NO");

            Console.WriteLine(Strings.DisplayName);
        }
    }
}

Execute this, and you should see "English" followed by "Norwegian" on the console.

As you can see from this example, as long as the right UI culture is set, the right resource file will be read.

The code that printed the english text and the code that printed the norwegian text is identical, but the underlying culture has changed, so it will use different resource files.

If you don't want to "soil" the rest of the application with spanish UI after producing the report, simply spin up a thread for the reporting code, and set the UI culture for that thread only to spanish. The resource subsystem of .NET should handle the rest.

Also make sure you're formatting numbers and dates using culture-aware method overloads.

This:

Console.WriteLine(string.Format(CultureInfo.CurrentUICulture, "{0}: {1}", Strings.DisplayName, 10.31));

Might produce different text from this:

Console.WriteLine(string.Format("{0}: {1}", Strings.DisplayName, 10.31));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top