當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaScript ArcGIS intl.registerMessageBundleLoader用法及代碼示例

基本信息

以下是所在類或對象的基本信息。

AMD: require(["esri/intl"], (intl) => { /* code goes here */ });

ESM: import * as intl from "@arcgis/core/intl";

對象: esri/intl

自從:用於 JavaScript 4.12 的 ArcGIS API

用法說明

intl.registerMessageBundleLoader函數(或屬性)的定義如下:

registerMessageBundleLoader (loader) {Object}


自從:ArcGIS 適用於 JavaScript 4.18 的 API

注冊消息加載器以加載翻譯字符串所需的指定消息包。需要在獲取應用程序的消息包之前調用此方法。

有兩個選項可用於創建所需的 MessageBundleLoader 參數。

  1. 通過實現 MessageBundleLoader 來定義您自己的加載器,或者
  2. 使用 API 通過便捷方法實現的加載器 createJSONLoader

下麵提供了兩者的示例。

參數:

類型說明

消息包加載器。

返回:

類型 說明
Object 返回一個帶有remove()應調用以注銷消息加載器的方法。
屬性 類型 說明
remove Function 調用時,注銷消息加載器。

例子:

// This example defines a loader by implementing a MessageBundleLoader.

// Register a loader that loads bundles with the specified bundle identifier
// starting with "my-application\/"

const patternBundle = /^my-application\/(.+)/g;

intl.registerMessageBundleLoader({
  pattern: patternBundle,
  // Calls the FetchMessageBundle function of the loader, passing in the bundle identifier and locale
  async fetchMessageBundle(bundleId, locale) {
    // extract the filename from the bundle id.
    const filename = pattern.exec(bundleId)[1];
    // normalize the locale, e.g. 'fr-FR' > 'fr'
    const knownLocale = intl.normalizeMessageBundleLocale(locale);
    // Fetch the corresponding JSON file given the specified url path
    const response = await fetch(new Url(`./assets/translations/${filename}_${knownLocale}.json`, window.location.href));
    return response.json();
  }
});

// If the locale changes, calling fetchMessageBundle resolves to the new localized message bundle.
intl.onLocaleChange((newLocale) => {
  const bundle = await intl.fetchMessageBundle("my-application/translations/MyBundle");
  // loads file: "https://myserver/my-application/translations/MyBundle_fr.json"
});

// Updates the locale
intl.setLocale("fr");
// This example defines the loader via the createJSONLoader method.

// Register a loader that loads bundles with the specified bundle identifier
// starting with "my-application\/"

const patternBundle = /^my-application\/(.+)/g;

intl.registerMessageBundleLoader(
  intl.createJSONLoader({
    pattern: patternBundle,
    base: "my-application/",
    location: new URL("./assets/", window.location.href)
  })
);

// If the locale changes, calling fetchMessageBundle resolves to the new localized message bundle.
intl.onLocaleChange((newLocale) => {
  const bundle = await intl.fetchMessageBundle("my-application/MyBundle");
 // loads file: "https://myserver/my-application/MyBundle_fr.json"
});

// Updates the locale
intl.setLocale("fr");

相關用法


注:本文由純淨天空篩選整理自arcgis.com大神的英文原創作品 intl.registerMessageBundleLoader。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。