Click or drag to resize
Localizer Class
Brings all functionality for an easy language change and translation of the applications.
Inheritance Hierarchy
SystemObject
  DW.SharpToolsLocalizer

Namespace: DW.SharpTools
Assembly: DW.SharpTools (in DW.SharpTools.dll) Version: 2.4.0.0 (2.4.0.0)
Syntax
C#
public static class Localizer

The Localizer type exposes the following members.

Methods
  NameDescription
Public methodStatic memberFormat(String, IDictionaryString, Object)
Formats the passed string by replacing the key string by the value string in the passed dictionary.
Public methodStatic memberFormat(String, IEnumerable)
Formats the passed string by replacing first string by the second and the third by the fourth and so on always alternately.
Public methodStatic memberFormat(String, Object)
Formats the passed string by replacing first object as string by the second and the third by the fourth and so on always alternately.
Public methodStatic memberInitializeResources
Initializes the passed dictionaries for the first time.
Public methodStatic memberLoad(String)
Loads a string by its resource key from the current known application resources.
Public methodStatic memberLoad(String, Object)
Loads a string by its resource key from the current known application resources and executes a C# format on it.
Public methodStatic memberLoadFormat(String, IDictionaryString, Object)
Loads a string from the resources and starts a named format by replacing the key string by the value string in the passed dictionary.
Public methodStatic memberLoadFormat(String, IEnumerable)
Loads a string from the resources and starts a named format by replacing first string by the second and the third by the fourth and so on always alternately.
Public methodStatic memberLoadFormat(String, Object)
Loads a string from the resources and starts a named format by replacing first object as a string by the second and the third by the fourth and so on always alternately.
Public methodStatic memberRefreshResources(CollectionResourceDictionary)
Refreshes the resources in the given collection by using the known resources registered for the current language. See RegisterCultureResources(CultureInfo, Uri).
Public methodStatic memberRefreshResources(CollectionResourceDictionary, CultureInfo, CultureInfo)
Refreshes the resources in the given collection by removing known resources of the old culture and adding new resources known for the new culture. See RegisterCultureResources(CultureInfo, Uri) how resources became known for a culture.
Public methodStatic memberRegisterCultureResources
Registers a collection of resources by their uri for a specific language.
Public methodStatic memberSetCulture
Changes the culture of the current thread.
Top
Examples
Possibility 1. With satellite assemblies built beside the excutable like .\en-US\ApplicationName.resources.dll.
C#
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;

namespace DW.SharpTools.SatelliteDemo
{
    public partial class App : Application
    {
        public void SwitchLanguage(CultureInfo newCulture)
        {
            // Set new culture
            var result = Localizer.SetCulture(newCulture);
            if (result.Exception != null)
                Debug.WriteLine(result.Exception);

            // Refresh the resources
            Localizer.RefreshResources(Resources.MergedDictionaries);
        }
    }
}
Possibility 2. With build in translated resources. The executable contains all resources, the original and all translations like Strings.xaml and Strings_en-US.xaml.
C#
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;

namespace DW.SharpTools.SimpleDemo
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            // Register resource dictionaries for each culture
            Localizer.RegisterCultureResources(new CultureInfo("de-DE"),
                                               new Uri("pack://application:,,,/Resources/Strings_de-DE.xaml", UriKind.Absolute));
            Localizer.RegisterCultureResources(new CultureInfo("en-US"),
                                               new Uri("pack://application:,,,/Resources/Strings_en-US.xaml", UriKind.Absolute));

            // First initializing of the resources for the CurrentCulture
            Localizer.InitializeResources(Resources.MergedDictionaries);

            base.OnStartup(e);
        }

        public void SwitchLanguage(CultureInfo newCulture)
        {
            // Set new culture
            var result = Localizer.SetCulture(newCulture);
            if (result.Exception != null)
                Debug.WriteLine(result.Exception);

            // Refresh the resources by the registered (throws the old and adds the new)
            Localizer.RefreshResources(Resources.MergedDictionaries, result.OldCulture, newCulture);
        }
    }
}
XAML
<!-- Strings.xaml and Strings_en-US and other looks like this one -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:System="clr-namespace:System;assembly=mscorlib">

    <System:String x:Key="Application_Name">Localization Demo</System:String>
    <System:String x:Key="Hello_World">Hello World</System:String>
    <System:String x:Key="Switch_Language">Switch language to:</System:String>
    <System:String x:Key="English">English</System:String>
    <System:String x:Key="German">German</System:String>
    <System:String x:Key="Language_Changed_Ok">The language is switched successfully</System:String>

</ResourceDictionary>
XAML
<Application x:Class="DW.SharpTools.SimpleDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/DW.SharpTools.SimpleDemo;component/Resources/Strings.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
See Also