How to use localization in C#

后端 未结 9 2476
栀梦
栀梦 2020-11-22 02:59

I just can\'t seem to get localization to work.

I have a class library. Now I want to create resx files in there, and return some values based on the threa

9条回答
  •  悲&欢浪女
    2020-11-22 03:24

    In general you put your translations in resource files, e.g. resources.resx.

    Each specific culture has a different name, e.g. resources.nl.resx, resources.fr.resx, resources.de.resx, …

    Now the most important part of a solution is to maintain your translations. In Visual Studio install the Microsoft MAT tool: Multilingual App Toolkit (MAT). Works with winforms, wpf, asp.net (core), uwp, …

    In general, e.g. for a WPF solution, in the WPF project

    • Install the Microsoft MAT extension for Visual Studio.
    • In the Solution Explorer, navigate to your Project > Properties > AssemblyInfo.cs
    • Add in AssemblyInfo.cs your default, neutral language (in my case English): [assembly: System.Resources.NeutralResourcesLanguage("en")]
    • Select your project in Solution Explorer and in Visual Studio, from the top menu, click "Tools" > "Multilingual App Toolkit" > "Enable Selection", to enable MAT for the project.
      • Now Right mouse click on the project in Solution Explorer, select "Multilingual App Toolkit" > "Add translation languages…" and select the language that you want to add translations for. e.g. Dutch.

    What you will see is that a new folder will be created, called "MultilingualResources" containing a ....nl.xlf file.

    The only thing you now have to do is:

    1. add your translation to your default resources.resx file (in my case English)
    2. Translate by clicking the .xlf file (NOT the .resx file) as the .xlf files will generate/update the .resx files.

    (the .xlf files should open with the "Multilingual Editor", if this is not the case, right mouse click on the .xlf file, select "Open With…" and select "Multilingual Editor".

    Have fun! now you can also see what has not been translated, export translations in xlf to external translation companies, import them again, recycle translations from other projects etc...

    More info:

    • Using the Multilingual App Toolkit 4.0: https://docs.microsoft.com/windows/uwp/design/globalizing/use-mat
    • Multilingual App Toolkit blog, visit: http://aka.ms/matblog
    • Multilingual App Toolkit User Voice feature voting site, visit: http://aka.ms/matvoice

提交回复
热议问题