Dacian Mujdar
Dacian Mujdar

Reputation: 584

How to use a converter on a hard-coded value in xaml

I have a Windows Phone app which I want to be localized. Because I download the translation from a server on runtime, I can't use AppResources for this. What I would want to do is create a converter which changes the key to the string in the translated value. Something like :

    Text="{"STRING_OK", Converter={StaticResource TranslationConverter}}"

Of course, this code doesn't compile, but is there a workaround for this? How could I use the converter for hard-coded values?

Upvotes: 4

Views: 1635

Answers (2)

har07
har07

Reputation: 89315

Another possible workaround which simpler than creating MarkupExtension is to set the hard-coded value as converter parameter. Anyway, as @KingKing already pointed, binding is still required for us to be able to use Converter. We can just make the converter ignore value passed via binding and only consider ConverterParameter in the conversion logic :

Text="{Binding Converter={StaticResource TranslationConverter}, 
               ConverterParameter=STRING_OK}"

Upvotes: 5

Mawy
Mawy

Reputation: 85

you could not create a fixed number of AppResources for various languages since your case is a dynamic translation process, but still you can create a single Appresource file for your hardcoded string values and then you can use a converter of our own.

Text="{Binding Path=LocalizedResources.TextLabelLocale, Source={StaticResource LocalizedStrings},Converter={StaticResource TranslationConverter}}"

public class LocalizedStrings { public LocalizedStrings() { }

    private static sdkGlobalizationCS.AppResources localizedResources = new sdkGlobalizationCS.AppResources();

    public sdkGlobalizationCS.AppResources LocalizedResources { get { return localizedResources; } }
}

Upvotes: 1

Related Questions