如何使用 Eclipse 国际化 OSGi 应用程序?

How do I internationalize an OSGi application using Eclipse?

我正在尝试使用 "OSGi way" 国际化 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 source

@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 添加对更多语言的支持,因此解决方案应该可以通过这种方式进行扩展。


我已经阅读了所有这些内容,但没有发现任何一致的内容对我有帮助。

此外,OSGi Alliance Tutorial Archive nor the OSGi enRoute 中也没有任何相关内容。

环境:

PS: 我确定这不是一项复杂的任务,只是我还没有找到任何有用的(对我而言)文档。

理论知识

本地化1

捆绑包本地化条目共享一个通用的基本名称。为了找到一个潜在的本地化条目,添加了一个下划线 ('_' \u005F) 加上一些后缀,由另一个下划线分隔,最后附加后缀 .properties。后缀在 java.util.Locale 中定义。后缀的顺序必须是:

  • 语言

  • 国家

  • 变体

例如,以下文件提供英语、荷兰语(比利时和荷兰)和瑞典语的清单翻译。

OSGI-INF/l10n/bundle_en.properties
OSGI-INF/l10n/bundle_nl_BE.properties
OSGI-INF/l10n/bundle_nl_NL.properties
OSGI-INF/l10n/bundle_sv.properties

清单本地化 2

本地化值存储在包内的 属性 资源中。捆绑包本地化 属性 文件的默认基本名称是 OSGI-INF/l10n/bundleBundle-Localization 清单 header 可用于覆盖本地化文件的默认基本名称。这个位置是相对于包和包片段的根。

一个本地化条目包含 key/value 个本地化信息条目。捆绑清单中的所有 header 都可以本地化。但是,框架必须始终使用具有框架语义的 non-localized 版本的 header。

可以使用以下语法将本地化密钥指定为捆绑包清单 header 的值:

header-value ::= '%'text
text ::= < any value which is both a valid manifest headervalue
   and a valid property key name >

例如,考虑以下捆绑包清单条目:

Bundle-Name: %acme bundle
Bundle-Vendor: %acme corporation
Bundle-Description: %acme description
Bundle-Activator: com.acme.bundle.Activator
Acme-Defined-Header: %acme special header

User-defined headers也可以本地化。本地化密钥中的空格是明确允许的。

前面的示例清单条目可以通过清单本地化条目中的以下条目进行本地化 OSGI-INF/l10n/bundle.properties

# bundle.properties
acme\ bundle=The ACME Bundle
acme\ corporation=The ACME Corporation
acme\ description=The ACME Bundle provides all of the ACME\ services
acme\ special\ header=user-defined Acme Data

实践

1. 首先,让我们创建捆绑文件,其中将包含 key/value 对。在这种情况下,一个用于英语 (bundle.properties),这将是默认的一个,一个用于西班牙语 (bundle_es.properties)

...
OSGI-INF
   |- l10n
        |- bundle.properties
        |- bunlde_es.properties
   |- ...

... 将包含我们之前的 hard-coded 字符串值。

#bundle.properties
startMessage = Yeah ha, I'm logging!
endMessage = Done, I'm finishing logging!

#bundle_es.properties
startMessage = Si, Estoy registrando logs!
endMessage = Terminado, He concluido de registrar logs!

2. 现在让我们创建一个实用程序组件,它将帮助我们根据语言环境获取与每个键关联的值。

src
   ...
   |- org.example.i18n.messages
                              |- MessageProvider
                              |- MessagesProviderImpl
   ...

有两个文件:接口和实际实现,后者包含获取 key/value 对的逻辑。

public interface MessageProvider {
    String get(String key);
}

@Component
public class MessagesProviderImpl implements MessageProvider {
    private BundleLocalization bundleLocalization;
    private LocaleProvider localeProvider;
    private ResourceBundle resourceBundle;

    @Reference
    public void bindBundleLocalization(BundleLocalization bundleLocalization) {
        this.bundleLocalization = bundleLocalization;
    }

    @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
    public void bindLocaleProvider(LocaleProvider localeProvider) {
        this.localeProvider = localeProvider;
        setResourceBundle()
    }

    /*unbind methods omitted*/

    @Activate
    public void activate() {
        setResourceBundle();
    }

    @Override
    public String get(String key) {
        return resourceBundle.getString(key);
    }

    private String getLocale() {
        return localeProvider != null ? localeProvider.getLocale().toString() : Locale.getDefault().toString();
    }

    private void setResourceBundle() {
        resourceBundle = bundleLocalization.getLocalization(FrameworkUtil.getBundle(getClass()), getLocale());
    }
}

3.SimpleLoggingComponent.

中使用MessageProvider组件
@Component
public class SimpleLoggingComponent {

    /*previously code omitted for brevity*/

    private MessageProvider messages;

    @Reference
    public void bindMessageProvider(MessageProvider messageProvider) {
        messages = messageProvider;
    }

    /*unbind methods omitted*/

    @Activate
    public void activate() {
        simpleLogService.log(messages.get("startMessage")); // <- use now the key: startMessage
    }

    @Deactivate
    public void deactivate() {
        simpleLogService.log(messages.get("endMessage")); // <- use now the key: endMessage
    }
}

使用自定义语言启动应用程序

为此,在“参数”选项卡上使用运行时参数 -nl,例如-nl en


参考资料

完整源代码