简体   繁体   English

Intil.DisplayNames() 在 Safari 版本 < 14.1 中未定义

[英]Intil.DisplayNames() undefined in Safari version < 14.1

The Intl.Displaynames() constructor enables the translation of language, region and script display names in Javascript. Intl.Displaynames()构造函数启用 Javascript 中的语言、区域和脚本显示名称的翻译。

Unfortunately, this is not supported in Safari versions < 14.1.不幸的是,Safari 版本 < 14.1. (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames#browser_compatibility ). (参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames#browser_compatibility )。

How to make this work in older Safari versions?如何使它在旧的 Safari 版本中工作?

We can use polyfills (see https://formatjs.io/docs/polyfills/intl-displaynames/ ) to make this work in older Safari versions.我们可以使用 polyfill(参见https://formatjs.io/docs/polyfills/intl-displaynames/ )使它在旧的 Safari 版本中工作。

Add the following polyfills to your code to make the Intl.DisplayNames() constructor work as expected:将以下 polyfill 添加到您的代码中,以使Intl.DisplayNames()构造函数按预期工作:

// the actual polyfills:
require('@formatjs/intl-locale/polyfill');
require('@formatjs/intl-displaynames/polyfill');
// the locale data of the languages that you need to support:
require('@formatjs/intl-displaynames/locale-data/de');
require('@formatjs/intl-displaynames/locale-data/en');

This requires the following dependencies:这需要以下依赖项:

"@formatjs/intl-displaynames"
"@formatjs/intl-getcanonicallocales"
"@formatjs/intl-locale"

After that, the constructor should be polyfilled and is ready to use.之后,构造函数应该被 polyfilled 并准备好使用。 Eg:例如:

const translatedRegionNames = new Intl.DisplayNames(['en'], {type: 'region'});
...
const translatedCountryName = translatedRegionNames.of('US')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM