当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。