Using embedded resources in Silverlight (4) - other cultures not being compiled
I am having a bit of a hard time providing localized strings for the UI in a small Silverlight 4 application. Basically I've put a folder "Resources" and placed two resource files in it :
Statuses.resx
Statuses.ro.resx
I do have an enum Statuses :
public enum Statuses
{
None,
Working
}
and a convertor :
public class StatusToMessage : IValueConverter
{
public object Convert(object value, Type targetTy开发者_C百科pe, object parameter, CultureInfo culture)
{
if (!Enum.IsDefined(typeof(Status), value))
{
throw new ArgumentOutOfRangeException("value");
}
var x = Statuses.None;
return Statuses.ResourceManager.GetString(((Status)value).ToString(), Thread.CurrentThread.CurrentUICulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
in the view I have a textblock :
<TextBlock Grid.Column="3" Text="{Binding Status, Converter={StaticResource StatusToMessage}}" />
Upon view rendering the converter is called but no matter what the Thread.CurrentThread.CurrentUICulture is set it always returns the default culture value.
Upon further inspection I took apart the XAP resulted file, taken the resulted DLL file to Reflector and inspected the embedded resources. It only contains the default resource!!
Going back to the two resource files I am now inspecting their properties :
Build action : Embedded Resource Copy to output directory : Do not copy Custom tool : ResXFileCodeGenerator Custom tool namespace : [empty]
Both resource (.resx) files have these settings. The .Designer.cs resulted files are as follows :
Statuses.Designer.cs :
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SilverlightApplication5.Resources {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Statuses {
// ... yadda-yadda
Statuses.ro.Designer.cs
[empty]
I've taken both files and put them in a console application and they behave as expected in it, not like in this silverlight application.
What is wrong?
It turns out you just have to do one more tintsy thing. As the MSDN article says :
In Solution Explorer, right-click the project name, and then click Unload Project to close the project while leaving the project icon visible.
In Solution Explorer, right-click the project name, and then click Edit.
The project file opens in the Visual Studio XML Editor.
In the project file, add the names of the region-neutral and specific cultures whose satellite assemblies your application has created to the <SupportedCultures> tag. If your application supports multiple cultures, use a semicolon (;) to separate their names. This list of cultures should not include your application's default culture. For example, a <SupportedCultures> tag for an application whose default culture is English ("en") and that supports the English - United States ("en-US"), French ("fr"), French - France ("fr-FR"), Russian ("ru"), and Russian - Russia ("ru-RU") cultures might appear as follows:
<SupportedCultures>en-US;fr;fr-FR;ru;ru-RU;</SupportedCultures>
So, remember to f!@#ingly manual edit the project file and specify which cultures to include in the compilation.
Now it effing works :D
精彩评论