如何在“网站”中使用 ResourceManager模式?
我在这里尝试为我正在使用的应用程序进行手动翻译。 (已经有一个工作的 LocalizationModule 但它工作不可靠,所以我不能使用
标签。
通常使用 ResourceManager,你应该将它用作 Namespace .Folder.Resourcename
(在应用程序中)。目前我正在翻译现有的 asp.net“网站”(不是 Web 应用程序,因此这里没有命名空间......)
。 /resources”,其中包含“fr-ca.resx”和“en-us.resx”。
所以我使用了这样的代码:
public static string T(string search)
{
System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null );
var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture);
if (text == null)
return "null";
else if (text == string.Empty)
return "empty";
else
return text;
}
在页面内部我有这样的内容 <%= Locale.T ("T_HOME") %>
当我刷新时,我有这个:
找不到任何资源 适合特定文化 或中立文化。确保 “Locales.resources”正确 嵌入或链接到程序集中 编译时的“App_Code.9yopn1f7”, 或者所有卫星组件 所需的可加载且完全 签署了。描述:未处理 期间发生异常 执行当前的网络请求。 请查看堆栈跟踪以了解更多信息 有关错误及其位置的信息 它起源于代码。
异常详细信息: System.Resources.MissingManifestResourceException: 找不到任何资源 适合特定文化 或中立文化。确保 “Locales.resources”正确 嵌入或链接到程序集中 编译时的“App_Code.9yopn1f7”, 或者所有卫星组件 所需的可加载且完全 已签署。
来源错误:
第 14 行:
系统.资源.资源管理器 资源=新 系统.资源.资源管理器( “区域设置”, System.Reflection.Assembly.GetExecutingAssembly(), 无效的 );第 15 行: 第 16 行:var 文本 = resMan.GetString(搜索, System.Threading.Thread.CurrentThread.CurrentCulture); 第 17 行:第 18 行:if (text == null)源文件: c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs 线路:16
我什至尝试使用 Locales.fr-ca 加载资源,或者仅使用 fr-ca 加载资源,但这里不起作用。
I am trying here to do a manual translation for the application I am working with. (There is already a working LocalizationModule but it's working dodgy, so I can't use <asp:Localize />
tags.
Normally with ResourceManager you are supposed to be using it as Namespace.Folder.Resourcename
(in an application). Currently I am translating an existing asp.net "website" (not web application so no namespace here....).
The resources are located into a folder name "Locales/resources" which contains "fr-ca.resx" and "en-us.resx".
So I used a code with something like this :
public static string T(string search)
{
System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null );
var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture);
if (text == null)
return "null";
else if (text == string.Empty)
return "empty";
else
return text;
}
and inside the page I have something like this <%= Locale.T("T_HOME") %>
When I refresh I have this :
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Locales.resources" was correctly
embedded or linked into assembly
"App_Code.9yopn1f7" at compile time,
or that all the satellite assemblies
required are loadable and fully
signed. Description: An unhandled
exception occurred during the
execution of the current web request.
Please review the stack trace for more
information about the error and where
it originated in the code.Exception Details:
System.Resources.MissingManifestResourceException:
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Locales.resources" was correctly
embedded or linked into assembly
"App_Code.9yopn1f7" at compile time,
or that all the satellite assemblies
required are loadable and fully
signed.Source Error:
Line 14:
System.Resources.ResourceManager
resMan = new
System.Resources.ResourceManager(
"Locales",
System.Reflection.Assembly.GetExecutingAssembly(),
null ); Line 15: Line 16: var
text = resMan.GetString(search,
System.Threading.Thread.CurrentThread.CurrentCulture);
Line 17: Line 18: if (text == null)Source File:
c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs
Line: 16
I even tried to load the resource with Locales.fr-ca
or only fr-ca
nothing quite work here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您无权访问 HTTPContext,Marvin Smit 的解决方案很棒,
但是如果您有权访问 HTTPContext,则只需使用 HttpContext.GetGlobalResourceObject
Marvin Smit's solution is great if you do not have access to the HTTPContext
But if you have access to the HTTPContext just use HttpContext.GetGlobalResourceObject
也许您只是在寻找 System.Web.Page 的基础 GetLocalResourceObject()?
请参阅 http://msdn.microsoft.com/en-us/library/ms153597 .aspx
Maybe you are just looking for The System.Web.Page's base GetLocalResourceObject(),?
see http://msdn.microsoft.com/en-us/library/ms153597.aspx
在 ASP.Net 中使用资源时,您将看到 .resx 文件 (ResXCodeGen) 上使用了专门的编译器(自定义 Tool 属性),
底线是您的资源被编译为类。当您想要寻址其中的资源时,应该使用该类的名称。
类的名称生成如下:
{项目名称空间}{文件夹结构}{文件名}.resx(.resx 不是生成名称的一部分)
既然您提到您的 resx 文件位于目录名称“Locales/resources”中, ”,您必须使用的名称是(假设 resx 文件名为“locales.resx”)
“locales.resources.locales”
添加语言和区域设置,如“uk-en”是由资源添加的管理器,根据创建 ResourceManager 实例时指定的文化加载适当的程序集。如果未给出,则使用 Thread.CurrentCulture。您不应该自己使用语言/区域设置扩展,将其留给资源管理器来处理。
如果您不确定资源类将如何结束,您始终可以查看 MSIL 或使用反射器来确定实际部署的程序集中资源类的名称。
;-- 在放置的注释后添加 ----------
我还查看了我周围的一些其他代码;
您是否尝试过 App_GlobalResources 方法的反射?
1:您在网站上下文中(在 HttpHandler、Aspx、Ascx 等中)加载“App_GlobalResources”库。 =>
Assembly.Load("App_GlobalResources");
2:浏览其中的可用类型并从每个类型中获取“ResourceManager”属性。=>
PropertyInfo pi = type.GetProperty("ResourceManager");
resManager = pi.GetValue(null, new object[] { }) 作为 ResourceManager;
3:如果有的话,获取你想要的ResourceSet => resManager.GetResourceSet(englishCulture, true, true);
希望这有帮助。
When using resources in ASP.Net, you will see a specialized compiler being used (custom Tool property) on the .resx files (ResXCodeGen)
Bottom line of this is that your resources are compiled into a class. The name of this class is the name you should use when you want to address the resources in there.
The name of the class is generated as follows:
{project namespace}{folder structure}{filename}.resx (.resx is NOT part of the generated name)
Since you mentioned your resx file is located in a directory name "Locales/resources", the name you would have to use is (assuming the resx file is called 'locales.resx')
"locales.resources.locales"
The addition of the language and locale, like the "uk-en" is added by the resource manager, loading the appropriate assembly based on the Culture that is specified when creating an instance of the ResourceManager. If none is given, the Thread.CurrentCulture is used. You should not use the language/locale extensions yourself, leave it to the resource manager to deal with that.
If you are not sure how the resources class will end up, you can always look at the MSIL or use reflector to determine the name of the resource class in the actual deployed assembly.
;-- Added after comments where placed -------
I also looked at some other code i had arround;
Have you tried the Reflection on App_GlobalResources approach?
1: You load the "App_GlobalResources" library while in the context of the website (in a HttpHandler, Aspx, Ascx, etc). =>
Assembly.Load("App_GlobalResources");
2: Walk through the available types in there and grab the "ResourceManager" property from each type.=>
PropertyInfo pi = type.GetProperty("ResourceManager");
resManager = pi.GetValue(null, new object[] { }) as ResourceManager;
3: If it had one, get the ResourceSet you want => resManager.GetResourceSet(englishCulture, true, true);
Hope this helps.
这不是 C#,但你可以轻松地将其转换为
我在 app_localResources 目录中包含的 2 个文件
我在我的 .aspx 页面中像这样使用它,
这就是我在我的页面上管理它的方式
,并且在我想要使用资源管理器时得到的每个页面中
我在根目录创建一个全局资源文件级别我这样使用它
this is not c# but you can easily convert it into it
I have in the app_localResources directory with 2 files
and I use it like this in my .aspx page
this is how I manage it on mine
and in every page I got
when I want to use the resourcemanager I create a global resources file at the root level the I use it like this
编辑
如果是网站项目,请阅读此内容微软演练。
恐怕您无法在网站项目中使用 ResourceMagager,因为它需要资源所在的命名空间/程序集。
Edit
In case of a Web Site project, read this walkthrough of Microsoft.
I'm afraid you can't use the ResourceMagager in a Website project, because it requires a namespace / assembly where the resources are located.