GT ClassMethodsLocales

getLocaleProperties

GT getLocaleProperties 方法的 API 参考

概览

getLocaleProperties 方法用于获取指定语言代码的完整属性,包含 显示名称、地区代码、书写系统信息以及表情符号旗帜等详细信息。 它返回一个完整的 LocaleProperties 对象,提供构建丰富的国际化用户界面所需的全部数据。

const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'es'
});

const props = gt.getLocaleProperties('fr-CA');
console.log(props.name); // "法语(加拿大)"
console.log(props.nativeName); // "français(Canada)"
console.log(props.emoji); // "🇨🇦"

参考资料

参数

Prop

Type

参数说明

参数说明
locale用于获取属性的 BCP-47 语言代码。若未提供,则使用该实例的 targetLocale

返回值

LocaleProperties - 一个包含所有语言环境信息的完整对象:

  • code: 标准化的语言代码
  • name: 源语言环境中的显示名称
  • nativeName: 该语言环境内的显示名称
  • languageCode, languageName, nativeLanguageName: 语言信息
  • regionCode, regionName, nativeRegionName: 地区信息
  • scriptCode, scriptName, nativeScriptName: 文字脚本信息
  • maximizedCode, minimizedCode: 规范形式
  • nameWithRegionCode, nativeNameWithRegionCode: 合并显示格式
  • emoji: 旗帜或代表性表情符号

抛出

  • Error - 当未提供 locale 且该实例未配置 targetLocale

示例

基本用法

const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'fr'
});

// 获取 targetLocale 的属性
const props = gt.getLocaleProperties();
console.log(props.name); // "法语(法国)"
console.log(props.nativeName); // "français(France)"
console.log(props.languageCode); // "fr"
console.log(props.regionCode); // "FR"
console.log(props.emoji); // "🇫🇷"

// 获取其他 locale 的属性
const germanProps = gt.getLocaleProperties('de-AT');
console.log(germanProps.name); // "奥地利德语"
console.log(germanProps.nativeName); // "Österreichisches Deutsch"
console.log(germanProps.regionName); // "奥地利"
console.log(germanProps.nativeRegionName); // "Österreich"

备注

  • 所有显示名称均遵循该实例的“源语言(sourceLocale)”设置
  • 自定义映射属性的优先级高于标准 Intl APIs

后续步骤

本指南如何?