Reputation: 20803
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?
Upvotes: 2
Views: 9203
Reputation: 5629
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."]
Upvotes: 0
Reputation: 9160
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.
Upvotes: 5