Skip to content

Localization

Reflection Emit edited this page May 20, 2018 · 6 revisions

The Locale class is a singleton dictionary that can be loaded with different translations from various sources. The source itself is required to implement the ILocalizationSource interface. The Locale class is also capable of converting numeric values to a readable string representation.
long, int, ulong and uint are formatted using {0:#,#} format string.
double, float and decimal are formatted using {0:#,#.00} format string.
Enums are formatted to $"{value} - {name}".
Locale adds a char to the end of the string if it was unable to find the key in any of the sources. This char can be changed with the MissingLocalizationIndicator property.
Cauldron.Localization also comes with 2 pre-implemented localization-sources: JsonLocalizationSourceBase<T> and YamlLocalizationSourceBase<T>.

The convertion extensions (ToDouble, ToFloat, ToInteger, ...) are converted using the CurrentCulture in .NET Desktop and Windows.System.UserProfile.GlobalizationPreferences.Languages[0] in UWP by default. There are also a built-in en-US formatted versions of the convertion extensions (ToDoubleUS, ToFloatUS, ...).
You can change this globally by changing the culture of Locale.CultureInfo

Example of a source implementation for a JSON source:

    
    [Component(typeof(ILocalizationSource), FactoryCreationPolicy.Singleton, 1)]
    public sealed class JsonLocalizationSource : ILocalizationSource
    {
        private IEnumerable<LocalizationKeyValue> localizations;
        public JsonLocalizationSourceBase()
        {
            this.localizations = JsonConvert.DeserializeObject<List<LocalizationKeyValue>>(
                  Assemblies
                       .GetManifestResource("strings.json")
                       .TryEncode())
                  .Cast<LocalizationKeyValue>()
                  .ToArray();
        }

        public IEnumerable<LocalizationKeyValue> GetValues() => this.localizations;
    }