Add drop down for Locale like Encoding (#6654)

Add dropdowns for locale parameters for format and parse methods.
This commit is contained in:
GregoryTravis 2023-05-31 08:43:20 -04:00 committed by GitHub
parent bc6b9bcf54
commit 7e53cd9af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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