-
Notifications
You must be signed in to change notification settings - Fork 0
C Sharp
To define the application's default culture (In this case English), the NeutralResourcesLanguageAttribute AssemblyInfo must be set:
Hint: Always use English and not a specific English locale such as English (United States)
Example Project: WinForms
This Tutorial is based on Microsofts MSDN Tutorial Walkthrough: Localizing Windows Forms
Following resources are used for internationalzation:
- Project resources (non-form-based, dialog-boxes, error-messages)
- Form resources (Auto-generated)
Hint: For Forms-Property: always use either project OR form resources, not mixed
-
Set project as localizable
-
Open Form to translate
-
Change Property Language of Form to Japanese
-
Set Text of desired Element (e.g. Button) to translated new Text.
This part is also applicable to simple Console-applications
-
Add new Resource-file to the Project
Hint: This file is the fallback for the current default language (English), so the text in this file should be English.
-
Enter a new string with a default English text.
-
Repeat step 1 and 2 with a new file named WinFormStrings.ja.resx
-
To access the manually added resources (e.g. on a button-click) use the following code:
using System.Resources; ... // Access resource by Getter (Recommended as by http://stackoverflow.com/a/14503044/2003325) MessageBox.Show(WinFormStrings.errorInsuffMemory);
Hint: To force the program to start in a specific locale, uncomment one of these lines in Program.cs
//Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP"); //Japanese
//Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB"); //English (Default of this project)
Hint: Instead of creating a new default resources-file the existing default Resources.resx file under Properties may be used. (See this stackoverflow answer). For this, only a new Resources file for the additional language Japanese must be added: Resources.ja.resx. However, this approach results that ALL strings will be in one file and can't be distinguished. So separated Resources-files are strongly recommended!
Example Project: CodeDefaultEnglish
See WinForms -> Localizable Error-message and dialog-boxes (Project resources)
Usually the application fallback is the main assembly if the requested UI Culture cannot be found. However, if another culture should be used as a fallback, you may do so by defining a satellite assembly for fallback:
See Packaging and Deploying Resources in Desktop Apps -> Ultimate Fallback to Satellite Assembly
Set in Code or in AssemblyInfo.cs
[assembly:NeutralResourcesLanguage("en", UltimateResourceFallbackLocation.Satellite)]
There are various combinations of default language and satellite languages (The specfic VS-Project in bold):
- CodeDefaultEnglish - Default language is English, Satellite Japanese -> Fallback will be English (e.g. German user) Recommended
- CodeDefaultJapanese - Default language is Japanese, Satellite English -> Fallback will be Japanese (e.g. German user)
- CodeJapaneseDefaultSatelliteEn - Default language is Japanese, Satellite English AND Japanese -> Ultimate Fallback on English -> Fallback will be English
Potentially useful tool for translating resource files:
Zeta Resource Editor (https://www.zeta-resource-editor.com/index.html)
How to get a treeview of culture-specific resources
Unload the project Edit the corresponding csproj file Locate the tags of the resources and rewrite them using the DependentUpon syntax:
<EmbeddedResource Include="Strings.de.resx">
<SubType>Designer</SubType>
<DependentUpon>Strings.resx</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>