简体   繁体   English

使用ResourceBundle仅读取一些属性

[英]read only some properties using ResourceBundle

I have a big file with messages specified in locale. 我有一个很大的文件,其中包含在区域设置中指定的消息。 For example: 例如:

book.registration.error.service = ..
book.registration.error.empty.book = ..
book.registration.error.file.format.book = ..

publisher.register.firstpublisher = ..
publisher.register.publisherNameUa = ..
publisher.register.nameRu = ..
publisher.register.NameEn = ..

There is a lot of messages. 有很多消息。 To read them I use 要阅读它们,我使用

java.util.ResourceBundle.getBundle(messagesLocation, locale)

but with whis i will read ALL messages. 但是我会读所有消息。 Is there any way to read only a piace of messages. 有什么办法只能读取大量消息。 For example read only messages which start with 'book'? 例如,仅读取以“ book”开头的消息?

The best way to accomplish what you want is to break up your very large resource bundle into multiple bundles - and put all the "book" ones into a specific bundle. 实现所需目标的最佳方法是将非常大的资源捆绑包分成多个捆绑包,然后将所有“书本”捆绑到一个特定的捆绑包中。

You could parse the file yourself, but you'd still have to essentially read through the entire file (unless it's sorted), but that seems to defeat the purpose of what you're trying to achieve. 您可以自己解析文件,但实际上您仍然必须通读整个文件(除非已排序),但这似乎违反了您要实现的目的。

Typically you'd only go down this road if you're having memory or performance problems with a single bundle - if it's memory problems, breaking up bundles might help; 通常,只有在单个捆绑软件遇到内存或性能问题时,才需要走这条路–如果是内存问题,拆开捆绑软件可能会有所帮助; if it's performance problems, initializing your bundle on a background thread may help. 如果是性能问题,则在后台线程上初始化包可能会有所帮助。

And in case it's not obvious - you should always cache the bundle! 如果情况不太明显-您应该始终缓存该捆绑包! Re-creating your bundle every time is a waste - if you want to support dynamically changing resources, just invalidate the cache when resources actually change. 每次都重新创建捆绑包是一种浪费-如果您想支持动态更改资源,只需在实际更改资源时使缓存无效即可。

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

相关问题 在Javascript中使用Resourcebundle属性 - Using Resourcebundle Properties in Javascript 资源束vs属性 - resourcebundle vs properties 使用ResourceBundle的Java本地化文本Web服务(locale.properties) - Java Localized text Web services using ResourceBundle(locale.properties) 使用ResourceBundle将属性文件从一种语言翻译成另一种语言 - Translate the Properties File from one language to another using ResourceBundle 从JFileChooser获取file.properties并在ResourceBundle上使用它 - getting file.properties from JFileChooser and using it on ResourceBundle ResourceBundle-无法解决文件系统中不存在属性文件的读取方式 - ResourceBundle - Cannot work out how a properties file is read when it does not exist in the filesystem 为什么不使用ResourceBundle而不是Properties? - Why not use ResourceBundle instead of Properties? 从 ResourceBundle 生成属性 object? - Generate a Properties object from a ResourceBundle? 如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们? - How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray? 如何在使用ResourceBundle API获取的属性文件中转义花括号 - How to escape curly braces in my properties file which is fetched using the ResourceBundle API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM