as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持

MessageFormatClassic

MessageFormatClassic

MessageFormatClassic API接受用经典的ICU MessageFormat模式来格式化消息。

MessageFormatClassic API支持模式中的命名参数样式和编号参数样式。使用命名参数样式通过提供描述性命名参数来改善开发者和翻译者的可读性。此外,这可以更好地推断出模式中的参数内包含哪种值。

示例

import { MessageFormatClassic } from '@amazon-devices/keplerscript-kepleri18n-lib';
...
    // 从字符串资源中检索模式。为了简化示例,在此对其进行了硬编码。
    const pattern: string = "车号 {carNumber} 赢了! 恭喜您,{driverName}!";
    const carNumber: number = 3;
    const driverName: string = "Alice";
    const argMap = new Map<string, number | string | Date>();
    argMap.set("carNumber", carNumber);
    argMap.set("driverName", driverName);
    const message: string | null = MessageFormatClassic.format(pattern, argMap);
    if (message === null) {
        // 错误
        return;
    }
    // 消息是 "3号车赢了! 恭喜您,Alice!"。
import { MessageFormatClassic } from '@amazon-devices/keplerscript-kepleri18n-lib';
...
    // 从字符串资源中检索模式。为了简化示例,在此对其进行了硬编码。
    const pattern: string = "车号 {0} 赢了! 恭喜您,{1}!";
    const carNumber: number = 11;
    const driverName: string = "Bob";
    const message: string | null = MessageFormatClassic.formatPositional(pattern, carNumber, driverName);
    if (message === null) {
        // 错误
        return;
    }
    // 消息是"车号11赢了! 恭喜您,Bob!"。

构造函数

new MessageFormatClassic()

new MessageFormatClassic(): MessageFormatClassic

返回值

MessageFormatClassic

方法

format()

static format(pattern, args): null | string

在默认区域设置中使用命名参数格式化指定模式。

参数

pattern

string

经典的消息格式模式。

args

Map<string, string | number | Date>

将参数名称映射到其值。

返回值

null | string

成功时的格式化消息,失败时为null。


formatPositional()

static formatPositional(pattern, …args): null | string

在默认区域设置中使用编号参数格式化指定模式。

参数

pattern

string

经典的消息格式模式。

args

…(string | number | Date)[]

参数值数组

返回值

null | string

成功时的格式化消息,失败时为null。


formatPositionalWithLocale()

static formatPositionalWithLocale(pattern, locale, …args): null | string

在指定区域设置中使用编号参数格式化指定模式。

参数

pattern

string

经典的消息格式模式。

locale

BCP-47区域设置字符串。如果此参数为null或空字符串,则使用默认区域设置。

null string
args

…(string | number | Date)[]

参数值数组

返回值

null | string

成功时的格式化消息,失败时为null。


formatWithLocale()

static formatWithLocale(pattern, locale, args): null | string

在指定区域设置中使用命名参数格式化指定模式。

参数

pattern

string

经典的消息格式模式。

locale

BCP-47区域设置字符串。如果此参数为null或空字符串,则使用默认区域设置。

null string
args

Map<string, string | number | Date>

将参数名称映射到其值。

返回值

null | string

成功时的格式化消息,失败时为null。


getMajorVersion()

static getMajorVersion(): number

获取主版本号。

返回值

number

主版本号。


getMinorVersion()

static getMinorVersion(): number

获取次版本号。

返回值

number

次版本号。


getPatchVersion()

static getPatchVersion(): number

获取补丁版本号。

返回值

number

补丁版本号。


Last updated: 2025年9月29日