From 1443dc2f55da5f2c725380751c5d0d7bd9968180 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 17 Jun 2014 18:15:52 +0400 Subject: [PATCH] Adds short month and short weekday support --- .../classlib/impl/unicode/CLDRHelper.java | 14 +++++++++++ .../classlib/impl/unicode/CLDRLocale.java | 10 ++++++++ .../classlib/impl/unicode/CLDRReader.java | 25 +++++++++++++++++++ .../unicode/DateSymbolsMetadataGenerator.java | 8 ++++++ .../java/text/TDateFormatSymbols.java | 6 +++++ .../main/java/org/teavm/samples/DateTime.java | 5 +++- 6 files changed, 67 insertions(+), 1 deletion(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java index c2510b32f..b6abfb854 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRHelper.java @@ -57,6 +57,20 @@ public class CLDRHelper { @MetadataProvider(DateSymbolsMetadataGenerator.class) private static native ResourceMap> getMonthMap(); + public static String[] resolveShortMonths(String language, String country) { + return resolveDateFormatSymbols(getShortMonthMap(), language, country); + } + + @MetadataProvider(DateSymbolsMetadataGenerator.class) + private static native ResourceMap> getShortMonthMap(); + + public static String[] resolveShortWeekdays(String language, String country) { + return resolveDateFormatSymbols(getShortWeekdayMap(), language, country); + } + + @MetadataProvider(DateSymbolsMetadataGenerator.class) + private static native ResourceMap> getShortWeekdayMap(); + private static String[] resolveDateFormatSymbols(ResourceMap> map, String language, String country) { String localeCode = getCode(language, country); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java index 4345a90bb..952a3086b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRLocale.java @@ -30,6 +30,8 @@ public class CLDRLocale { String[] eras; String[] dayPeriods; String[] months; + String[] shortMonths; + String[] shortWeekdays; public Map getLanguages() { return Collections.unmodifiableMap(languages); @@ -50,4 +52,12 @@ public class CLDRLocale { public String[] getMonths() { return Arrays.copyOf(months, months.length); } + + public String[] getShortMonths() { + return Arrays.copyOf(shortMonths, shortMonths.length); + } + + public String[] getShortWeekdays() { + return Arrays.copyOf(shortWeekdays, shortWeekdays.length); + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java index 2260b7731..888808618 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java @@ -30,6 +30,7 @@ import com.google.gson.JsonParser; * @author Alexey Andreev */ public class CLDRReader { + private static String[] weekdayKeys = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; private Map knownLocales = new LinkedHashMap<>(); private Map minDaysMap = new LinkedHashMap<>(); private Map firstDayMap = new LinkedHashMap<>(); @@ -104,6 +105,8 @@ public class CLDRReader { readEras(localeName, localeInfo, root); readAmPms(localeName, localeInfo, root); readMonths(localeName, localeInfo, root); + readShortMonths(localeName, localeInfo, root); + readShortWeekdays(localeName, localeInfo, root); break; } } @@ -167,6 +170,28 @@ public class CLDRReader { } } + private void readShortMonths(String localeCode, CLDRLocale locale, JsonObject root) { + JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() + .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() + .get("gregorian").getAsJsonObject().get("months").getAsJsonObject() + .get("stand-alone").getAsJsonObject().get("abbreviated").getAsJsonObject(); + locale.shortMonths = new String[12]; + for (int i = 0; i < 12; ++i) { + locale.shortMonths[i] = monthsJson.get(String.valueOf(i + 1)).getAsString(); + } + } + + private void readShortWeekdays(String localeCode, CLDRLocale locale, JsonObject root) { + JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() + .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() + .get("gregorian").getAsJsonObject().get("days").getAsJsonObject() + .get("stand-alone").getAsJsonObject().get("short").getAsJsonObject(); + locale.shortWeekdays = new String[7]; + for (int i = 0; i < 7; ++i) { + locale.shortWeekdays[i] = monthsJson.get(weekdayKeys[i]).getAsString(); + } + } + private void readWeekData(InputStream input) { JsonObject root = (JsonObject)new JsonParser().parse(new InputStreamReader(input)); JsonObject weekJson = root.get("supplemental").getAsJsonObject().get("weekData").getAsJsonObject(); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateSymbolsMetadataGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateSymbolsMetadataGenerator.java index 617cdaa06..a11f77f73 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateSymbolsMetadataGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/unicode/DateSymbolsMetadataGenerator.java @@ -39,6 +39,14 @@ public class DateSymbolsMetadataGenerator implements MetadataGenerator { return generateSymbols(context, new ResourceExtractor() { @Override public String[] extract(CLDRLocale locale) { return locale.getMonths(); } }); + case "getShortMonthMap": + return generateSymbols(context, new ResourceExtractor() { + @Override public String[] extract(CLDRLocale locale) { return locale.getShortMonths(); } + }); + case "getShortWeekdayMap": + return generateSymbols(context, new ResourceExtractor() { + @Override public String[] extract(CLDRLocale locale) { return locale.getShortWeekdays(); } + }); default: throw new AssertionError("Unsupported method: " + method); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java index 15b647abf..0e6af286a 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java @@ -147,10 +147,16 @@ public class TDateFormatSymbols implements TSerializable, TCloneable { } public String[] getShortMonths() { + if (shortMonths == null) { + shortMonths = CLDRHelper.resolveShortMonths(locale.getLanguage(), locale.getCountry()); + } return shortMonths.clone(); } public String[] getShortWeekdays() { + if (shortWeekdays == null) { + shortWeekdays = CLDRHelper.resolveShortWeekdays(locale.getLanguage(), locale.getCountry()); + } return shortWeekdays.clone(); } diff --git a/teavm-samples/src/main/java/org/teavm/samples/DateTime.java b/teavm-samples/src/main/java/org/teavm/samples/DateTime.java index 8c32b9f27..202d3e294 100644 --- a/teavm-samples/src/main/java/org/teavm/samples/DateTime.java +++ b/teavm-samples/src/main/java/org/teavm/samples/DateTime.java @@ -170,7 +170,10 @@ public class DateTime { text = symbols.getAmPmStrings()[value]; break; case Calendar.MONTH: - text = symbols.getMonths()[value]; + text = symbols.getMonths()[value] + "/" + symbols.getShortMonths()[value]; + break; + case Calendar.DAY_OF_WEEK: + text = symbols.getShortWeekdays()[value - 1]; break; default: text = "";