如何在“网站”中使用 ResourceManager模式?

发布于 2024-08-20 00:13:47 字数 1737 浏览 3 评论 0原文

我在这里尝试为我正在使用的应用程序进行手动翻译。 (已经有一个工作的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深居我梦 2024-08-27 00:13:47

如果您无权访问 HTTPContext,Marvin Smit 的解决方案很棒,

const string ASSEMBLY_NAME = "App_GlobalResources";
const string RESOURCE_NAME = "Resources.MetaTagResource";
const string RESOURCE_MANAGER = "ResourceManager";

Assembly assembly = Assembly.Load(ASSEMBLY_NAME);
Type type = assembly.GetType(RESOURCE_NAME);
PropertyInfo propertyInfo = type.GetProperty(RESOURCE_MANAGER);
ResourceManager resourceManager = propertyInfo.GetValue(null, new object[] { }) as ResourceManager;
resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);

但是如果您有权访问 HTTPContext,则只需使用 HttpContext.GetGlobalResourceObject

string title = HttpContext.GetGlobalResourceObject("MetaTagResource", "Title").ToString();
string keywords = HttpContext.GetGlobalResourceObject("MetaTagResource", "keywords").ToString();
string description = HttpContext.GetGlobalResourceObject("MetaTagResource", "Description").ToString();

Marvin Smit's solution is great if you do not have access to the HTTPContext

const string ASSEMBLY_NAME = "App_GlobalResources";
const string RESOURCE_NAME = "Resources.MetaTagResource";
const string RESOURCE_MANAGER = "ResourceManager";

Assembly assembly = Assembly.Load(ASSEMBLY_NAME);
Type type = assembly.GetType(RESOURCE_NAME);
PropertyInfo propertyInfo = type.GetProperty(RESOURCE_MANAGER);
ResourceManager resourceManager = propertyInfo.GetValue(null, new object[] { }) as ResourceManager;
resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);

But if you have access to the HTTPContext just use HttpContext.GetGlobalResourceObject

string title = HttpContext.GetGlobalResourceObject("MetaTagResource", "Title").ToString();
string keywords = HttpContext.GetGlobalResourceObject("MetaTagResource", "keywords").ToString();
string description = HttpContext.GetGlobalResourceObject("MetaTagResource", "Description").ToString();
挽你眉间 2024-08-27 00:13:47

也许您只是在寻找 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

︶葆Ⅱㄣ 2024-08-27 00:13:47

在 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.

夜巴黎 2024-08-27 00:13:47

这不是 C#,但你可以轻松地将其转换为

我在 app_localResources 目录中包含的 2 个文件

myPage.aspx.resx
myPage.aspx.fr-ca.resx

我在我的 .aspx 页面中像这样使用它,

<asp:Label ID="lAddress1" runat="server" Text="<%$ Resources: lAddress1 %>"></asp:Label>

这就是我在我的页面上管理它的方式

'using LCID from 
Public Enum elang
    En = &H1009  'en-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=1009&OS=Windows%202003%20%20Service%20Pack%201
    Fr = &HC0C   'fr-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=0C0C&OS=Windows%202003%20%20Service%20Pack%201
End Enum

'strongly typed value in session
Private _lang As elang = elang.En
Public Property lang() As elang
    Get
        Return _lang
    End Get
    Set(ByVal value As elang)
        _lang = value
    End Set
End Property

,并且在我想要使用资源管理器时得到的每个页面中

Protected Overrides Sub InitializeCulture()
    If Not Me.IsPostBack Then
        Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(Sess.lang)
        Threading.Thread.CurrentThread.CurrentCulture = Globalization.CultureInfo.CreateSpecificCulture(Threading.Thread.CurrentThread.CurrentUICulture.Name)
    End If
    MyBase.InitializeCulture()
End Sub

我在根目录创建一个全局资源文件级别我这样使用它

Dim rm = New System.Resources.ResourceManager("Resources.MyPage", Reflection.Assembly.Load("App_GlobalResources"))

this is not c# but you can easily convert it into it

I have in the app_localResources directory with 2 files

myPage.aspx.resx
myPage.aspx.fr-ca.resx

and I use it like this in my .aspx page

<asp:Label ID="lAddress1" runat="server" Text="<%$ Resources: lAddress1 %>"></asp:Label>

this is how I manage it on mine

'using LCID from 
Public Enum elang
    En = &H1009  'en-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=1009&OS=Windows%202003%20%20Service%20Pack%201
    Fr = &HC0C   'fr-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=0C0C&OS=Windows%202003%20%20Service%20Pack%201
End Enum

'strongly typed value in session
Private _lang As elang = elang.En
Public Property lang() As elang
    Get
        Return _lang
    End Get
    Set(ByVal value As elang)
        _lang = value
    End Set
End Property

and in every page I got

Protected Overrides Sub InitializeCulture()
    If Not Me.IsPostBack Then
        Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(Sess.lang)
        Threading.Thread.CurrentThread.CurrentCulture = Globalization.CultureInfo.CreateSpecificCulture(Threading.Thread.CurrentThread.CurrentUICulture.Name)
    End If
    MyBase.InitializeCulture()
End Sub

when I want to use the resourcemanager I create a global resources file at the root level the I use it like this

Dim rm = New System.Resources.ResourceManager("Resources.MyPage", Reflection.Assembly.Load("App_GlobalResources"))
风为裳 2024-08-27 00:13:47

编辑

如果是网站项目,请阅读此内容微软演练

恐怕您无法在网站项目中使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文