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


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

基本信息

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

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

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

對象: esri/intl

自從:用於 JavaScript 4.12 的 ArcGIS API

用法說明

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

substitute (template, data, options) {String}


使用它來用參數 data 中的值替換 template 字符串中的鍵。 nullundefined 值不會打印在輸出結果中。

可以指定來自data 的值的格式。默認情況下,這些值將根據其原生 JavaScript 類型進行格式化。

在內部 substitute 為當前語言環境創建 Intl formatter 實例。格式化程序使用options 作為緩存鍵進行緩存。重複使用相同的 options 對象以獲得最佳性能。

參數:

類型說明
template String

用於替換的模板字符串。

data Object

要替換的數據對象。

可選的

用於確定如何替換模板字符串中的鍵的選項。

返回:

類型 說明
String 格式化的字符串。

例子:

// Format a date
const data = {
  increasedValue: 500,
  time: Date.UTC(2019),
}

const dateFormatOptions = intl.convertDateFormatToIntlOptions("short-date-short-time-24");

intl.substitute("Date: {time}", data, {
  format: {
    time: {
      type: "date",
      intlOptions: dateFormatOptions
    }
  }
});
// Format a number
const data = {
  increasedValue: 500,
  time: Date.UTC(2019),
}

intl.substitute("Number: {value}", data, {
  format: {
    value: {
      type: "number",
      intlOptions: {
        maximumFractionDigits: 21
      }
    }
  }
});
const data = {
  increasedValue: 500,
  time: Date.UTC(2019),
}

intl.substitute("Median income increased by {increasedValue} in {time:yearFormat}", data, {
  format: {
    increasedValue: {
      type: "number",
      intlOptions: {
        useGrouping: true,
        currency: "EUR",
        currencyDisplay: "symbol",
        maximumFractionDigits: 2
      }
    },
    yearFormat: {
      type: "date",
      intlOptions: {
        year: "numeric"
      }
    }
  }
});

相關用法


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