GT ClassMethodsLocales

isSameLanguage

GT isSameLanguage 方法的 API 参考

概览

isSameLanguage 方法用于检查多个 BCP-47 语言代码是否属于同一基础语言,忽略地区和书写体系的差异。 这对于判断语言兼容性以及按语言族对内容进行分组很有用。


参考资料

参数

Prop

Type

返回值

boolean - 当所有语言代码表示相同的基础语言时返回 true


示例

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


// 相同语言,不同地区
console.log(gt.isSameLanguage('en-US', 'en-GB')); // true
console.log(gt.isSameLanguage('es-ES', 'es-MX')); // true
console.log(gt.isSameLanguage('zh-CN', 'zh-TW')); // true

// 不同语言
console.log(gt.isSameLanguage('en-US', 'es-ES')); // false
console.log(gt.isSameLanguage('fr-FR', 'de-DE')); // false

// 基础语言及其变体
console.log(gt.isSameLanguage('en', 'en-US', 'en-GB')); // true

注意事项

  • 仅比较基础语言代码(第一个连字符之前)
  • 忽略区域、文字脚本和变体差异
  • 对基于语言的内容组织至关重要
  • 支持可变数量的 locale 参数

后续步骤

这份指南怎么样?

isSameLanguage