Windows 8 Store App and Universal App Multilanguage support

To realize multilanguage app now in Window Store app, Windows Phone 8.1 and universal app the resources are changed.

This Microsoft post show how to use resource:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965326.aspx

as you can see It is necessary set the attribute x:Uid in the target control like this:

In resource file .resw add this key:
MainHub.Header

This key is composed by [Control Uid].[Property]

In design time the resource file will not be used so the text displayed is the property “Text” or “Header”; only at runtime the right resource text will be applyed.

But how can we use only one string/resource and share it with many controls and many property like TextBlock.Text , Button.Content, Hub.Header ecc??
The answer coming from Windows Phone 7.

Set in the resource file a key like “MyKey”

Create this class:

public class LocalizedStrings
{
    private static ResourceLoader _localizedResources = new ResourceLoader();

    public ResourceLoader LocalizedResources { get { return _localizedResources; } }

    private string GetVal(string key)
    {
        return LocalizedResources.GetString(key);
    }

    public string this[string key]
    {
        get
        {
            return GetVal(key);
        }
    }
}

Use it in every place you need.

By code:
new LocalizedStrings()[“MyKey”]

By Xaml:

Create an instance in app.xaml file:

<Application.Resources>
    <local:LocalizedStrings x:Key="LocalizedStrings" />
</Application.Resources>

Use it in your xaml page:

<HubSection Header="{Binding [MyKey], Source={StaticResource LocalizedStrings}}">
<Button Content="{Binding [MyKey], Source={StaticResource LocalizedStrings}}" />
<TextBlock Text="{Binding [MyKey], Source={StaticResource LocalizedStrings}}" />

This permit to specify only on key/value entry and reference it many times.
This is very similar to wp8/wp7.

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in c#, Silverlight, Store Apps, Windows 8, WP8, xaml e contrassegnata con , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...