From fb401cae4bfc654d42acf5c2b2fec46d2bf35e9e 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 | 55 ++++++++++++++++++++++++++++++++++++++++ lib/js_of_ocaml/intl.mli | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) 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..3ab8c62b1e 100644 --- a/lib/js_of_ocaml/intl.mli +++ b/lib/js_of_ocaml/intl.mli @@ -627,6 +627,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 +678,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 +710,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 c79b7451109d32e5f7c81721082b789cf485a1fc 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] attempt comment test? 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 3ab8c62b1e..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. *) From f21142ac965599b10fc56f30c564ddf106669493 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 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)