From 7e53cd9af1e29822c07c177dc802ff9d3833fa0c Mon Sep 17 00:00:00 2001 From: GregoryTravis Date: Wed, 31 May 2023 08:43:20 -0400 Subject: [PATCH] Add drop down for Locale like Encoding (#6654) Add dropdowns for locale parameters for format and parse methods. --- .../Base/0.0.0-dev/src/Data/Locale.enso | 26 +++++++++++++++++++ .../Base/0.0.0-dev/src/Data/Time/Date.enso | 1 + .../0.0.0-dev/src/Data/Time/Date_Time.enso | 2 ++ .../0.0.0-dev/src/Data/Time/Time_Of_Day.enso | 2 ++ .../Table/0.0.0-dev/src/Data/Column.enso | 1 + test/Tests/src/Data/Locale_Spec.enso | 12 ++++++--- 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Locale.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Locale.enso index fb62a605659..be1d739a872 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Locale.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Locale.enso @@ -1,9 +1,16 @@ import project.Any.Any +import project.Data.Filter_Condition.Filter_Condition +import project.Data.Text.Case.Case +import project.Data.Text.Extensions import project.Data.Text.Text import project.Data.Vector.Vector +import project.Metadata.Display import project.Nothing.Nothing +import project.Meta from project.Data.Boolean import Boolean, False +from project.Metadata.Widget import Single_Choice +from project.Metadata.Choice import Option polyglot java import java.util.Locale as JavaLocale @@ -421,3 +428,22 @@ type Locale Convert Locale to a friendly string. to_display_text : Text to_display_text self = "Locale(" + self.to_text + ")" + + ## PRIVATE + Gets the default drop down option for this encoding. + default_widget : Single_Choice + default_widget = Single_Choice values=Locale.widget_options display=Display.When_Modified + + ## PRIVATE + predefined_locale_fields : Vector Text + predefined_locale_fields = + locale_meta = Meta.meta Locale + remove_us = locale_meta.methods + ["Value", "new", "default", "from_language_tag", "from_java", "predefined_locale_fields", "default_widget", "widget_options"] + Meta.Type.Value (Meta.type_of locale_meta.value) . methods . filter (Filter_Condition.Not_In remove_us) . sort + + ## PRIVATE + widget_options : Vector Option + widget_options = Locale.predefined_locale_fields.map field_name-> + display_string = field_name.replace '_' ' ' . to_case (if field_name.length == 2 then Case.Upper else Case.Title) + code_string = "Locale." + field_name + Option display_string code_string diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso index ee7212ba054..6fec6c39cbf 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date.enso @@ -705,6 +705,7 @@ type Date Format "2020-06-21" with French locale as "21. juin 2020" example_format = Date.new 2020 6 21 . format "d. MMMM yyyy" (Locale.new "fr") + @locale Locale.default_widget format : Text -> Locale -> Text format self pattern locale=Locale.default = case locale of Nothing -> Time_Utils.local_date_format self pattern diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso index e2bd0ad1eb7..9cd24652241 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Time.enso @@ -244,6 +244,7 @@ type Date_Time example_parse = Date_Time.parse "06 of May 2020 at 04:30AM" "dd 'of' MMMM yyyy 'at' hh:mma" + @locale Locale.default_widget parse : Text -> Text | Nothing -> Locale -> Date_Time ! Time_Error parse text pattern=Nothing locale=Locale.default = Panic.catch JException handler=(cause -> Error.throw (Time_Error.Error cause.payload.getMessage)) <| @@ -656,6 +657,7 @@ type Date_Time example_format = Date_Time.parse "2020-06-21T16:41:13+03:00" . format "d. MMMM yyyy" (Locale.new "fr") + @locale Locale.default_widget format : Text -> Locale -> Text format self pattern locale=Locale.default = case locale of Nothing -> Time_Utils.date_time_format self pattern diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso index 1086b067191..e6b8b0d035a 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Of_Day.enso @@ -167,6 +167,7 @@ type Time_Of_Day from Standard.Base import Time_Of_Day example_parse = Time_Of_Day.parse "4:30AM" "h:mma" + @locale Locale.default_widget parse : Text -> Text | Nothing -> Locale -> Time_Of_Day ! Time_Error parse text pattern=Nothing locale=Locale.default = Panic.catch JException handler=(cause -> Error.throw (Time_Error.Error cause.payload.getMessage)) <| @@ -354,6 +355,7 @@ type Time_Of_Day from Standard.Base import Time_Of_Day example_format = Time_Of_Day.new 16 21 10 . format "'hour:'h" + @locale Locale.default_widget format : Text -> Locale -> Text format self pattern locale=Locale.default = case locale of Nothing -> Time_Utils.time_of_day_format self pattern diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Column.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Column.enso index 3be889442ff..d526de4d2ae 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Column.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Column.enso @@ -1271,6 +1271,7 @@ type Column input = Column.from_vector "values" ["100000000", "2222", "3"] . parse numeric_type input.format "#,##0.00" locale=(Locale.new "fr") # ==> ["100 000 000,00", "2 222,00", "3,00"] + @locale Locale.default_widget format : Text | Column -> Locale -> Column ! Illegal_Argument format self format=Nothing locale=Locale.default = create_formatter = make_value_formatter_for_value_type self.value_type locale diff --git a/test/Tests/src/Data/Locale_Spec.enso b/test/Tests/src/Data/Locale_Spec.enso index 443e9f1d8e7..2338116a68f 100644 --- a/test/Tests/src/Data/Locale_Spec.enso +++ b/test/Tests/src/Data/Locale_Spec.enso @@ -1,10 +1,11 @@ from Standard.Base import all -polyglot java import java.util.Locale as JavaLocale - +from Standard.Base.Metadata.Choice import Option from Standard.Test import Test, Test_Suite import Standard.Test.Extensions +polyglot java import java.util.Locale as JavaLocale + with_locale locale ~test = default_locale = JavaLocale.getDefault JavaLocale.setDefault locale.java_locale @@ -14,7 +15,8 @@ with_locale locale ~test = JavaLocale.setDefault default_locale result -spec = Test.group "Locale" <| +spec = + Test.group "Locale" <| en_gb = Locale.new "en" "GB" Test.specify "allow constructing a locale with optional parts" <| loc = Locale.new "en" @@ -74,4 +76,8 @@ spec = Test.group "Locale" <| Locale.uk . should_equal Locale.uk Locale.uk . should_not_equal Locale.us + Test.specify "Should correctly auto-discover static Locales" <| + locale_names = Locale.widget_options.map x-> case x of Option locale_name _ _ _ -> locale_name + locale_names . should_equal ['Bangladesh', 'Brazil', 'Canada English', 'Canada French', 'China', 'France', 'Germany', 'India English', 'India Hindi', 'Indonesia', 'Italy', 'Japan', 'Mexico', 'Nigeria', 'Pakistan English', 'Pakistan Urdu', 'Poland', 'Russia', 'South Korea', 'UK', 'US'] + main = Test_Suite.run_main spec