Adds support of day periods and months to DateFormatSymbols

This commit is contained in:
konsoletyper 2014-06-16 17:39:25 +04:00
parent ee5bcf2c32
commit 8780a13ab3
6 changed files with 105 additions and 20 deletions

View File

@ -37,16 +37,38 @@ public class CLDRHelper {
private static native ResourceMap<StringResource> getLikelySubtagsMap(); private static native ResourceMap<StringResource> getLikelySubtagsMap();
public static String[] resolveEras(String language, String country) { public static String[] resolveEras(String language, String country) {
ResourceMap<ResourceArray<StringResource>> map = getErasMap(); return resolveDateFormatSymbols(getErasMap(), language, country);
String localeCode = getCode(language, country);
ResourceArray<StringResource> arrayRes = map.has(localeCode) ? map.get(localeCode) :
map.has(language) ? map.get(language) : map.get("root");
return new String[] { arrayRes.get(0).getValue(), arrayRes.get(1).getValue() };
} }
@MetadataProvider(DateSymbolsMetadataGenerator.class) @MetadataProvider(DateSymbolsMetadataGenerator.class)
private static native ResourceMap<ResourceArray<StringResource>> getErasMap(); private static native ResourceMap<ResourceArray<StringResource>> getErasMap();
public static String[] resolveAmPm(String language, String country) {
return resolveDateFormatSymbols(getAmPmMap(), language, country);
}
@MetadataProvider(DateSymbolsMetadataGenerator.class)
private static native ResourceMap<ResourceArray<StringResource>> getAmPmMap();
public static String[] resolveMonths(String language, String country) {
return resolveDateFormatSymbols(getMonthMap(), language, country);
}
@MetadataProvider(DateSymbolsMetadataGenerator.class)
private static native ResourceMap<ResourceArray<StringResource>> getMonthMap();
private static String[] resolveDateFormatSymbols(ResourceMap<ResourceArray<StringResource>> map, String language,
String country) {
String localeCode = getCode(language, country);
ResourceArray<StringResource> arrayRes = map.has(localeCode) ? map.get(localeCode) :
map.has(language) ? map.get(language) : map.get("root");
String[] result = new String[arrayRes.size()];
for (int i = 0; i < result.length; ++i) {
result[i] = arrayRes.get(i).getValue();
}
return result;
}
@MetadataProvider(LanguageMetadataGenerator.class) @MetadataProvider(LanguageMetadataGenerator.class)
public static native ResourceMap<ResourceMap<StringResource>> getLanguagesMap(); public static native ResourceMap<ResourceMap<StringResource>> getLanguagesMap();

View File

@ -28,6 +28,8 @@ public class CLDRLocale {
final Map<String, String> languages = new LinkedHashMap<>(); final Map<String, String> languages = new LinkedHashMap<>();
final Map<String, String> territories = new LinkedHashMap<>(); final Map<String, String> territories = new LinkedHashMap<>();
String[] eras; String[] eras;
String[] dayPeriods;
String[] months;
public Map<String, String> getLanguages() { public Map<String, String> getLanguages() {
return Collections.unmodifiableMap(languages); return Collections.unmodifiableMap(languages);
@ -40,4 +42,12 @@ public class CLDRLocale {
public String[] getEras() { public String[] getEras() {
return Arrays.copyOf(eras, eras.length); return Arrays.copyOf(eras, eras.length);
} }
public String[] getDayPeriods() {
return Arrays.copyOf(dayPeriods, dayPeriods.length);
}
public String[] getMonths() {
return Arrays.copyOf(months, months.length);
}
} }

View File

@ -99,11 +99,15 @@ public class CLDRReader {
case "territories.json": case "territories.json":
readCountries(localeName, localeInfo, input); readCountries(localeName, localeInfo, input);
break; break;
case "ca-gregorian.json": case "ca-gregorian.json": {
readEras(localeName, localeInfo, input); JsonObject root = (JsonObject)new JsonParser().parse(new InputStreamReader(input));
readEras(localeName, localeInfo, root);
readAmPms(localeName, localeInfo, root);
readMonths(localeName, localeInfo, root);
break; break;
} }
} }
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error reading CLDR file", e); throw new RuntimeException("Error reading CLDR file", e);
} }
@ -133,14 +137,34 @@ public class CLDRReader {
} }
} }
private void readEras(String localeCode, CLDRLocale locale, InputStream input) { private void readEras(String localeCode, CLDRLocale locale, JsonObject root) {
JsonObject root = (JsonObject)new JsonParser().parse(new InputStreamReader(input));
JsonObject erasJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() JsonObject erasJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject() .get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
.get("gregorian").getAsJsonObject().get("eras").getAsJsonObject().get("eraNames").getAsJsonObject(); .get("gregorian").getAsJsonObject().get("eras").getAsJsonObject().get("eraNames").getAsJsonObject();
String am = erasJson.get("0").getAsString(); String bc = erasJson.get("0").getAsString();
String pm = erasJson.get("1").getAsString(); String ac = erasJson.get("1").getAsString();
locale.eras = new String[] { am, pm }; locale.eras = new String[] { bc, ac };
}
private void readAmPms(String localeCode, CLDRLocale locale, JsonObject root) {
JsonObject ampmJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
.get("gregorian").getAsJsonObject().get("dayPeriods").getAsJsonObject()
.get("format").getAsJsonObject().get("abbreviated").getAsJsonObject();
String am = ampmJson.get("am").getAsString();
String pm = ampmJson.get("pm").getAsString();
locale.dayPeriods = new String[] { am, pm };
}
private void readMonths(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("wide").getAsJsonObject();
locale.months = new String[12];
for (int i = 0; i < 12; ++i) {
locale.months[i] = monthsJson.get(String.valueOf(i + 1)).getAsString();
}
} }
private void readWeekData(InputStream input) { private void readWeekData(InputStream input) {

View File

@ -28,24 +28,38 @@ public class DateSymbolsMetadataGenerator implements MetadataGenerator {
public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) {
switch (method.getName()) { switch (method.getName()) {
case "getErasMap": case "getErasMap":
return generateEras(context); return generateSymbols(context, new ResourceExtractor() {
@Override public String[] extract(CLDRLocale locale) { return locale.getEras(); }
});
case "getAmPmMap":
return generateSymbols(context, new ResourceExtractor() {
@Override public String[] extract(CLDRLocale locale) { return locale.getDayPeriods(); }
});
case "getMonthMap":
return generateSymbols(context, new ResourceExtractor() {
@Override public String[] extract(CLDRLocale locale) { return locale.getMonths(); }
});
default: default:
throw new AssertionError("Unsupported method: " + method); throw new AssertionError("Unsupported method: " + method);
} }
} }
private Resource generateEras(MetadataGeneratorContext context) { private Resource generateSymbols(MetadataGeneratorContext context, ResourceExtractor extractor) {
CLDRReader reader = context.getService(CLDRReader.class); CLDRReader reader = context.getService(CLDRReader.class);
ResourceMap<ResourceArray<StringResource>> result = context.createResourceMap(); ResourceMap<ResourceArray<StringResource>> result = context.createResourceMap();
for (Map.Entry<String, CLDRLocale> localeEntry : reader.getKnownLocales().entrySet()) { for (Map.Entry<String, CLDRLocale> localeEntry : reader.getKnownLocales().entrySet()) {
ResourceArray<StringResource> erasRes = context.createResourceArray(); ResourceArray<StringResource> symbolsRes = context.createResourceArray();
result.put(localeEntry.getKey(), erasRes); result.put(localeEntry.getKey(), symbolsRes);
for (String era : localeEntry.getValue().getEras()) { for (String symbol : extractor.extract(localeEntry.getValue())) {
StringResource eraRes = context.createResource(StringResource.class); StringResource symbolRes = context.createResource(StringResource.class);
eraRes.setValue(era); symbolRes.setValue(symbol);
erasRes.add(eraRes); symbolsRes.add(symbolRes);
} }
} }
return result; return result;
} }
private static interface ResourceExtractor {
String[] extract(CLDRLocale locale);
}
} }

View File

@ -119,6 +119,9 @@ public class TDateFormatSymbols implements TSerializable, TCloneable {
} }
public String[] getAmPmStrings() { public String[] getAmPmStrings() {
if (ampms == null) {
ampms = CLDRHelper.resolveAmPm(locale.getLanguage(), locale.getCountry());
}
return ampms.clone(); return ampms.clone();
} }
@ -130,10 +133,16 @@ public class TDateFormatSymbols implements TSerializable, TCloneable {
} }
public String getLocalPatternChars() { public String getLocalPatternChars() {
if (localPatternChars == null) {
localPatternChars = "";
}
return localPatternChars; return localPatternChars;
} }
public String[] getMonths() { public String[] getMonths() {
if (months == null) {
months = CLDRHelper.resolveMonths(locale.getLanguage(), locale.getCountry());
}
return months.clone(); return months.clone();
} }

View File

@ -166,6 +166,12 @@ public class DateTime {
case Calendar.ERA: case Calendar.ERA:
text = symbols.getEras()[value]; text = symbols.getEras()[value];
break; break;
case Calendar.AM_PM:
text = symbols.getAmPmStrings()[value];
break;
case Calendar.MONTH:
text = symbols.getMonths()[value];
break;
default: default:
text = ""; text = "";
break; break;