C Sharp

Luke31 edited this page Nov 11, 2016 · 4 revisions

Application's default culture

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)

AssemblyInfo Default Culture

WinForms

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

Localizable Forms (Form resources)

  1. Set project as localizable

    Set localizable

  2. Open Form to translate

    Form to translate

  3. Change Property Language of Form to Japanese

    Form in Japanese

  4. Set Text of desired Element (e.g. Button) to translated new Text.

    Form in Japanese with Japanese text

Localizable Error-message and dialog-boxes (Project resources)

This part is also applicable to simple Console-applications

  1. 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.

    New Project resources

  2. Enter a new string with a default English text.

    Japanese Text

  3. Repeat step 1 and 2 with a new file named WinFormStrings.ja.resx

    Japanese Text

  4. 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!

Code

Example Project: CodeDefaultEnglish

See WinForms -> Localizable Error-message and dialog-boxes (Project resources)

Fallback to Satellite Assembly

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

Translate resource files

Potentially useful tool for translating resource files:

Zeta Resource Editor (https://www.zeta-resource-editor.com/index.html)

Tree-View of resource-files with language

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>
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.