Question

I want to translate my App with the Multilingual App Toolkit for Windows Phone, but I haven't found anything about how to use the translation in code. In XAML it looks like this:

<TextBlock Text="{Binding Path=LocalizedResources.Hallo, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

This don't help if I want have a localized string in code.

I hope you understand my question, and can help me. (I know my English is very bad).

Was it helpful?

Solution

In App.xaml add key which points to your class LocalizedStrings:

<Application.Resources xmlns:my="clr-namespace:NameSpaceWhereLocalizedStrings">
    <my:LocalizedStrings x:Key="Localized"/>
</Application.Resources>

clr-namespace: - is a namespace where you have your LocalizedStrings (probably your main namespace). Then you can use in any other xaml file your LocalizedResources:

<TextBlock Text="{Binding LocalizedResources.Hallo, Source={StaticResource Localized}}"/>

Hallo - is your variable in AppResources.resx (of course check if it's public). LocalizedResources is a class which you probably have as a default in LocalizedStrings.cs:

public class LocalizedStrings
{
  private static AppResources _localizedResources = new AppResources();
  public AppResources LocalizedResources { get { return _localizedResources; } }
}

In code I'm using then:

string myName = AppResources.Hallo;

Note that after adding a variable or changing it you will have to rebuild the project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top