Best practices for global resources in C #

When we want to make our application for all users (speak different languages), we need a global technology.
In C #, we use the ResourceManager as follows:

using System; using System.Reflection; using System.Resources; public class Example { public static void Main() { // Retrieve the resource. ResourceManager rm = new ResourceManager("ExampleResources" , typeof(Example).Assembly); string greeting = rm.GetString("Greeting"); Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine("{0} {1}!", greeting, name); } } // The example produces output similar to the following: // Enter your name: John // Hello John! 

An assembly has two or more language resources:
Assembly
| --Assembly.en-us.resx
| --Assembly.zh-cn.resx

Then we archive to modify the resource, changing thread cultinfo to use another resource.

If the application has many dll files (assembly).
I want to have one point (one resource file for one language) for the application,
Is there a good solution for my idea?

Before just changing the presentation (for example. Winform or UserControl) Language to implement different user interfaces for the corresponding language.

+6
source share
2 answers

Just create internationalization in C # using the method you described. But as the last step of the build process, you can run Fody.Costura .

It will take all the different DLLs and pack them into your application, so you only have one .exe file with everything included.

The advantage is that you can use the C # internationalization frameworks for their intended purpose without any hacking, but you still get one exe that you can provide to your clients.

0
source

I believe that the framework for internationalizing C # is very absent, so I usually make one assembly for resources and a link from other projects. The resource files that I generate from some tool (DB, excel, textfile) keep both the source data and the resource files under version control.

 MyApp.sln ResourceProject.csproj Resources.resx Resources.ru.resx Resources.de.resx Resource.cs Core.csproj UI.csproj 

A resource class can load all different assemblies

  namespace MyApp.Resources { public static class Resource { private static ResourceManager manager; static Resource() { manager = new ResourceManager("MyApp.Resources", Assembly.GetAssembly(typeof(Resource))); } public static string GetString(string key, string culture) { return GetString(key, new CultureInfo(culture)); } public static string GetString(string key, CultureInfo culture) { return manager.GetString(key, culture); } } } 

This simple class can be extended in various ways. In calling assemblies, you can have utility classes that call based on the current UI culture or thread culture, depending on the situation.

Note that this completely bypasses any of the built-in WinForms or WPF i18N methods.

For the GUI: you can make a utility that recursively translates whole forms. The search itself can / should be expanded with warnings about missing keys, fallback arguments, prefixes / namespaces if you have thousands of keys, etc.

0
source

All Articles