2014-10-27 105 views
1

在某些情況下,我們的項目我們必須忽略當前文化應用 和英語獲取資源。只有英語如何從特定的資源文件的字符串資源在C#

我們基本上做到像所有的語言

<label>@ModelEntities.Properties.Resource.Gender</label> 

怎麼可能得到?

我假設我們不知何故創建了一些參考Resource.resx

我用

ResourceManager rm = new ResourceManager("Resource.Strings", 
        Assembly.GetAssembly(typeof(ModelEntities.Properties.Resource))); 
    var s = rm.GetString("Age"); 

但好像不工作。

謝謝!

回答

2

我找到了可行的解決方案。

我們所有的資源文件,第一種是像結束

Resource.resx

,而不是像Resources.resx

而在第二,我們有一個單獨的程序集資源:ModelEntities

所以工作代碼如下

var resourceManager = new ResourceManager(typeof(ModelEntities.Properties.Resource)); 
var genderString = resourceManager.GetString("Gender", ci); 
+1

This works.Thank you – 2017-05-16 09:41:52

4

您可以使用資源管理器類click here for more info about the class

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-GB"); 
ResourceManager resourceManager = new ResourceManager("YourResource", Assembly.GetExecutingAssembly()); 
string bodyResource = resourceManager.GetString("yourText", ci); 
+0

你爲什麼幀'C#'代碼到這樣的片段? – Sinatr 2014-10-27 16:00:11

+0

我的猜測是他沒有意識到代碼片段是什麼,因爲他是一個相當新的用戶。我編輯了答案並用一個簡單的代碼塊取代了 – 2014-10-27 16:14:04

+0

確實我是一個新用戶。使用Tidy功能。感謝Anthony Shaw – 4nis 2014-10-27 16:17:26

3

一個簡單的方法:

YorResourceName.ResourceManager.GetString("ResourceKeyName", System.Globalization.CultureInfo.GetCultureInfo("en-US"))