C# 资源 (.resx) 文件 密钥命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/896342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resource (.resx) file Key naming conventions?
提问by Dawsy
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 the Keys in my resouce files. Has anyone tackled this before me?
我正在构建一个 C# 应用程序,它可能包含几个资源文件来存储用于语言翻译的字符串。我正在尝试为我的资源文件中的键提出一个命名约定。有没有人在我之前解决过这个问题?
采纳答案by bobbyalex
Just use Pascal naming convention. Dont attribute the key to a module or the class. Generalize it so that it can be reused.
只需使用 Pascal 命名约定。不要将密钥归因于模块或类。概括它以便它可以被重用。
Eg: ReadWriteWarningMessage
例如:ReadWriteWarningMessage
The dot separated convention works fine for menu items. But what about strings that are generated dynamically or user messages.
点分隔约定适用于菜单项。但是动态生成的字符串或用户消息呢?
回答by Sauron
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.
希望这会有所帮助。
回答by Jaguar
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.
你有没有考虑过像 Menu_File_Open 这样的下划线或像 Place_StringDescription 这样的东西?我目前采用一种方案,其中常见的东西去 Common_ 像 Common_PressHereTo 和查看特定去他们各自的地方像 MainMenu_FileOpen。通常,我在下划线之前输入资源出现的位置,在下划线之后输入描述性文本。
回答by Scott Whitlock
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.
我尝试将其组织为类似于我用来布局程序结构的命名空间。因此,如果您有 MyCompany.MyProduct.MyModule,那么该模块中的字符串将是 MyModule_Blah_Blah。这样,它们在整个产品中都是独一无二的。