diff --git a/CHANGES.md b/CHANGES.md index 3490684715..5adb988a3e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -63,6 +63,7 @@ * Ppx: allow "function" in object literals (#1897) * Lib: add Dom_html.window.matchMedia & Dom_html.mediaQueryList (#2017) * Lib: make the Wasm version of Json.output work with native ints and JavaScript objects (#1872) +* Lib: add Intl.RelativeDateFormat (#2070) ## Bug fixes * Compiler: fix stack overflow issues with double translation (#1869) diff --git a/lib/js_of_ocaml/intl.ml b/lib/js_of_ocaml/intl.ml index 6a71b5d2cb..c659db4cc1 100644 --- a/lib/js_of_ocaml/intl.ml +++ b/lib/js_of_ocaml/intl.ml @@ -413,6 +413,57 @@ module PluralRules = struct end end +module RelativeTimeFormat = struct + include Shared + + class type resolved_options = object + method locale : Js.js_string Js.t Js.readonly_prop + + method style : Js.js_string Js.t Js.readonly_prop + + method numberingSystem : Js.js_string Js.t Js.readonly_prop + + method numeric : Js.js_string Js.t Js.readonly_prop + end + + class type options = object + method localeMatcher : Js.js_string Js.t Js.prop + + method numberingSystem : Js.js_string Js.t Js.optdef Js.prop + + method style : Js.js_string Js.t Js.optdef Js.prop + + method numeric : Js.js_string Js.t Js.optdef Js.prop + end + + let options () : options Js.t = + object%js + val mutable localeMatcher = Js.string "best fit" + + val mutable style = Js.undefined + + val mutable numberingSystem = Js.undefined + + val mutable numeric = Js.undefined + end + + class type format_part = object + method _type : Js.js_string Js.t Js.readonly_prop + + method _value : Js.js_string Js.t Js.readonly_prop + end + + class type t = object + method format : + (Js.number Js.t -> Js.js_string Js.t -> Js.js_string Js.t) Js.readonly_prop + + method formatToParts : + Js.date Js.t Js.optdef -> format_part Js.t Js.js_array Js.t Js.meth + + method resolvedOptions : unit -> resolved_options Js.t Js.meth + end +end + class type intl = object method _Collator : Collator._object Js.t Js.readonly_prop @@ -422,6 +473,8 @@ class type intl = object method _PluralRules : PluralRules._object Js.t Js.readonly_prop + method _RelativeTimeFormat : RelativeTimeFormat._object Js.t Js.readonly_prop + method getCanonicalLocales : Js.js_string Js.t Js.js_array Js.t -> Js.js_string Js.t Js.js_array Js.t Js.meth end @@ -436,4 +489,6 @@ let numberFormat_constr = Js.Unsafe.global##._Intl##._NumberFormat let pluralRules_constr = Js.Unsafe.global##._Intl##._PluralRules +let relativeTimeFormat_constr = Js.Unsafe.global##._Intl##._RelativeTimeFormat + let is_supported () = Js.Optdef.test intl diff --git a/lib/js_of_ocaml/intl.mli b/lib/js_of_ocaml/intl.mli index c0e61b9515..066e53312f 100644 --- a/lib/js_of_ocaml/intl.mli +++ b/lib/js_of_ocaml/intl.mli @@ -349,6 +349,13 @@ then ( with Error err -> Console.console##debug (string (string_of_error err))) else Console.console##debug (string "Intl is not supported!") ]} + + let options = Intl.RelativeTimeFormat.options () in + let () = options##.numeric := "auto" in + let () = options##.style := "short" in + let th_rtf = new%js Intl.relativeTimeFormat_constr (def (jas [| "th-TH" |])) options in + fc (th_rtf.format -1 "day"); + @see for API documentation. @see for the ECMAScript specification. *) @@ -627,6 +634,48 @@ module PluralRules : sig end end +module RelativeTimeFormat : sig + include Shared + + class type resolved_options = object + method locale : Js.js_string Js.t Js.readonly_prop + + method style : Js.js_string Js.t Js.readonly_prop + + method numberingSystem : Js.js_string Js.t Js.readonly_prop + + method numeric : Js.js_string Js.t Js.readonly_prop + end + + class type options = object + method localeMatcher : Js.js_string Js.t Js.prop + + method numberingSystem : Js.js_string Js.t Js.optdef Js.prop + + method style : Js.js_string Js.t Js.optdef Js.prop + + method numeric : Js.js_string Js.t Js.optdef Js.prop + end + + val options : unit -> options Js.t + + class type format_part = object + method _type : Js.js_string Js.t Js.readonly_prop + + method _value : Js.js_string Js.t Js.readonly_prop + end + + class type t = object + method format : + (Js.number Js.t -> Js.js_string Js.t -> Js.js_string Js.t) Js.readonly_prop + + method formatToParts : + Js.date Js.t Js.optdef -> format_part Js.t Js.js_array Js.t Js.meth + + method resolvedOptions : unit -> resolved_options Js.t Js.meth + end +end + class type intl = object method _Collator : Collator._object Js.t Js.readonly_prop @@ -636,6 +685,8 @@ class type intl = object method _PluralRules : PluralRules._object Js.t Js.readonly_prop + method _RelativeTimeFormat : RelativeTimeFormat._object Js.t Js.readonly_prop + method getCanonicalLocales : Js.js_string Js.t Js.js_array Js.t -> Js.js_string Js.t Js.js_array Js.t Js.meth end @@ -666,4 +717,10 @@ val pluralRules_constr : -> PluralRules.t Js.t) Js.constr +val relativeTimeFormat_constr : + ( Js.js_string Js.t Js.js_array Js.t Js.optdef + -> RelativeTimeFormat.options Js.t Js.optdef + -> RelativeTimeFormat.t Js.t) + Js.constr + val is_supported : unit -> bool