Skip to content

Localization

Volodymyr Podshyvalov edited this page Jan 27, 2020 · 19 revisions

How it works

The logic schema

Lable < GUIContent < RAM: Loaded ".lang." dictionaries < File system: Localization files

  • Label -a control that implements WpfHandler.UI.Controls.ILabel interface.
  • GUIContent - an instance of the WpfHandler.UI.GUIContent class that contains localization code forwarding to resource with localized lable content.
  • RAM: Loaded ".lang." dictionaries - loaded localization files for a certain culture.
  • File system: Localization files - collection of dictionaries for different cultures that contain some localized content.

Localization files

A xaml dictionaries stored into the \plugins\ directory that contains pairs localization key -> localized string.

File's name format: GROUP_NAME.lang.CULTURE_CODE.xaml

  • GROUP_NAME - sequence of any symbols till the .lang keyword. The files with the same group name will be overloaded during language changing to the files with the same group's name but the different CULTURE_CODE.

  • .lang - the keyword that mark the dictionary as a part of the localization system.

  • CULTURE_CODE - the name of the culture localized into that dictionary. Examples: en-US, ru-RU, etc.

Hierarchy example

\appFolder                                    | A folder where stored the application.
    \plugins                                  | The folder where must be plased all plugins resources.
        \vanila                               | The folder that will contain default dictionaries.
            - app.vanila.lang.en-US.xaml      | An english localization for the UI element used on the clear app.
        \plugin1                              | A folder that will contain the files relative to the first plugin.
            - plugin1.dll                     |
            - devidQ.plugin1.lang.en-US.xaml  | The localiztion of the plugin's UI elements for russian.
            - devidQ.plugin1.lang.ru-RU.xaml  | The localiztion of the plugin's UI elements for english.
            - devidQ.plugin1.lang.zh-CN.xaml  | The localiztion of the plugin's UI elements for chinese.
        \plugin2                              | Folder with resource of the second plugin.
            - plugin2.dll                     | A compiled plugin's dll.
            - plugin2.lang.en-US.xaml         | The localiztion of the plugin's UI elements for english.
            - plugin2.lang.zh-CN.xaml         | The localiztion of the plugin's UI elements for chinese.
         

Example

In that example we will create the button and add a two languages localization for it.

XAML

  1. Adding namspace to get access to WPFH controls.
xmlns:wpfh="clr-namespace:WpfHandler.UI.Controls;assembly=WpfHandler"
  1. Adding a control with implemented ILable interface to a Plane. Define the name to get access to the control from the code behind. In that example we will use the FlatButton control.
<wpfh:FlatButton Name="localizedButton"/>

The code behind

  1. Creating the GUI content.
  2. Adding the title that will displayed in case if localization key not found among the loaded.
  3. Adding the key that will be looking into the loaded dictionaries.
var content = new GUIContent(){ 
    DefaultTitle = "Native title", 
    TitleLocalizationResourseKey = "localizedLableCustomKey" };
  1. Binding the content to the button's lable.
content.BindToLable(localizedButton);  
  1. Requesting english "en-US" localization as prior and russian "ru-RU" localization as secondary in case if english one not found. (You can use any culture code available for the CultureInfo)
LocalizationHandler.LoadDictionaries(        
    new CultureInfo("en-US"),
    new CultureInfo("ru-RU")); 

Localiztion files

The files stored by dyrecory: APP_FOLDER/plugins/

English

The full file's name: example.lang.en-US.xaml.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:v="clr-namespace:System;assembly=mscorlib">

    <v:String x:Key="localizedLableCustomKey">Loaded title</v:String>
</ResourceDictionary>

Russian

The full file's name: example.lang.ru-RU.xaml.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:v="clr-namespace:System;assembly=mscorlib">

    <v:String x:Key="localizedLableCustomKey">Заголовок</v:String>
</ResourceDictionary>

Links

Projects

Relative pages