在 Android 应用程序中,资源在 xml 文档中指定,这些文档自动内置到 R 类中,可以在源代码中作为强类型轻松访问。
有什么方法可以对常规 Java 桌面应用程序使用类似的方法吗?
我想要完成的任务是从代码中删除字符串(或多或少作为“层”的分离),并通过简单地告诉程序选择 xml 来轻松添加对本地化的支持与所需语言相对应的文件。
我用谷歌搜索了一下,但我正在寻找的东西似乎淹没在有关解析或输出 xml 的结果中,而不是利用 xml 生成代码的工具。
In Android applications, resources are specified in xml documents, which automatically are built into the R
class, readily accessible within the source code as strongly typed.
Is there any way I could use a similar approach for a regular Java desktop application?
What I'd like to accomplish, is both the removal of strings from the code (as a separation of "layers", more or less) and to make it easy to add support for localization, by simply telling the program to choose the xml file corresponding to the desired language.
I've googled around a bit, but the things I'm looking for seem to be drowning in results about parsing or outputting xml, rather than tools utilizing xml to generate code.
发布评论
评论(4)
Eclipse 的消息包实现(例如由插件使用)与外部化字符串功能集成,并为字符串生成静态类和资源属性文件:
http://www.eclipse.org/eclipse/platform-core/documents/3.1/message_bundles.html
为此集成工作 Eclipse 需要在类路径上看到 org.eclipse.osgi.util.NLS。根据记忆,它所提供的库的依赖关系对于我使用这种方法的项目来说有点棘手,所以我刚刚获得了源代码并将其作为我的核心模块中的独立类(有关更多信息,请参阅评论)在那)。
它提供了您正在寻找的类型安全性,并且 IDE 功能可以节省大量时间。到目前为止,我还没有发现这种方法有任何缺点。
编辑:这实际上是 Ghostbust555 在评论中提到的,但在那篇文章中并不清楚这不仅限于 Eclipse 插件,而且您通过消息类的静态成员引用您的资源。
我还没有看到其他人在自己的应用程序中使用这种方法,但对我来说,考虑到 IDE 集成和类型安全性,这是完全有意义的。
Eclipse's message bundle implementation (used by plugins for example) integrates with the Externalize Strings feature and generates both a static class and a resource properties file for your strings:
http://www.eclipse.org/eclipse/platform-core/documents/3.1/message_bundles.html
For this integration to work Eclipse needs to see org.eclipse.osgi.util.NLS on the class path. From memory, the dependencies of the libraries it was available in were a little tricky for the project I used this approach in, so I just got the source and have it as a stand-alone class in my core module (see the comments for more on that).
It provides the type safety you're looking for and the IDE features save a lot of time. I've found no downsides to the approach so far.
Edit: this is actually what ghostbust555 mentioned in the comments, but not clear in that article that this isn't limited to Eclipse plugins and you refer to your resources via static members of a messages class.
I haven't seen any mention of others using this approach with their own applications, but to me it makes complete sense given the IDE integration and type safety.
我不确定这是否是您的意思,但请查看国际化 - http ://netbeans.org/kb/docs/java/gui-automatic-i18n.html
I'm not sure if this is what you mean but check out internationalization- http://netbeans.org/kb/docs/java/gui-automatic-i18n.html
您是否正在寻找能够解析 XML 文件并生成类似“类似结构”对象的 Java 实例的工具,例如 JAXP 和 JAXB?
Are you looking for something that parses XML files and generates Java instances of similar "struct-like" objects, like JAXP, and JAXB?
我遇到了 ResGen,它给定资源包 XML 文件,生成可用于以类型安全方式访问资源的 Java 文件。
http://eigenbase.sourceforge.net/resgen/
I came across ResGen which, given resource bundle XML files generates Java files that can be used to access the resources in a type-safe way.
http://eigenbase.sourceforge.net/resgen/