csutils.Globalisation.TranslationManager is the core class around the Globalisation-System.

It is organized by Cultures and Channels. Every Culture can have multiple Channels with different translations. It is meant to provide translations for different Use Cases, like Menus, Tooltips, Textual Help, Plugins, etc. The class has some important Members:

  • DefaultCulture - the Fallback Language if a definition can not be found, default is en-US
  • CurrentCulture - this Language is currently used
  • LanguageChanged - Event that occours if CurrentCulture has been modified
  • Manager.Add() - Adds a file (or stream) with the translations to the manager
  • Translate() - translates a text into another language


There is a built in TranslationProvider-System that enables to grab translation from different Sources. Currently only XML is supported, (but INI will follow). The name of the root element does not matter. Only the direct Children will be used. The element name is the dictionary key, the content (or the value-Attribute) will be the translation. The following picture shows how this can look like:

TranslationManager.png

Using it

TranslationManager will use the filename convention [<Channel>.]<Culture>.<Extension> by default, but Channelname and Culture can also adjusted manually.

When translating, it will be checked if there is a translation of a key in the current language and channel. If not the DefaultCulture will be checked (the same channel). If even there is no translation the original string gets returned. Translation is also available via Extension-Methods


  • TranslationManager.CurrentCulture = new CultureInfo("de-DE"); - use german
  • TranslationManager.Add("en-US.xaml"); - Default-Channel, English Translations
  • TranslationManager.Add("Tooltip.de-DE.xaml","etc"); - "etc"-Channel, German Translations (manually overriden)
  • TranslationManager.Add(stream,".xml",new CultureInfo("es-ES")); - Default-Channel, Spanish Translations
  • "newfile".Translate(); - returns "Neue Datei"
  • "checkupdates".Translate(); - returns "Check for Updates" because there is no german version of it and english is Default
  • "foobar".Translate(); - returns "foobar", because there is no translation, neither Current nor Default
  • TranslationManager.Translate("foobar"); - the same in green
  • "newfile".Translate("Tooltip"); - returns "Erstellt eine neue Datei"
  • "newfile".Translate("Tooltip", TranslationManager.DefaultCulture); - returns "Creates a new File"


LanguageChanged

Whenever CurrentCulture changes this Event will be fired. You can use it to adapt the translations at runtime in your WinForms-Application.

In WPF this is much easier due to the Translation-MarkupExtension. Just use

<StackPanel>
    <TextBlock Text="{Translate newfile}" />
    <TextBlock Text="{Translate newfile, Channel=Tooltip}" />
    <TextBlock Text="{Translate newfile, Channel=Tooltip, Language=en-US}" />
</StackPanel>


and the UI will magically update the Text whenever it is needed.

Benefits of this solution

This approach allows to adjust the translation of a component (or add more) without recompile it. This enables community-driven adjustments and improvements on existing components





Last edited Apr 3, 2015 at 12:22 AM by recursivebytes, version 4