简体   繁体   English

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

[英]Resource (.resx) file Key naming conventions?

I'm building a C# app that will likely contain a couple resource files to store strings for use in language translation.我正在构建一个 C# 应用程序,它可能包含几个资源文件来存储用于语言翻译的字符串。 I'm trying to come up with a naming convention for the Keys in my resouce files.我正在尝试为我的资源文件中的键提出一个命名约定。 Has anyone tackled this before me?有没有人在我之前解决过这个问题?

Just use Pascal naming convention.只需使用 Pascal 命名约定。 Dont attribute the key to a module or the class.不要将密钥归因于模块或类。 Generalize it so that it can be reused.概括它以便它可以被重用。

Eg: ReadWriteWarningMessage例如:ReadWriteWarningMessage

The dot separated convention works fine for menu items.点分隔约定适用于菜单项。 But what about strings that are generated dynamically or user messages.但是动态生成的字符串或用户消息呢?

have you considered underscores like Menu_File_Open or something like Place_StringDescription?你有没有考虑过像 Menu_File_Open 或 Place_StringDescription 这样的下划线? I currently employ a scheme where common stuff go to Common_ like Common_PressHereTo and view specific go to their respective place like MainMenu_FileOpen.我目前采用一种方案,其中常见的东西去 Common_ 像 Common_PressHereTo 和查看特定去他们各自的地方像 MainMenu_FileOpen。 In general, before the underscore i type where the Resource appears and after the underscore a descriptive text.通常,我在下划线之前输入资源出现的位置,在下划线之后输入描述性文本。

I try to organize it similar to the namespaces I'm using to layout the program structure.我尝试将其组织为类似于我用来布局程序结构的命名空间。 So if you have MyCompany.MyProduct.MyModule, then strings in that module would be MyModule_Blah_Blah.因此,如果您有 MyCompany.MyProduct.MyModule,那么该模块中的字符串将是 MyModule_Blah_Blah。 That way they're unique within the overall product.这样,它们在整个产品中都是独一无二的。

See https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-resources .请参阅https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-resources As @bobbyalex has said, this includes using PascalCasing, since the generated resource designer file does implement the resources as properties.正如@bobbyalex 所说,这包括使用 PascalCasing,因为生成的资源设计器文件确实将资源实现为属性。

✔️ DO use PascalCasing in resource keys. ✔️ 务必在资源键中使用 PascalCasing。

✔️ DO provide descriptive rather than short identifiers. ✔️ 务必提供描述性而非简短的标识符。

❌ DO NOT use language-specific keywords of the main CLR languages. ❌ 不要使用主要 CLR 语言的特定于语言的关键字。

✔️ DO use only alphanumeric characters and underscores in naming resources. ✔️ 务必在命名资源时仅使用字母数字字符和下划线。

✔️ DO use the following naming convention for exception message resources. ✔️ 务必对异常消息资源使用以下命名约定。

The resource identifier should be the exception type name plus a short identifier of the exception:资源标识符应该是异常类型名称加上异常的短标识符:

ArgumentExceptionIllegalCharacters ArgumentExceptionInvalidName ArgumentExceptionFileNameIsMalformed ArgumentExceptionIllegalCharacters ArgumentExceptionInvalidName ArgumentExceptionFileNameIsMalformed

If you have a Name Value Pair in resources like如果您在资源中具有名称值对,例如

CloseConfirmation - Do you want to close the window without saving ? CloseConfirmation - 你想不保存就关闭窗口吗?

Add a new class called Messages.添加一个名为 Messages 的新类。

 public static class Messages
{
    public const String CloseConfirmation = "CloseConfirmation";
    public static String GetMessage( String messageId )
    {
        return //your namespace//.Properties.Resources.ResourceManager.GetString( messageId );
    }}

and to access it use并访问它使用

MessageBox.Show( Messages.GetMessage(Messages.CloseConfirmation));

Hope this will help.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM