Intl.RelativeTimeFormat.prototype.resolvedOptions()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年9月.
resolvedOptions() は Intl.RelativeTimeFormat のメソッドで、この RelativeTimeFormat オブジェクトの初期化時に計算されたロケールや日時や書式化オプションを反映したプロパティを持つ新しいオブジェクトを返します。
試してみましょう
const rtf1 = new Intl.RelativeTimeFormat("ja", { style: "narrow" });
const options1 = rtf1.resolvedOptions();
const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
const options2 = rtf2.resolvedOptions();
console.log(`${options1.locale}, ${options1.style}, ${options1.numeric}`);
// 予想される結果: "ja, narrow, always"
console.log(`${options2.locale}, ${options2.style}, ${options2.numeric}`);
// 予想される結果: "es, long, auto"
構文
resolvedOptions()
引数
なし。
返値
この RelativeTimeFormat オブジェクトの初期化中に計算されたオプションを反映したプロパティを持つ、新しいオブジェクト。このオブジェクトは、以下の一覧の順序でプロパティを持ちます。
locale-
実際に使用されるロケールのBCP 47 言語タグであり、ロケールネゴシエーションプロセスによって決まります。リクエストされた場合、
nuの Unicode 拡張キーのみが出力結果に含まれる可能性があります。 style-
options引数でこのプロパティに指定された値です。必要に応じてデフォルト値が設定されます。値は"long","short","narrow"のいずれかです。デフォルト値は"long"です。 numeric-
options引数でこのプロパティに指定された値です。必要に応じてデフォルト値が設定されます。値は"always"または"auto"のどちらかです。デフォルトは"always"です。 numberingSystem-
options引数で指定された値、または Unicode 拡張キー"nu"を使用して指定された値です。必要に応じてデフォルト値が設定されます。これは、このロケールで対応している記数法です。デフォルト値はロケールによって異なります。
例
>resolvedOptions() メソッドの使用
const de = new Intl.RelativeTimeFormat("de-DE");
const usedOptions = de.resolvedOptions();
usedOptions.locale; // "de-DE"
usedOptions.style; // "long"
usedOptions.numeric; // "always"
usedOptions.numberingSystem; // "latn"
仕様書
| Specification |
|---|
| ECMAScript® 2027 Internationalization API Specification> # sec-intl.relativetimeformat.prototype.resolvedoptions> |