4

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?

2
  • 3
    technically a Converter is used in Binding context only. No binding, no converter. I think your issue can be solved with a workaround by using some custom MarkupExtension which accepts an input string, it then will convert the string to some another value depending on the target culture.
    – King King
    Commented Sep 18, 2014 at 10:46
  • 1
    As @KingKing said it has to be done using MarkupExtension. Go through these links- 1. stackoverflow.com/a/14839496/442444 2. wpftutorial.net/LocalizeMarkupExtension.html
    – Carbine
    Commented Sep 18, 2014 at 11:34

2 Answers 2

5

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}"
2
  • That's strange, worked fine for me without having to create a dummy string. Could you share the binding statement you tried?
    – har07
    Commented Sep 18, 2014 at 14:09
  • 1
    You are right, I wasn't using the ConvertParameter correctly (I was passing the parameter as a string). Thanks Commented Sep 18, 2014 at 14:22
1

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; } }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.