From 0a9d1292acad977b5fcee7b57286c3745b7ab146 Mon Sep 17 00:00:00 2001 From: pubiqq Date: Wed, 18 Dec 2024 17:32:18 +0300 Subject: [PATCH] [DatePicker] Fix date input hints --- .../android/material/datepicker/UtcDates.java | 76 ++++++++++++++++--- .../datepicker/res/values-az/bools.xml | 20 +++++ .../datepicker/res/values-ko/bools.xml | 20 +++++ .../datepicker/res/values-zh-rHK/bools.xml | 20 +++++ .../material/datepicker/res/values/bools.xml | 20 +++++ .../material/datepicker/UtcDatesTest.java | 18 +++++ 6 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 lib/java/com/google/android/material/datepicker/res/values-az/bools.xml create mode 100644 lib/java/com/google/android/material/datepicker/res/values-ko/bools.xml create mode 100644 lib/java/com/google/android/material/datepicker/res/values-zh-rHK/bools.xml create mode 100644 lib/java/com/google/android/material/datepicker/res/values/bools.xml diff --git a/lib/java/com/google/android/material/datepicker/UtcDates.java b/lib/java/com/google/android/material/datepicker/UtcDates.java index e32c5a50bb0..b5e1952fb6a 100644 --- a/lib/java/com/google/android/material/datepicker/UtcDates.java +++ b/lib/java/com/google/android/material/datepicker/UtcDates.java @@ -167,17 +167,75 @@ static SimpleDateFormat getDefaultTextInputFormat() { } static String getDefaultTextInputHint(Resources res, SimpleDateFormat format) { - String formatHint = format.toPattern(); - String yearChar = res.getString(R.string.mtrl_picker_text_input_year_abbr); - String monthChar = res.getString(R.string.mtrl_picker_text_input_month_abbr); - String dayChar = res.getString(R.string.mtrl_picker_text_input_day_abbr); - - // Remove duplicate characters for Korean. - if (Locale.getDefault().getLanguage().equals(Locale.KOREAN.getLanguage())) { - formatHint = formatHint.replaceAll("d+", "d").replaceAll("M+", "M").replaceAll("y+", "y"); + String datePattern = format.toPattern(); + + final String yearAbbr = res.getString(R.string.mtrl_picker_text_input_year_abbr); + final String monthAbbr = res.getString(R.string.mtrl_picker_text_input_month_abbr); + final String dayAbbr = res.getString(R.string.mtrl_picker_text_input_day_abbr); + + final boolean abbrsArePlaceholders = + res.getBoolean(R.bool.mtrl_picker_text_input_hint_abbrs_are_placeholders); + + return abbrsArePlaceholders + ? replaceOneLetterFormatSpecifiers(datePattern, yearAbbr, monthAbbr, dayAbbr) + : replaceFormatSpecifiers(datePattern, yearAbbr, monthAbbr, dayAbbr); + } + + private static String replaceOneLetterFormatSpecifiers( + String datePattern, String yearAbbr, String monthAbbr, String dayAbbr) { + final StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < datePattern.length(); i++) { + char ch = datePattern.charAt(i); + switch (ch) { + case 'y': + stringBuilder.append(yearAbbr); + break; + case 'M': + stringBuilder.append(monthAbbr); + break; + case 'd': + stringBuilder.append(dayAbbr); + break; + default: + stringBuilder.append(ch); + break; + } + } + + return stringBuilder.toString(); + } + + private static String replaceFormatSpecifiers( + String datePattern, String yearAbbr, String monthAbbr, String dayAbbr) { + final StringBuilder stringBuilder = new StringBuilder(); + + char prevCh = 0; + for (int i = 0; i < datePattern.length(); i++) { + char ch = datePattern.charAt(i); + switch (ch) { + case 'y': + if (prevCh != 'y') { + stringBuilder.append(yearAbbr); + } + break; + case 'M': + if (prevCh != 'M') { + stringBuilder.append(monthAbbr); + } + break; + case 'd': + if (prevCh != 'd') { + stringBuilder.append(dayAbbr); + } + break; + default: + stringBuilder.append(ch); + break; + } + prevCh = ch; } - return formatHint.replace("d", dayChar).replace("M", monthChar).replace("y", yearChar); + return stringBuilder.toString(); } /** diff --git a/lib/java/com/google/android/material/datepicker/res/values-az/bools.xml b/lib/java/com/google/android/material/datepicker/res/values-az/bools.xml new file mode 100644 index 00000000000..a7b46cf19ee --- /dev/null +++ b/lib/java/com/google/android/material/datepicker/res/values-az/bools.xml @@ -0,0 +1,20 @@ + + + + + false + diff --git a/lib/java/com/google/android/material/datepicker/res/values-ko/bools.xml b/lib/java/com/google/android/material/datepicker/res/values-ko/bools.xml new file mode 100644 index 00000000000..a7b46cf19ee --- /dev/null +++ b/lib/java/com/google/android/material/datepicker/res/values-ko/bools.xml @@ -0,0 +1,20 @@ + + + + + false + diff --git a/lib/java/com/google/android/material/datepicker/res/values-zh-rHK/bools.xml b/lib/java/com/google/android/material/datepicker/res/values-zh-rHK/bools.xml new file mode 100644 index 00000000000..a7b46cf19ee --- /dev/null +++ b/lib/java/com/google/android/material/datepicker/res/values-zh-rHK/bools.xml @@ -0,0 +1,20 @@ + + + + + false + diff --git a/lib/java/com/google/android/material/datepicker/res/values/bools.xml b/lib/java/com/google/android/material/datepicker/res/values/bools.xml new file mode 100644 index 00000000000..55ad3c24395 --- /dev/null +++ b/lib/java/com/google/android/material/datepicker/res/values/bools.xml @@ -0,0 +1,20 @@ + + + + + true + diff --git a/lib/javatests/com/google/android/material/datepicker/UtcDatesTest.java b/lib/javatests/com/google/android/material/datepicker/UtcDatesTest.java index a24cb6d2ca4..6e40d9c8e52 100644 --- a/lib/javatests/com/google/android/material/datepicker/UtcDatesTest.java +++ b/lib/javatests/com/google/android/material/datepicker/UtcDatesTest.java @@ -78,6 +78,15 @@ public void textInputHintWith1CharYearLocalized() { assertEquals("m/j/a", hint); } + @Test + @Config(qualifiers = "az") + public void textInputHintForAzerbaijani() { + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy"); + String hint = UtcDates.getDefaultTextInputHint(context.getResources(), sdf); + + assertEquals("gün.ay.il", hint); + } + @Test @Config(qualifiers = "ko") public void textInputHintForKorean() { @@ -87,6 +96,15 @@ public void textInputHintForKorean() { assertEquals("년.월.일.", hint); } + @Test + @Config(qualifiers = "zh-rHK") + public void textInputHintForChineseHongKong() { + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy"); + String hint = UtcDates.getDefaultTextInputHint(context.getResources(), sdf); + + assertEquals("日/月/年", hint); + } + @Test public void normalizeTextInputFormat() { SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");