After a recent post about how to allow the Resource Manager to use a non-supported culture I wondered how difficult it would be to support the usual localisation binding. So, in the great tradition of computer science I wanted to bind to Klingon. Whilst I can see that the correct solution would involve writing a custom tool I wanted a quick ‘hack’ and this is what I’m presenting here. To keep the post size down, I’m assuming you’ve followed the previous project and have that project available.
- Create the Klingon resx. Copy AppResource.en-GB.resx over to AppResources.klingon.resx
- Add the custom tool PublicResXFileCodeGenerator to AppResources.klingon.resx (see the properties of the file)
- Write some klingon – open the resx and change the ApplicationTitle to the following Klingon (it’s not rude) – TLHO’
- Create a new String Library class;
- Add the resource to App.xaml
- Now you can bind to Klingon, e.g.
public class AlienLocalizedStrings { private static AppResources_klingon _localizedResources; static AlienLocalizedStrings() { _localizedResources = new AppResources_klingon(); Type type = _localizedResources.GetType(); FieldInfo info = type.GetField("resourceMan", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.SetField); info.SetValue(null, new AlienResourceManager("klingon")); } public AppResources_klingon LocalizedResources { get { return _localizedResources; } } }
<Application.Resources> <local:LocalizedStrings x:Key="LocalizedStrings"/> <local:AlienLocalizedStrings x:Key="AlienLocalizedStrings"/> </Application.Resources>
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle , Source={StaticResource AlienLocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
The solution works pretty well, obviously the string component is hardcoded to ‘klingon’ which isn’t ideal but I’m sure that can also be resolved with a little more effort. Enjoy Klingon on the phone