From 854202b7e8c4bc7b19b6477ae13dc84b0a3641b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Fri, 4 Jul 2025 14:25:29 +0700 Subject: [PATCH 1/3] add Intl.RelativeTime --- lib/js_of_ocaml/intl.ml | 54 ++++++++++++++++++++++++++++++++++++++++ lib/js_of_ocaml/intl.mli | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/lib/js_of_ocaml/intl.ml b/lib/js_of_ocaml/intl.ml index 6a71b5d2cb..9c8c610fe6 100644 --- a/lib/js_of_ocaml/intl.ml +++ b/lib/js_of_ocaml/intl.ml @@ -413,6 +413,56 @@ 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.meth + + method formatToParts : + Js.number Js.t -> Js.js_string Js.t -> 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 +472,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 +488,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..afa989f4d5 100644 --- a/lib/js_of_ocaml/intl.mli +++ b/lib/js_of_ocaml/intl.mli @@ -627,6 +627,47 @@ 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.meth + + method formatToParts : + Js.number Js.t -> Js.js_string Js.t -> 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 +677,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 +709,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 From 067efd339e544adbe40a856218a8605c41128491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Fri, 4 Jul 2025 15:02:15 +0700 Subject: [PATCH 2/3] comment test / doc Not entirely sure what these are or how they work --- lib/js_of_ocaml/intl.mli | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/js_of_ocaml/intl.mli b/lib/js_of_ocaml/intl.mli index afa989f4d5..392a179675 100644 --- a/lib/js_of_ocaml/intl.mli +++ b/lib/js_of_ocaml/intl.mli @@ -346,9 +346,16 @@ then ( (intl##._PluralRules##supportedLocalesOf (jas [| "ban"; "id-u-co-pinyin"; "de-ID" |]) (def options)) + + let options = Intl.RelativeTimeFormat.options () in + let () = options##.numeric := def (string "auto") in + let () = options##.style := def (string "short") in + let th_rtf = new%js Intl.relativeTimeFormat_constr (def (jas [| "th-TH" |])) (def options) in + fc (th_rtf##.format -1 "day"); with Error err -> Console.console##debug (string (string_of_error err))) else Console.console##debug (string "Intl is not supported!") ]} + @see for API documentation. @see for the ECMAScript specification. *) From 016d15be5cf8e83f9b2ec4b44b59781bd5021d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=B7=F0=90=91=91=F0=90=91=B4=F0=90=91=95=F0=90=91=91?= =?UTF-8?q?=F0=90=91=A9=F0=90=91=A4?= Date: Fri, 4 Jul 2025 15:05:41 +0700 Subject: [PATCH 3/3] update CHANGES --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 279807920b..40ac29ceea 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -66,6 +66,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)