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


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

基本信息

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

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

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

對象: esri/intl

自從:用於 JavaScript 4.12 的 ArcGIS API

用法說明

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

createJSONLoader (params) {MessageBundleLoader}


自從:ArcGIS 適用於 JavaScript 4.18 的 API

創建一個消息包加載器,專門用於將翻譯文件加載為 JSON 文件。在內部,這是用於 JavaScript 的ArcGIS API 加載語言環境的加載器。

參數:

規格:
類型說明
params Object

裝載機的配置。

規格:
pattern

String|RegExp

用於檢查是否應該使用加載器來加載候選人的消息包。

base

String

用於計算要加載的文件的相對路徑。

location

String|URL|Function

翻譯文件的位置。它可以是指向文件所在文件夾的stringURL,也可以是使用指定路徑調用的函數。該函數應返回最終路徑。

返回:

類型 說明
intl.MessageBundleLoader loader - 消息包加載器。

例子:

// Assume the following directory structure
src/
  assets/
    translations/
      MyBundle.json
      MyBundle_fr.json
  widgets/
    MyWidget.ts

// Configure the message bundle loader given the directory structure noted above

const loader = intl.createJSONLoader({
  pattern: "my-application/", // The pattern is used to match the string in `intl.fetchMessageBundle("my-application/translations/MyBundle")`
  base: "my-application", // This removes the base, ie. "translations/MyBundle"
  location: new Url("./assets/", window.location.href) // Add the location, ie. "assets/translations/MyBundle"
});

// This loads file, "./assets/translations/MyBundle.json" or
// "./assets/translations/MyBundle_en.json" (unless locale is updated to something, e.g. like `fr`).

// Register the message bundle created from the createJSONLoader method
intl.registerMessageBundleLoader(loader);

// Fetches the message bundle, "./assets/translations/MyBundle.json"
const bundle = await intl.fetchMessageBundle("my-application/MyBundle");

// If no `base` property is specified for the loader method, the assets would read as,
src/
  assets/
    my-application/
      translations/
        MyBundle.json
        MyBundle_en.json
        MyBundle_fr.json

// The method would load file, "./assets/my-application/translations/MyBundle.json" or
// "./assets/my-application/translations/MyBundle_en.json" (unless locale is updated to something, e.g. like `fr`).

相關用法


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