亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Eclipse 國際化 OSGi 應用程序?

如何使用 Eclipse 國際化 OSGi 應用程序?

皈依舞 2022-11-02 16:50:39
我正在嘗試使用“OSGi 方式”將 OSGi 應用程序國際化,但我沒有取得進展。通過OSGi 的方式,我的意思是,使用框架為其提供的功能。我之前已經將 Java 應用程序國際化,但我想知道如何將其作為OSGi應用程序來實現。我創建了這個簡單的演示[GitHub repo],它旨在創建一個捆綁包,一旦它被激活就會記錄一條消息,一旦它被停用就會記錄另一條消息。項目結構:src   |- org.example.i18n                     |- SimpleLoggingComponent   // where the actual strings are                     |- SimpleLogService                     |- SimpleLogServiceImplMETA-INF   |- MANIFEST.MFOSGI-INF   |- org.example.i18n.SimpleLoggingComponent   |- org.example.i18n.SimpleLogServiceImplbuild.propertiesSimpleLoggingComponent源碼@Componentpublic 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添加對更多語言的支持,因此該解決方案應該可以通過這種方式進行擴展。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

理論知識

本地化1

Bundle 本地化條目共享一個通用的基本名稱。為了找到一個潛在的本地化條目,添加一個下劃線 ('_' \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-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

用戶定義的標題也可以本地化。本地化鍵中的空格是明確允許的。


前面的示例清單條目可以通過清單本地化條目中的以下條目進行本地化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.首先,讓我們創建捆綁文件,這些文件將包含鍵/值對。在這種情況下,一種用于英語 ( bundle.properties),這將是默認的一種,另一種用于西班牙語 ( bundle_es.properties)


...

OSGI-INF

   |- l10n

        |- bundle.properties

        |- bunlde_es.properties

   |- ...

...這將包含我們之前硬編碼的字符串值。


#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

   ...

有兩個文件:接口和實際實現,它包含獲取鍵/值對的邏輯。


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.使用中的MessageProvider組件SimpleLoggingComponent。


@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

    }

}

使用自定義語言啟動應用程序

在 Arguments 選項卡上-nl,為此目的使用 runtime 參數,例如-nl en

http://img1.sycdn.imooc.com//63622f990001b4fd11880280.jpg

查看完整回答
反對 回復 2022-11-02
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號