Intl.DisplayNamesで言語,地域,文字,通貨名を翻訳する
使い方
1. new Intl.DisplayNames(locales, options)でtypeを指定する
new Intl.DisplayNames(['ja-jp'], { type: 'language' });みたいな感じ
2. Intl.DisplayNames.prototype.of(code)にコードを渡す
引数で取り得るコードの値はコンストラクタで渡したtypeによって変わる
type が "region" である場合、コードは ISO-3166 地域の 2 文字コードまたは three digits UN M49 Geographic Regions である必要があります。
type が "script" である場合、コードは ISO-15924 文字体系の 4 文字コードである必要があります。
type が "language" である場合、コードは languageCode scriptCode regionCode *("-" variant ) の形である必要があり、これは UTS 35's Unicode Language and Locale Identifiers grammar の unicode_language_id の構文です。 languageCode は 2 文字の ISO 639-1 言語コードまたは 3 文字の ISO 639-2 言語コードです。
type が "currency" である場合、コードは 3 文字の ISO 4217 通貨コードである必要があります。
言語を翻訳する (language)
こんなコードを書いて実行
code:languages.js
const languageNamesInJapanese = new Intl.DisplayNames('ja-jp', { type: 'language' }); const languageNamesInEnglish = new Intl.DisplayNames('en-us', { type: 'language' }); const languageNamesInKorean = new Intl.DisplayNames('ko', { type: 'language' }); languages.forEach(language => {
console.log(languageNamesInJapanese.of(language));
console.log(languageNamesInEnglish.of(language));
console.log(languageNamesInKorean.of(language));
console.log('')
});
実行結果
code:実行結果(txt)
日本語
Japanese
일본어
日本語 (日本)
Japanese (Japan)
일본어(일본)
英語
English
영어
アメリカ英語
American English
영어(미국)
韓国語
Korean
한국어
https://gyazo.com/d88f7191c7719774030163f0cf6593e3
後ろの国コードも考慮してくれる
地域を翻訳する (region)
こんなコードを書いて実行
code:regions.js
const regionNamesInJapanese = new Intl.DisplayNames('ja-jp', { type: 'region' }); const regionNamesInEnglish = new Intl.DisplayNames('en-us', { type: 'region' }); const regionNamesInKorean = new Intl.DisplayNames('ko', { type: 'region' }); regions.forEach(region => {
console.log(regionNamesInJapanese.of(region));
console.log(regionNamesInEnglish.of(region));
console.log(regionNamesInKorean.of(region));
console.log('')
});
実行結果
code:実行結果(txt)
日本
Japan
일본
アメリカ合衆国
United States
미국
韓国
South Korea
대한민국
https://gyazo.com/7d3cae54eeb22cd1763121c3a487258f
文字を翻訳する (script)
こんなコードを書いて実行
code:scripts.js
const scriptNamesInJapanese = new Intl.DisplayNames('ja-jp', { type: 'script' }); const scriptNamesInEnglish = new Intl.DisplayNames('en-us', { type: 'script' }); const scriptNamesInKorean = new Intl.DisplayNames('ko', { type: 'script' }); scripts.forEach(script => {
console.log(scriptNamesInJapanese.of(script));
console.log(scriptNamesInEnglish.of(script));
console.log(scriptNamesInKorean.of(script));
console.log('')
});
実行結果
code:実行結果(txt)
日本語の文字
Japanese
일본 문자
ひらがな
Hiragana
히라가나
カタカナ
Katakana
가타카나
仮名
Japanese syllabaries
가나
https://gyazo.com/73bb10654ddae7290431fc8dae8b1b41
通貨を翻訳する (currency)
こんなコードを書いて実行
code:currencies.js
const currencyNamesInJapanese = new Intl.DisplayNames('ja-jp', { type: 'currency' }); const currencyNamesInEnglish = new Intl.DisplayNames('en-us', { type: 'currency' }); const currencyNamesInKorean = new Intl.DisplayNames('ko', { type: 'currency' }); currencies.forEach(currency => {
console.log(currencyNamesInJapanese.of(currency));
console.log(currencyNamesInEnglish.of(currency));
console.log(currencyNamesInKorean.of(currency));
console.log('')
});
実行結果
code:実行結果(txt)
日本円
Japanese Yen
일본 엔화
米ドル
US Dollar
미국 달러
韓国ウォン
South Korean Won
대한민국 원
https://gyazo.com/a980f3e8f4c4c6f0224e0a9507543b31
参考