第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Eclipse 國際化 OSGi 應(yīng)用程序?

如何使用 Eclipse 國際化 OSGi 應(yīng)用程序?

皈依舞 2022-11-02 16:50:39
我正在嘗試使用“OSGi 方式”將 OSGi 應(yīng)用程序國際化,但我沒有取得進(jìn)展。通過OSGi 的方式,我的意思是,使用框架為其提供的功能。我之前已經(jīng)將 Java 應(yīng)用程序國際化,但我想知道如何將其作為OSGi應(yīng)用程序來實(shí)現(xiàn)。我創(chuàng)建了這個(gè)簡(jiǎn)單的演示[GitHub repo],它旨在創(chuàng)建一個(gè)捆綁包,一旦它被激活就會(huì)記錄一條消息,一旦它被停用就會(huì)記錄另一條消息。項(xiàng)目結(jié)構(gòu):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        }    }}目前,字符串在代碼中是固定的,我希望能夠?qū)⑺鼈儑H化。假設(shè)支持英語和西班牙語。稍后我計(jì)劃通過Fragment Bundles添加對(duì)更多語言的支持,因此該解決方案應(yīng)該可以通過這種方式進(jìn)行擴(kuò)展。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

理論知識(shí)

本地化1

Bundle 本地化條目共享一個(gè)通用的基本名稱。為了找到一個(gè)潛在的本地化條目,添加一個(gè)下劃線 ('_' \u005F) 和一些后綴,用另一個(gè)下劃線分隔,最后附加后綴.properties. 后綴定義在java.util.Locale. 后綴的順序必須是:

  • 國家

  • 變體

例如,以下文件提供英語、荷蘭語(比利時(shí)和荷蘭)和瑞典語的清單翻譯。

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

本地化值存儲(chǔ)在包內(nèi)的屬性資源中。包本地化屬性文件的默認(rèn)基本名稱是OSGI-INF/l10n/bundleBundle-Localization清單標(biāo)頭可用于覆蓋本地化文件的默認(rèn)基本名稱。此位置相對(duì)于捆綁包和捆綁包片段的根。

本地化條目包含本地化信息的鍵/值條目。捆綁包清單中的所有標(biāo)頭都可以本地化。但是,框架必須始終使用具有框架語義的標(biāo)頭的非本地化版本。

可以使用以下語法將本地化鍵指定為包的清單標(biāo)頭的值:


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

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


前面的示例清單條目可以通過清單本地化條目中的以下條目進(jìn)行本地化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

在實(shí)踐中

1.首先,讓我們創(chuàng)建捆綁文件,這些文件將包含鍵/值對(duì)。在這種情況下,一種用于英語 ( bundle.properties),這將是默認(rèn)的一種,另一種用于西班牙語 ( 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.現(xiàn)在讓我們創(chuàng)建一個(gè)實(shí)用組件,它將幫助我們根據(jù)語言環(huán)境獲取與每個(gè)鍵關(guān)聯(lián)的值。


src

   ...

   |- org.example.i18n.messages

                              |- MessageProvider

                              |- MessagesProviderImpl

   ...

有兩個(gè)文件:接口和實(shí)際實(shí)現(xiàn),它包含獲取鍵/值對(duì)的邏輯。


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

    }

}

使用自定義語言啟動(dòng)應(yīng)用程序

在 Arguments 選項(xiàng)卡上-nl,為此目的使用 runtime 參數(shù),例如-nl en

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

查看完整回答
反對(duì) 回復(fù) 2022-11-02
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)