Skip to content

Add Intl.RelativeTimeFormat #2070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions lib/js_of_ocaml/intl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
57 changes: 57 additions & 0 deletions lib/js_of_ocaml/intl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl> for API documentation.
@see <https://www.ecma-international.org/ecma-402/1.0/> for the ECMAScript specification. *)

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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