我正在尝试使用“OSGi 方式”将 OSGi 应用程序国际化,但我没有取得进展。通过OSGi 的方式,我的意思是,使用框架为其提供的功能。我之前已经将 Java 应用程序国际化,但我想知道如何将其作为OSGi应用程序来实现。
我创建了这个简单的演示[GitHub repo],它旨在创建一个捆绑包,一旦它被激活就会记录一条消息,一旦它被停用就会记录另一条消息。
项目结构:
src
|- org.example.i18n
|- SimpleLoggingComponent // where the actual strings are
|- SimpleLogService
|- SimpleLogServiceImpl
META-INF
|- MANIFEST.MF
OSGI-INF
|- org.example.i18n.SimpleLoggingComponent
|- org.example.i18n.SimpleLogServiceImpl
build.properties
SimpleLoggingComponent源码
@Component
public class SimpleLoggingComponent {
private SimpleLogService simpleLogService;
@Reference
public void bindLogger(SimpleLogService logService) {
this.simpleLogService = logService;
}
public void unbindLogger(SimpleLogService logService) {
this.simpleLogService = null;
}
@Activate
public void activate() {
if (simpleLogService != null) {
simpleLogService.log("Yee ha, I'm logging!"); // <-- need this message internationalized
}
}
@Deactivate
public void deactivate() {
if (simpleLogService != null) {
simpleLogService.log("Done, I'm finishing logging!"); // <-- need this message internationalized
}
}
}
目前,字符串在代码中是固定的,我希望能够将它们国际化。假设支持英语和西班牙语。
稍后我计划通过Fragment Bundles添加对更多语言的支持,因此该解决方案应该可以通过这种方式进行扩展。
神不在的星期二
相关分类