2011-09-29 71 views
1

我是新来的本地化...... ..我最初创建一个资源文件(.resx文件),然后使用resgen命令来生成相应的.resource文件..它被创建..然后我用al .exe文件来生成相应的..这也得到了建立卫星装配..但进一步的,当我试图使用资源管理器类扔东西的错误代码隐藏从访问.resx文件like--.NET窗口WPF本地化

集清单例外..

我不明白我在哪里错了.. PLZ让我知道是否有任何进一步的过程要做(PLZ不建议LOCBAML工具)..我想只使用资源文件的解决方案..

回答

3

你的开始很不错。使用LocBaml对我来说太复杂了。所以我不得不找出更容易的东西。这里的解决方案:首先,你必须创建资源.resx文件(这一步已经完成了);从资源文件获取所有字符串的简单方法是将其存储在字典中。您可以使用此方法做到这一点:

public Dictionary<string, string> ApplicationStrings(string locale) 
    { 
     Dictionary<string, string> result = new Dictionary<string, string>(); 

     ResourceManager manager = null; 
// Here is two locales (custom and english locale), but you can use more than two if there's a need 
      if (locale == "Your locale") 
      { 
       manager = new ResourceManager(typeof(your_locale_resource_file)); 
      } 
      else 
      { 
       manager = new ResourceManager(typeof(ApplicationStrings_en)); 
      } 
     ResourceSet resources = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true); 

     IDictionaryEnumerator enumerator = resources.GetEnumerator(); 

     while (enumerator.MoveNext()) 
     { 
      result.Add((string)enumerator.Key, (string)enumerator.Value); 
     } 

     return result; 
    } 

你必须设置主窗口的DataContext的是这种方法的结果,并结合你的字符串到字典中。 绑定例如:

Text="{Binding [YourKey]}" 

您可以调用该方法,然后改变的DataContext无论何时何地你想要的。由于数据绑定,它在运行时很好地工作。 我保证这个解决方案不是最好的,但它的工作方式很简单。 我希望它有帮助。