問題描述
我正在嘗試在 C# 類中使用 ResourceManager,但在創建 ResourceManager 類的新實例時不知道用什么替換基本名稱.
I'm trying to use the ResourceManager in a C# class, but don't know what to substitute for the basename when creating a new instance of the ResourceManager class.
我有一個單獨的項目,其中包含我上面的項目引用的資源文件,命名如下:
I have a separate project that contains the resource files which is referenced by my project above named as the following:
- Resources.resx
- Resources.es-ES.resx
如果我有如下代碼片段,當我想使用英語或西班牙語版本的資源時,我應該用什么代替基本名稱?
If I have a code snippet as follows, what should I substitute for the basename when I want to use the English or Spanish version of the resources?
ResourceManager RM = new ResourceManager(basename,Assembly.GetExecutingAssembly());
我嘗試了 Tom 建議的方法,但遇到了臭名昭著的錯誤
I tried the approach suggested by Tom, but am getting the infamous error
找不到任何適合指定文化或中立文化的資源.
Could not find any resources appropriate for the specified culture or the neutral culture.
我的解決方案有兩個項目,其中項目 YeagerTech 是一個 Web 應用程序,項目 YeagerTechResources 包含所有資源.Project YeagerTech 參考了 YeagerTechResources.
My solution has two projects where project YeagerTech is a web application and project YeagerTechResources contains all the resources. Project YeagerTech has a reference to YeagerTechResources.
我在創建資源管理器時使用以下語法:
I have the following syntax when creating my Resource Manager:
ResourceManager RM = new ResourceManager("YeagerTechResources.Resources",
Assembly.GetExecutingAssembly());
顯然,這是不正確的.
YeagerTechResources 項目中的資源文件的 BuildAction 設置為 Embedded Resource.
The resource files in the YeagerTechResources project have their BuildAction set to Embedded Resource.
我的資源文件的名稱是:Resources.resx 和 Resources.es-ES.resx.
The name of my resource files are: Resources.resx and Resources.es-ES.resx.
如果有人可以在實例化資源管理器時根據我的項目和資源文件名簡單地告訴我要使用的確切語法,我將不勝感激......
If someone can simply tell me the exact syntax to use based on my project and resource file names when instantiating the resource manager, I would greatly appreciate it...
我已盡我所能解決此問題,但無法....
I've done everything I can think of to resolve this and can't....
這是我最后一次嘗試在這里解決它......
This is my last attempt to resolve it here...
ResourceManager RM = new ResourceManager("YeagerTechResources.Resources", Assembly.GetExecutingAssembly());
sb.Append(RM.GetString("RegisterThanks"));
執行上述代碼后出現以下錯誤:
I am getting the following error after executing the code above:
找不到任何適合指定文化或中性文化的資源.確保YeagerTechResources.Resources.resources"在編譯時已正確嵌入或鏈接到程序集YeagerTech"中,或者所有所需的附屬程序集都是可加載且完全簽名的.
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "YeagerTechResources.Resources.resources" was correctly embedded or linked into assembly "YeagerTech" at compile time, or that all the satellite assemblies required are loadable and fully signed.
我可以毫無問題地使用 HTML 標記中的資源,但是在控制器中使用 C# 代碼時,我不斷收到上述錯誤.
I am able to use the resources in the HTML markup with absolutely no issues, but when coming to the C# code in the Controller, I keep on getting the above error.
任何關于我需要的基于我的項目的確切語法的幫助將不勝感激.
Any help for the exact syntax I need based on my projects would be greatly appreciated.
推薦答案
通過字符串讀取資源的方法非常簡單:
There's surprisingly simple way of reading resource by string:
ResourceNamespace.ResxFileName.ResourceManager.GetString("ResourceKey")
在不能使用點符號"的情況下(例如,當資源鍵被持久保存在數據庫中時),它是一種簡潔而優雅的解決方案.
It's clean and elegant solution for reading resources by keys where "dot notation" cannot be used (for instance when resource key is persisted in the database).
這篇關于使用資源管理器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!