ASP.NET .resx 资源文件中的有效资源密钥

发布于 2024-07-24 05:47:08 字数 500 浏览 8 评论 0原文

我正在使用 CMS 工具生成 .resx 资源文件。

创建其中包含空格或标点符号的资源名称是否有任何危险?

如果我使用此语法来获取资源,它可以正常工作:

GetGlobalResourceObject("myresources", "audio,visual");

但是,这会导致声明性资源语法出现错误,例如:

<asp:Literal ID="litLastName" runat="server" Text="<%$ Resources: GlobalResources,audio,visual %>"></asp:Literal>

此外,当我在 Visual Studio 中编辑 .resx 文件时,如果我的资源键包含除此之外的任何字符,它会向我发出警告字母数字和下划线。 它说“资源名称”不是有效的标识符。

我在这里违反了 .NET 规则吗?

I am using a CMS tool to generate .resx resource files.

Is there any danger in creating resource names with spaces or punctuation characters in them?

If I use this syntax to get resources, it works fine:

GetGlobalResourceObject("myresources", "audio,visual");

However, this causes an error with declarative resource syntax, e.g.:

<asp:Literal ID="litLastName" runat="server" Text="<%$ Resources: GlobalResources,audio,visual %>"></asp:Literal>

Also, when I edit .resx files in Visual Studio, it gives me warnings if my resource keys contain any characters besides alphanumerics and underscores. It says "The resource name " is not a valid identifier".

Am I breaking a .NET rule here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别把无礼当个性 2024-07-31 05:47:08

资源键的一般准则与您定义的变量的规则相同。

以下是 asp.net 论坛中的一条评论,指出不允许使用句点:

http ://forums.asp.net/t/967741.aspx

这是关于 resx 文件密钥命名约定的命名约定的问题:

资源 (.resx) 文件密钥命名约定?

另外,如果您考虑一下它们在具有空格或奇怪内容的密钥的应用程序中如何使用它们也将真正对代码进行处理。 在将资源文件用于 Telerik 控件之类的情况下,这尤其糟糕,如控件快速教程中所示:

http://www.telerik.com/help/aspnet-ajax/advancedmultilinguallocal.html

如果现在您在那里添加一个名称,例如“

I like to put spaces in resource keys.ChartTitle.TextBlock.Text

好吧,一切都会呕吐”地方,因为空间意味着某种东西。

(在上面的链接中也很明显为什么该期限不再有效)

我想我也可以说,似乎微软的某人肯定认为这是一个错误,这就是为什么 Visual Studio 给你这个错误。 尽管如此,质疑男人对我们的权威和力量当然是件好事。

从长远来看,您可能没有理由需要做如此非常规的事情。

The general guideline for resource keys are the same as the rules for a variable that you define.

Here is a comment in the asp.net forums stating that the use of a period is not allowed:

http://forums.asp.net/t/967741.aspx

Here is a question on SO about naming conventions for resx file key naming conventions:

Resource (.resx) file Key naming conventions?

Also, if you think about how they are used in the application having a key with a space or something odd is going to really hose the code as well. This is especially bad in the case of using a resource file for something like a telerik control as seen in this quick tutorial for a control:

http://www.telerik.com/help/aspnet-ajax/advancedmultilanguagelocal.html

If now you were adding a name there like

I like to put spaces in resource keys.ChartTitle.TextBlock.Text

Well, everything will barf all over the place because spaces mean something.

(It is also obvious in the above link why the period is no longer valid)

I suppose I could also punt and say, it seems that someone at Microsoft certainly thinks that's an error and that's why visual studio is giving you that error. Although, it is certainly good to question authority and the power of the man over us.

In the long run, there's probably not a reason why you would NEED to do something so unconventional.

娜些时光,永不杰束 2024-07-31 05:47:08

十年过去了,显然微软现在建议在使用资源进行本地化时,可以使用原始语言中的实际字符串作为资源名称。 如果不允许使用句号、逗号等常见字符,则该方法将不起作用。

https://learn.microsoft.com/ en-us/aspnet/core/fundamentals/localization?view=aspnetcore-6.0

IStringLocalizer 不需要将默认语言字符串存储在资源文件中。

例子:

ViewData["Message"] = _localizer["Your application description page."]

It's a decade later, and apparently MS now suggests that when using resources for localization, the actual string in the original language can be used as the resource name. That would not work if common characters like period, comma, etc were not allowed.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-6.0

IStringLocalizer doesn't require storing the default language strings in a resource file.

Example:

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