Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Child pages (Children Display)

Setting up the LanguageService

Setting cultures

By default the LanguageService will use the current UI culture to retrieve the right language values. These can easily be customized:

Code Block
var dependencyResolver = this.GetDependencyResolver();
var languageService = dependencyResolver.Resolve<ILanguageService>();
 
languageService.PreferredCulture = new CultureInfo("nl-NL");
languageService.FallbackCulture = new CultureInfo("en-US");

Registering custom language sources

In order to customize the language sources, custom language sources can be registered via the RegisterLanguageSource method.

...

The LanguageService will now automatically query these sources for the translations.

Using the LanguageService

To use the LanguageService, retrieve it via the DependencyResolver (or let it be injected) and use the provided methods. The example below retrieves the WarningTitle resource string in the PreferredCulture. If the resource cannot be found in the PreferredCulture, it will be retrieved for the FallbackCulture. If that cannot be found, it will return null.

Code Block
var dependencyResolver = this.GetDependencyResolver();
var languageService = dependencyResolver.Resolve<ILanguageService>();

var warningTitle = languageService.GetString("WarningTitle");

Using the LanguageService in XAML

To use the LanguageService in XAML, Catel provides the markup extensions.

Using the LanguageBinding in WPF and Silverlight

To use the LanguageBinding markup extension in WPF and Silverlight, use the following code:

Code Block
<TextBlock Text="{LanguageBinding WarningTitle}" />

Using the LanguageBinding in Windows Phone

Since Windows Phone does not support markup extensions, a custom MarkupExtension implementation is used in Catel. This requires a little difference in the usage of the markup extension:

Code Block
<TextBlock Text="{markup:LanguageBinding ResourceName=WarningTitle}" />

Implementing custom LanguageService (from database)

Implementing a custom LanguageService consists of several steps which are described below.

Note

Note that this implementation queries the database for each translation. It is best to read all translations into memory at once to improve performance

Creating a custom ILanguageSource implementation

First of all, we need to implement a customized language source to allow the custom service to know what source to read for translations:

Code Block
public class DbLanguageSource : ILanguageSource
{
    public DbLanguageSource(string connectionString)
    {
        Argument.IsNotNullOrWhitespace(() => connectionString);

        ConnectionString = connectionString;
    }

    public string ConnectionString { get; private set; }

    public string GetSource()
    {
        return ConnectionString;
    }
}

Creating a custom DbLanguageService

Below is a custom implementation of the LanguageService. Note that we only have to derive a single method to fully customize the implementation:

Code Block
public class DbLanguageService : LanguageService
{
    protected override string GetString(ILanguageSource languageSource, string resourceName, CultureInfo cultureInfo)
    {
        var connectionString = languageSource.GetSource();
        using (var dbConnection = new SqlConnection(connectionString))
        {
            dbConnection.Open();

            var sqlCommand = dbConnection.CreateCommand();
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.CommandText = @"SELECT [Name] FROM [Translations] WHERE [ResourceName] = @ResourceName AND [CultureName] = @CultureName";
            sqlCommand.Parameters.Add(new SqlParameter("ResourceName", resourceName));
            sqlCommand.Parameters.Add(new SqlParameter("CultureName", cultureInfo.ThreeLetterISOLanguageName));

            var translation = sqlCommand.ExecuteScalar() as string;
            if (!string.IsNullOrWhiteSpace(translation))
            {
                return translation;
            }
        }

        // Resource not found, fall back to base if you like, or simply return null
        return base.GetString(languageSource, resourceName, cultureInfo);
    }
}

Enabling the custom DbLanguageService

To enable the custom DbLanguageService, it must be registered in the ServiceLocator:

...