-
Notifications
You must be signed in to change notification settings - Fork 5
Localization
Localization sub module defines GetText inspired localization mechanism. Localization is done simply by explicitly casting string in default application language to L
struct and then by implicit casting back to string. For e.g.:
...
textBox.Text = (L)"Hell, World!";
...
These text for localization should be then registered inside translation dictionaries. When explicit to L
cast is executed, new instance of L
is created and using class Translate
localized text is found. By default, without any configuration, are texts are translated into itself. This is caused by default handler defined in Translate
class, this handler can overridden by calling Translate.SetHandler
. Function passed to the method takes two parameters: original text to be translated and assembly that invoked translation. This assembly is usable for assembly (local) specific translation that should be different from global dictionary.
In the project Neptuo.Localization
there is implementation of key-value dictionary readers and current user culture providers. Also in class TranslationAdapter
, there is method that fits the one expected by the Translate.SetHandler
method. This adapter takes culture provider and translation (dictionary) reader provider in its constructor and when translating text, reader best matching calling assembly and user culture is used for translation.
Thread based user culture provider, implemented in ThreadCultureProviderBase
and CurrentThreadCultureProvider
, uses extension method from Neptuo.Globalization._CultureInfoExtensions
that beside current used cutlure, returns also all parent cultures (until reaching invariant culture).
How are translations resolved? For e.g.: When assembly named 'Test' uses (L) for translating text and user has thread culture set to 'en-US', these translation dictionaries are searched:
- 'en-US' dictionary for assembly 'Test'.
- 'en-US' global dictionary.
- 'en' dictionary for assembly 'Test'.
- 'en' global dictionary.
This way, dictionaries can inherit from others and only differences between local and global translations or more-specific and less-specific cultures need to be done set inside dictionaries.