Can you please tell me how to localize a WPF application?

Never before did you have to write multi-language applications. Localization of interface strings made for this article Localization of WPF applications on the fly .
But it is also necessary to translate the strings that the application gives in case of an error or as a hint to the user. For example, the user selects a file of the wrong format and receives a message depending on the selected language.

  • Localization is done in the same way, all the text from the code goes to the resx files, and in the code, instead of the text, the link to the corresponding resource is used. - Monk
  • @Monk give code examples, please. All I found was some kind of source code snippet. And the examples on msdn are generally quiet horror ... I also want that when using the resx-files, no additional libraries (ru-RU.dll, en-US.dll, etc.) are created. - badc0de32
  • Hurried about the same way, the link is quite specific article. Tomorrow, if I do not forget and no one writes, I will write it off. - Monk
  • @ badc0de32 and what do you add. libraries prevented? - Pavel Mayorov
  • @PavelMayorov, I want to have only one build. It is planned that the program will be transferred between colleagues and do not want to create dependencies. - badc0de32

1 answer 1

Normal localization in C # is represented in one single way - resx files.

For small projects, you can simply include resources, open the project properties and use:

Resources in project

This creates a resx file with default localization. To add one more - just create (Add -> New Item -> resx) one more file, adding a culture to the name ( Resources.{culture}.resx )

Project resx files

In the code, we simply write an appeal to the resource:

Console.WriteLine(Resources.Example);

To change the language of the application is forced enough to change CurrentUICulture .

Now about the additional assemblies. There are no good decisions, the simplicity of the solution described above is lost as soon as you try to take a step to the left / right. Less working solution described here (enSO) , but it is terribly inconvenient to use. As you can see, the ease of use of the form Console.WriteLine(Resources.Example); is lost and the treatment is done manually. This can be automated, but then it is better to look in the direction of templates instead of the standard generator (for a general idea, you can look at MSDN ).

Those. to do everything in one assembly, you have to write a replacement for the standard resource assembly scheme. Is it worth it - decide for yourself. If the options with templates are interesting - better study the question and ask questions separately, I didn’t see the ready solution, the copywriters in different organizations came across.

In addition, as advice about:

It is planned that the program will be transferred between colleagues and do not want to create dependencies.

There is a click-once, although it is very unfortunate in my opinion. But, no problems on the dependencies and a bunch of assemblies will not.