如何处理国际化常量中的单引号? [英] How to handle single quotes in internationalization constants?

查看:667
本文介绍了如何处理国际化常量中的单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将所有国际化常量字符串定义在位于 google.gwt.i18n中的单个属性文件 LocalizableResource_xx.properties 中(每种语言一个)。客户端



通过这种方式,可以通过常量界面访问Java代码中的常量

  Window.alert(myConstants.helloWorld()); 

以及在UiBinder .ui.xml中使用它们 code
$ b $ pre $ < ui:attribute key =some.keyname =textdescription =useful info/>

如果字符串包含单引号( )。这是因为GWT编译器在处理 .ui.xml java.text.ParseException:Unterminated single quote: c>文件。如果我们转义引号(即''),编译器会传递,但通过常量接口访问的字符串包含单引号(如你不能这样做)。



用UTF-8编码\\\'替换单引号不会帮助(同样的例外如上)。

是否有可能在UiBinder模板和Java代码中使用相同的属性文件,而不会遇到烦人的单引号问题?

解决方案

由于似乎没有其他解决方案,因此我会将解决方法发布到上述问题:



我们创建了一个实现常量接口的包装类,它在传递之前将i18n常量传递给 String modify(String)

  public class ConstantsWrapper implements MyConstants {

private static MyConstants sConstants = GWT。创建(MyConstants.class);

public static String transform(String text){
return text.replaceAll('',');
}

@Override
public String someText(){
return transform(sConstants.someText());
}

...
}

我仍然期待着真正的解决方案。


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?

解决方案

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.

这篇关于如何处理国际化常量中的单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆