[英]How to read the Language.properties file in Liferay (outside of portlet)?
Say I have 3 modules in a Liferay project, A
, B
and language
.假设我在 Liferay 项目中有 3 个模块,
A
, B
和language
。 Module A
is a normal module with portlet in it.模块
A
是一个包含 portlet 的普通模块。 Module B
, however, is a portletless module, it is nothing more than a plain Java utility module.然而,模块
B
是一个无 portlet 模块,它只不过是一个普通的 Java 实用程序模块。 language
module is something like what is described in the "Localizing Your Application" article in Liferay's Help Center. language
模块类似于 Liferay 帮助中心的“本地化您的应用程序”一文中描述的内容。
Now, I have one Language.properties
file in each of these modules.现在,我在每个模块中都有一个
Language.properties
文件。 Language.properties
file in the language
module contains properties that are to be shared between modules A
and B
and Language.properties
file in modules A
and B
contains properties specific for each module. language
模块中的Language.properties
文件包含要在模块A
和B
之间共享的属性,模块A
和B
中的Language.properties
文件包含特定于每个模块的属性。
Now, this Help Center's article has defined what to do to enable reading from those files in a module.现在,这个帮助中心的文章已经定义了如何在模块中读取这些文件。 However, this article says that it is required to configure a portlet in the module where
Language.properties
file is located.但是,这篇文章说需要在
Language.properties
文件所在的模块中配置一个portlet。
That step may work for the module A
in my case, but for module B
I can not do this since module B
is portletless.在我的情况下,该步骤可能适用于模块
A
,但对于模块B
,我不能这样做,因为模块B
是无端口的。
Inside the module B
I tried reading the Language.properties
file with the following code: com.liferay.portal.kernel.language.LanguageUtil.get("locale", "key");
在模块
B
中,我尝试使用以下代码读取Language.properties
文件: com.liferay.portal.kernel.language.LanguageUtil.get("locale", "key");
However, this does not work.但是,这不起作用。 It does not find the keys which I put in the
B
's Language.properties
file.它没有找到我放在
B
的Language.properties
文件中的键。 However, interestingly enough, what it finds are keys from the language
's Language.properties
file.然而,有趣的是,它找到的是
language
的Language.properties
文件中的键。
So, surprisingly invoking get
method of the LanguageUtil
class inside the B
module works, just it reads unwanted Language.properties
file.因此,令人惊讶的是,在
B
模块中调用LanguageUtil
类的get
方法可以正常工作,只是它读取了不需要的Language.properties
文件。
My question here is, how can I configure Language Utilities to read the Language.properties
file from the portletless b
module instead of the language
common language module?我的问题是,如何配置 Language Utilities 以从 portletless
b
模块而不是language
公共语言模块读取Language.properties
文件?
There are several layers to unfold here:这里有几个层次要展开:
Liferay has pulled together almost all of its localization keys into a single module. Liferay 已将几乎所有的本地化键整合到一个模块中。 From a product perspective, this is great, as it allows central handling of overrides, avoids duplicates, and even enables a UI to change those common keys.
从产品的角度来看,这很棒,因为它允许集中处理覆盖,避免重复,甚至使 UI 能够更改这些常用键。
For custom extensions, centralization is not such a great thing, and you'd rather come with your own translations in your custom modules (or explicitly use the default ones).对于自定义扩展,集中化并不是一件好事,您宁愿在自定义模块中提供自己的翻译(或明确使用默认模块)。
Enter OSGi: With using LanguageUtil
, my understanding is that you're using the "current bundle" perspective, and LanguageUtil will only check Liferay's own central translation repository.输入 OSGi:使用
LanguageUtil
时,我的理解是您使用的是“当前包”透视图,并且 LanguageUtil 只会检查 Liferay 自己的中央翻译存储库。 What you can (and likely should) do is to utilize ResourceBundleUtil in such a way:您可以(并且可能应该)做的是以这种方式利用 ResourceBundleUtil :
ResourceBundle bundle = ResourceBundleUtil.getBundle(locale, this.getClass().getClassLoader());
String result = ResourceBundleUtil.getString(bundle, key, parameter);
Instead of this.getClass().getClassLoader()
, you can of course provide any other class' loader, but it should give you a hint.除了
this.getClass().getClassLoader()
,您当然可以提供任何其他类的加载器,但它应该给您一个提示。 This provides the bundle in which the translation is located.这提供了翻译所在的包。
Encapsulate this in a method that defaults back to any other localization that you'd like (could be another bundle, or Liferay's global one), and you'll find your translation.将其封装在一个默认返回为您想要的任何其他本地化的方法中(可能是另一个包,或 Liferay 的全局包),您将找到您的翻译。
Of course, you can also deploy a plugin that adds to Liferay's own translations, but personally, I prefer not to go that route.当然,您也可以部署一个插件来添加到 Liferay 自己的翻译中,但就我个人而言,我更喜欢不走那条路。 There's always potential to run into conflicts with other modules that try to translate the same key differently, or for Liferay to introduce the same key at some point in time (and it doesn't play well with an individual portlet's translation)
总是有可能与尝试以不同方式翻译相同密钥的其他模块发生冲突,或者 Liferay 在某个时间点引入相同的密钥(并且它不能很好地与单个 portlet 的翻译配合使用)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.