views:

136

answers:

2

I'm having a little trouble getting localized resources files to work on Windows Phone 7. Here's what I'm doing:

  1. Create a resource file, say "Strings.resx" (Build Action: Compile)
  2. Create a key, say "TestKey" with a default value of empty string
  3. Add a English resource file in the same folder with a value of "some English string": Strings.en-us.resx (Build Action: Embedded Resource)
  4. Add a Japanese resource file in the same folder with a value of "some Japanese string": Strings.ja-jp.resx (Build Action: Embedded Resource)

In my PC Silverlight, WPF Apps that works fine when I change the Thread.CurrentThread.CurrentCulture. But in the phone I always seem to be getting the value that's in the Strings.resx file - an empty string.

I have tried using the designer generated code and wiring up the resource manager by hand and it does not seem to matter. Here's my code:

            Type t = typeof(Strings);

            _resourceManager = new ResourceManager(
                t.Namespace + "." + t.Name,
                t.Assembly);

            _resourceManager.GetString("TestKey");

Tell me localized resources are supported on the phone... ;> What am I doing wrong? Thanks!

Update: Thanks Olivier for forwarding the link. I saw that as well but missed an important step. I didn't add the "SupportedCultures" node to my csproj. Made all the difference - hoping someone else doesn't loose two hours trying to figure this out like I did.

<SupportedCultures>de-DE;es-ES;</SupportedCultures>
+1  A: 

Of course, localized resources are supported on the phone:

How to: Build a Localized Application for Windows Phone

Olivier PAYEN
A: 

I wrote a blog post that provides links to a bunch of Globalization / Localization guides for WP7. There is a Windows Phone 7 in 7 Training video that helped me understand the basics. After that it was simply a matter of learning how to do databinding:

The MSDN article shows you how to setup the files and create the LocalizedStrings class, but they then assume that you know how to use that class for data binding. Visual Studio 2010 and Silverlight handle data binding differently than Winforms, and it gets even more confusing since XAML also has it’s own definition of Resources that are different then the .NET resources we just created. Silverlight also uses the term Resource to refer to files that use the the Build Action of "Content”, as these files get wrapped up into the .XAP file similar to how files with Build Action of "Resource” get embedded into the .Dll assembly (ex: loading an image from content or resource files). I found that instead of using the Text="{Binding Path=resourceFile.resourceName, Source={StaticResource Localizedresources }}" XAML syntax it was easier to use the following steps:

  1. Open your primary XAML page (usually MainPage.xaml) in the Visual Studio designer

  2. Open the properties for the PhoneApplicationPage and set the DataContext to be Application.Resources –> LocalizedStrings. NOTE: if you already are using a DataContext object, then you should integrate the LocalizedStrings class into that object so that it has localization support.

  3. Once the Page’s DataContext has been set you can change the data binding for any control on the page by simply selecting the property (ex: text, checked, etc), selecting “Apply Data Binding…”, and setting the Path to Localizedresources.BtnText or whatever the name of the desired resource value is.

Greg Bray
This is a great answer, thanks very much for taking the time!
will