2011-09-29 69 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無論何時何地你想要的。由於數據綁定,它在運行時很好地工作。 我保證這個解決方案不是最好的,但它的工作方式很簡單。 我希望它有幫助。