Alaa Masoud
Alaa Masoud

Reputation: 7135

More convenient way to localize in windows phone apps

Currently, if you want to localize say Application Title in a windows phone app you would do this:

<TextBlock Text="{Binding LocalizedResources.ApplicationTitle, 
                  Source={StaticResource LocalizedStrings}}" />

This is too long, and certain parts are repeated for each binding. Even if you were to rename LocalizedResources property to R and LocalizedStrings class to LS for example, some repetition still exist.

So I tried making a class that inherits from Binding class and implemented as follows:

public class LocalizedBinding : Binding {
    public LocalizedBinding(string path) : base(path) {
        Source = Application.Current.Resources["LocalizedStrings"];
    }
}

The hope was to use it as follows:

<TextBlock Text="{b:LocalizedBinding LocalizedResources.ApplicationTitle}" />

However, the app crashes immediately upon start and I can't see any errors even in debugger. Any tips on how this might work?

Thanks

Edit:

Adding a parameterless constructor to LocalizedBinding and appending Path= to the binding fixes it.

Upvotes: 2

Views: 249

Answers (2)

David Fenko
David Fenko

Reputation: 131

You might try and put a Localization class into your App.xaml, then on Application_Launching check which language the user has set. Everywhere that you display the text you then refer to the App.xaml class.

Upvotes: 0

Alaa Masoud
Alaa Masoud

Reputation: 7135

This is fixed by adding a parameterless constructor to the LocalizedBinding class

public class LocalizedBinding : Binding {
    public LocalizedBinding() {
        Source = Application.Current.Resources["LocalizedStrings"];
    }

    public LocalizedBinding(string path) : base(path) {
        Source = Application.Current.Resources["LocalizedStrings"];
    }
}

Upvotes: 1

Related Questions