All throughout an application wherever error messages (or other user messages) are used I typically hard-code a string. Obviosly this can be really bad (especially when you may have to come back and localize an app). What is the best approach to centralize these strings? A static class? Constants? An XML File? Or a combination (like creating a static class with constants that are used to read from an xml file).
在任何使用错误消息(或其他用户消息)的应用程序中,我通常都会对字符串进行硬编码。显然,这可能非常糟糕(特别是当你可能不得不回来并本地化应用程序时)。集中这些字符串的最佳方法是什么?一个静态类?常量?一个XML文件?或者组合(比如创建一个带有常量的静态类,用于从xml文件中读取)。
5 个解决方案
#1
5
Create the strings in a resource file. You can then localise by adding additional resource files.
在资源文件中创建字符串。然后,您可以通过添加其他资源文件进行本地化。
Check out http://geekswithblogs.net/dotNETPlayground/archive/2007/11/09/116726.aspx
查看http://geekswithblogs.net/dotNETPlayground/archive/2007/11/09/116726.aspx
#2
1
Use string resources.
使用字符串资源。
#3
1
I've always defined constants wherever they make the most sense based on your language (a static class? application-wide controller? resource file?) and just call them where/whenever needed. Sure they're still "hard-coded" in a way at that point, but they're also nicely centralized, with naming conventions that make sense.
我总是根据你的语言(一个静态类?应用程序范围的控制器?资源文件?)定义常量,无论它们最有意义,只需在需要的地方/需要时调用它们。当然,它们在某种程度上仍然是“硬编码”的,但它们也很好地集中化,命名惯例是有意义的。
#4
1
Create a Resource (.resx) file and add your strings there. VS will generate a class for you for easy access to these resources with full intellisence. You can then add localised resources in the same manner.
创建一个资源(.resx)文件并在那里添加您的字符串。 VS将为您生成一个类,以便在完全智能的情况下轻松访问这些资源。然后,您可以以相同的方式添加本地化资源。
#5
0
.net has a pretty good support for so-called ressource-files where you can store all strings for one language.
.net对所谓的ressource文件有很好的支持,你可以在这里存储一种语言的所有字符串。
#1
5
Create the strings in a resource file. You can then localise by adding additional resource files.
在资源文件中创建字符串。然后,您可以通过添加其他资源文件进行本地化。
Check out http://geekswithblogs.net/dotNETPlayground/archive/2007/11/09/116726.aspx
查看http://geekswithblogs.net/dotNETPlayground/archive/2007/11/09/116726.aspx
#2
1
Use string resources.
使用字符串资源。
#3
1
I've always defined constants wherever they make the most sense based on your language (a static class? application-wide controller? resource file?) and just call them where/whenever needed. Sure they're still "hard-coded" in a way at that point, but they're also nicely centralized, with naming conventions that make sense.
我总是根据你的语言(一个静态类?应用程序范围的控制器?资源文件?)定义常量,无论它们最有意义,只需在需要的地方/需要时调用它们。当然,它们在某种程度上仍然是“硬编码”的,但它们也很好地集中化,命名惯例是有意义的。
#4
1
Create a Resource (.resx) file and add your strings there. VS will generate a class for you for easy access to these resources with full intellisence. You can then add localised resources in the same manner.
创建一个资源(.resx)文件并在那里添加您的字符串。 VS将为您生成一个类,以便在完全智能的情况下轻松访问这些资源。然后,您可以以相同的方式添加本地化资源。
#5
0
.net has a pretty good support for so-called ressource-files where you can store all strings for one language.
.net对所谓的ressource文件有很好的支持,你可以在这里存储一种语言的所有字符串。