問題描述
我正在嘗試在我的應用程序中進行本地化.我的解決方案中有名為ui.resx"和ui.de.resx"的 ui 資源文件.但是,我的實現(xiàn)中有些地方是不正確的,我很難過.
I'm trying to get localization working in my application. I have my ui resource files, named "ui.resx" and "ui.de.resx" in my solution. However, something in my implementation is incorrect, and I'm stumped.
ResourceManager res_man;
CultureInfo culture;
string exception;
private void myForm_Load(object sender, EventArgs e)
{
culture = CultureInfo.CreateSpecificCulture("de");
res_man = new ResourceManager("MyApp.ui.resx", typeof(myForm).Assembly);
doTheThing();
}
private void doTheThing()
{
try
{
BTN_save.text = ui.SAVE;
//Do code here
}
catch(Exception e)
{
exception = e.toString();
}
}
當我運行程序時,它會出錯并顯示異常:
When I run the program, it errors out and exception reads:
異常:System.Resources.MissingManifestResourceException:找不到適合指定區(qū)域性或中性區(qū)域性的任何資源.確保ui.resx.resources"在編譯時正確嵌入或鏈接到程序集myProgram"中,或者所需的所有附屬程序集都是可加載的且完全簽名的."
"Exception: System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "ui.resx.resources" was correctly embedded or linked into assembly "myProgram" at compile time, or that all the satellite assemblies required are loadable and fully signed."
推薦答案
你應該使用你的類的全名(帶命名空間)作為第一個參數(shù),比如:
You should use the full name (with namespace) of your class as first parameter lik:
var resman= new ResourceManager("Sample.Resources", typeof(Resources).Assembly);
要知道應該使用什么名稱,請打開 ui.resx
節(jié)點下的 ui.cs
并查看類的命名空間和類名并使用它們?nèi)缟蠄D.
To know what name you should use, open ui.cs
under the ui.resx
node and look at the namespace of the class and the class name and use them as shown above.
注意不要傳"MyApp.ui.resx"
,而是傳"MyApp.ui"
然后您可以簡單地使用管理器來獲取特定文化的資源,如下所示:
Then you can simply use the manager to get a resource for specific culture like this:
var str = resman.GetString("YourResourceKey", culture);
注意:
您應該注意的另一件事是,閱讀特定文化資源的方法更簡單.您可以簡單地設置:
Another thing you should notice, there is more simple way to read culture specific resources. You can simply set :
var culture= ...
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
設置文化后,無論您在何處使用,例如 MyApp.Ui.Key
,都將使用特定于該文化的 Key
的值.
After setting culture, wherever you use for example MyApp.Ui.Key
the value of the Key
specific to that culture will be used.
這篇關(guān)于C# 本地化和資源文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!