Resource (.resx) file Key naming conventions?

后端 未结 5 407
一生所求
一生所求 2020-12-30 21:13

I\'m building a C# app that will likely contain a couple resource files to store strings for use in language translation. I\'m trying to come up with a naming convention for

相关标签:
5条回答
  • 2020-12-30 21:24

    If you have a Name Value Pair in resources like

    CloseConfirmation - Do you want to close the window without saving ?

    Add a new class called 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.

    0 讨论(0)
  • 2020-12-30 21:28

    Just use Pascal naming convention. Dont attribute the key to a module or the class. Generalize it so that it can be reused.

    Eg: ReadWriteWarningMessage

    The dot separated convention works fine for menu items. But what about strings that are generated dynamically or user messages.

    0 讨论(0)
  • 2020-12-30 21:31

    have you considered underscores like Menu_File_Open or something like 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. In general, before the underscore i type where the Resource appears and after the underscore a descriptive text.

    0 讨论(0)
  • 2020-12-30 21:34

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

    ✔️ DO use PascalCasing in resource keys.

    ✔️ DO provide descriptive rather than short identifiers.

    ❌ DO NOT use language-specific keywords of the main CLR languages.

    ✔️ 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

    0 讨论(0)
  • 2020-12-30 21:39

    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. That way they're unique within the overall product.

    0 讨论(0)
提交回复
热议问题