Silverlight 4.0 来自数据库的Silverlight本地化,而不是resx

Silverlight 4.0 来自数据库的Silverlight本地化,而不是resx,silverlight-4.0,localization,silverlight-oob,Silverlight 4.0,Localization,Silverlight Oob,我有一个需要本地化的Silverlight 4 OOB应用程序。在过去,我使用了传统的resx路线,但有人要求我遵循现有winforms应用程序的架构 所有的字符串目前都存储在一个数据库中——我使用一个Web服务将它们下拉并写入本地Effiproz隔离存储数据库。登录时,我加载一个字典对象,其中包含用户语言的语言字符串。这个很好用 但是,我想自动化UI本地化(WinForms应用程序是这样做的): 循环浏览页面上的所有控件并查找任何文本块-如果有文本属性,我将用本地化版本替换它。如果找不到文本,

我有一个需要本地化的Silverlight 4 OOB应用程序。在过去,我使用了传统的resx路线,但有人要求我遵循现有winforms应用程序的架构

所有的字符串目前都存储在一个数据库中——我使用一个Web服务将它们下拉并写入本地Effiproz隔离存储数据库。登录时,我加载一个字典对象,其中包含用户语言的语言字符串。这个很好用

但是,我想自动化UI本地化(WinForms应用程序是这样做的): 循环浏览页面上的所有控件并查找任何文本块-如果有文本属性,我将用本地化版本替换它。如果找不到文本,则将字符串写入数据库以进行本地化

这在简单表单上可以正常工作,但一旦您有了Expander/ScrollViewer和内容控件,VisualTree解析器就不会返回这些控件的子控件,因为它们不一定可见(请参阅下面的代码)。阻止了我的自动化尝试

我的第一个问题是:有没有一种方法可以通过循环复杂(非可视)元素并在字典中查找值来自动加载页面

我的第二个问题是:如果不是,那么最好的处理方法是将字符串加载到应用程序资源字典中,并更改我的所有页面以引用它,或者我应该在服务器(并按照正常方式将其与应用程序打包)或客户端上生成resx文件(我有下载的字符串,我可以制作和加载resx文件吗?)

谢谢你的指点

以下是我现有的代码,它不适用于折叠的元素和复杂的内容控件:

  public void Translate(DependencyObject dependencyObject)
    {
        //this uses the VisualTreeHelper which only shows controls that are actually visible (so if they are in a collapsed expander they will not be returned). You need to call it OnLoaded to make sure all controls have been added
        foreach (var child in dependencyObject.GetAllChildren(true))
        {
            TranslateTextBlock(child);
        }
    }

private void TranslateTextBlock(DependencyObject child)
{
    var textBlock = child as TextBlock;
    if (textBlock == null) return;

    var value = (string)child.GetValue(TextBlock.TextProperty);
    if (!string.IsNullOrEmpty(value))
    {
        var newValue = default(string);
        if (!_languageMappings.TryGetValue(value, out newValue))
        {
            //write the value back to the collection so it can be marked for translation
            _languageMappings.Add(value, string.Empty);
            newValue = "Not Translated";
        }
        child.SetValue(TextBlock.TextProperty, newValue);
    }
}
然后我尝试了两种不同的方法:

1) 将字符串存储在普通字典对象中 2) 将字符串存储在普通字典对象中,并将其作为资源添加到应用程序中,然后可以将其作为

TextBlock Text="{Binding Path=[Equipment], Source={StaticResource ResourceHandler}}" 



App.GetApp.DictionaryStrings = new AmtDictionaryDAO().GetAmtDictionaryByLanguageID(App.GetApp.CurrentSession.DefaultLanguageId);

Application.Current.Resources.Add("ResourceHandler", App.GetApp.DictionaryStrings);

好的,所以没有人回答这个问题,我想出了一个解决办法

基本上,您似乎可以使用

Application.Current.Resources.Add("ResourceHandler", App.GetApp.DictionaryStrings);

<TextBlock Text="{Binding [Equipment], Source={StaticResource ResourceHandler}}" />
这种方法对我们来说是可行的

Text="{Binding Source='Logged on User', Converter={StaticResource LocalizationConverter}}"/>