Search code examples
gwtinternationalizationuibinder

How to handle single quotes in internationalization constants?


We define all our internationalized constant strings in a single properties file LocalizableResource_xx.properties (one per language) located in google.gwt.i18n.client.

This way it is possible to access the constants in Java code via the constants interface

Window.alert(myConstants.helloWorld());

as well as to use them inside the UiBinder .ui.xml

<ui:attribute key="some.key" name="text" description="useful info" />

This approach doesn't work well if the string contains a single quote ('). This is because the GWT compiler throws a java.text.ParseException: Unterminated single quote: when it processes the .ui.xml files. If we escape the quote, meaning double it (''), the compiler passes but the strings accessed via the constant interface contain both single quotes (like in You can''t do that).

Replacing the single quotes with the utf-8 encoding \u0027 doesn't help (same exception as above).

Is it somehow possible to use the same properties file in UiBinder templates as well as in Java code without running into annoying single quote problems?


Solution

  • Since there doesn't seem to be another solution I'll post our workaround to the mentioned issue:

    We created a wrapper class implementing the constants interface that simply passes the i18n constants to a String modify(String) before they're passed to the caller.

    public class ConstantsWrapper implements MyConstants {
    
        private static MyConstants sConstants = GWT.create(MyConstants.class);
    
        public static String transform(String text) {
            return text.replaceAll("''", "'");
        }
    
        @Override
        public String someText() {
            return transform(sConstants.someText());
        }
    
        ...
    }
    

    I am still looking forward to a real solution.