// sort for deterministic output
Arrays.sort(allLocales);
PrintWriter pw = context.tryCreate(logger, packageName, superClassName);
if (pw != null) {
String qualName = packageName + "." + superClassName;
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(
packageName, superClassName);
factory.setSuperclass(targetClass.getQualifiedSourceName());
factory.addImport("com.google.gwt.core.client.JavaScriptObject");
SourceWriter writer = factory.createSourceWriter(context, pw);
writer.println("private JavaScriptObject nativeDisplayNames;");
writer.println();
writer.println("@Override");
writer.println("public String[] getAvailableLocaleNames() {");
writer.println(" return new String[] {");
boolean hasAnyRtl = false;
for (GwtLocaleImpl possibleLocale : allLocales) {
writer.println(" \""
+ possibleLocale.toString().replaceAll("\"", "\\\"") + "\",");
if (RTL_LOCALES.contains(
possibleLocale.getCanonicalForm().getLanguage())) {
hasAnyRtl = true;
}
}
writer.println(" };");
writer.println("}");
writer.println();
writer.println("@Override");
writer.println("public native String getLocaleNativeDisplayName(String localeName) /*-{");
writer.println(" this.@" + qualName + "::ensureNativeDisplayNames()();");
writer.println(" return this.@" + qualName
+ "::nativeDisplayNames[localeName];");
writer.println("}-*/;");
writer.println();
writer.println("@Override");
writer.println("public boolean hasAnyRTL() {");
writer.println(" return " + hasAnyRtl + ";");
writer.println("}");
writer.println();
writer.println("private native void ensureNativeDisplayNames() /*-{");
writer.println(" if (this.@" + qualName
+ "::nativeDisplayNames != null) {");
writer.println(" return;");
writer.println(" }");
writer.println(" this.@" + qualName + "::nativeDisplayNames = {");
LocalizedProperties displayNames = new LocalizedProperties();
LocalizedProperties displayNamesManual = new LocalizedProperties();
LocalizedProperties displayNamesOverride = new LocalizedProperties();
ClassLoader classLoader = getClass().getClassLoader();
try {
InputStream str = classLoader.getResourceAsStream(GENERATED_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNames.load(str, "UTF-8");
}
str = classLoader.getResourceAsStream(MANUAL_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNamesManual.load(str, "UTF-8");
}
str = classLoader.getResourceAsStream(OVERRIDE_LOCALE_NATIVE_DISPLAY_NAMES);
if (str != null) {
displayNamesOverride.load(str, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
// UTF-8 should always be defined
logger.log(TreeLogger.ERROR, "UTF-8 encoding is not defined", e);
throw new UnableToCompleteException();
} catch (IOException e) {
logger.log(TreeLogger.ERROR, "Exception reading locale display names",
e);
throw new UnableToCompleteException();
}
boolean needComma = false;
for (GwtLocaleImpl possibleLocale : allLocales) {
String localeName = possibleLocale.toString();
String displayName = displayNamesOverride.getProperty(localeName);
if (displayName == null) {
displayName = displayNamesManual.getProperty(localeName);
}
if (displayName == null) {
displayName = displayNames.getProperty(localeName);
}
if (displayName != null && displayName.length() != 0) {
localeName = quoteQuotes(localeName);
displayName = quoteQuotes(displayName);
if (needComma) {
writer.println(",");
}
writer.print(" \"" + localeName + "\": \"" + displayName + "\"");
needComma = true;
}
}
if (needComma) {
writer.println();
}
writer.println(" };");
writer.println("}-*/;");
writer.commit(logger);
}
GwtLocale locale = localeUtils.getCompileLocale();
String className = targetClass.getName().replace('.', '_') + "_"
+ locale.getAsString();
Set<GwtLocale> runtimeLocales = localeUtils.getRuntimeLocales();
if (!runtimeLocales.isEmpty()) {
className += "_runtimeSelection";
}
pw = context.tryCreate(logger, packageName, className);
if (pw != null) {
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(
packageName, className);
factory.setSuperclass(superClassName);
factory.addImport("com.google.gwt.core.client.GWT");
factory.addImport("com.google.gwt.i18n.client.LocaleInfo");
factory.addImport("com.google.gwt.i18n.client.constants.NumberConstants");
factory.addImport("com.google.gwt.i18n.client.constants.NumberConstantsImpl");
factory.addImport("com.google.gwt.i18n.client.constants.DateTimeConstants");
factory.addImport("com.google.gwt.i18n.client.constants.DateTimeConstantsImpl");
SourceWriter writer = factory.createSourceWriter(context, pw);
writer.println("@Override");
writer.println("public String getLocaleName() {");
if (runtimeLocales.isEmpty()) {
writer.println(" return \"" + locale + "\";");
} else {