【问题标题】:Converting strings to unicode as part of a maven build作为 maven 构建的一部分将字符串转换为 unicode
【发布时间】:2013-03-19 10:57:57
【问题描述】:

我有一些本地化资源文件(例如,messages_es.properties)需要采用 unicode 格式才能在浏览器中正确显示。为了避免存储资源文件的 unicode 和非 unicode* 版本,我想将转换为 unicode 任务合并到 maven 构建中。有没有简单的方法/工具可以做到这一点?

示例

label.button.loadMoreImages=CARGAR MÁS IMÁGENES -> label.button.loadMoreImages=CARGAR MÁS IMÁGENES

*当文本更改并需要重新翻译时更容易使用

【问题讨论】:

  • 也许这会有所帮助:stackoverflow.com/questions/8330485/…
  • 如果您的翻译人员使用适当的属性文件编辑器可能会更好。
  • @McDowell - 不幸不在我的控制范围内
  • 属性文件需要用 ISO-8859-1 编码,所以 wheresrhys 的例子实际上是一个正确的属性文件。 \u0100 以下的字符不需要转义。

标签: java maven unicode build


【解决方案1】:

您的要求非常具体,我怀疑您会找到一种工具来做您想做的事。考虑writing your own Maven plugin

首先,您必须获取属性文件并解码数据。属性文件通常存储在 ISO 8859-1 中,但可以通过提供 InputStreamReader 来存储 perform your own character decoding

接下来,您要枚举和 XML 转义非 ASCII 值。 Apache Commons Lang 提供了一种类型,如果你不想自己滚动的话。

【讨论】:

    【解决方案2】:

    有两个简单的 ant 任务可以将本机转换为 ascii 并返回,可以像这样安排为目标

      <target name="native2ascii">
        <delete dir="${classes.dir}/resources" includes="*.properties" />
        <native2ascii encoding="cp1252" src="${source.dir}/resources" dest="${classes.dir}/resources" includes="*.properties" />
      </target>
    
      <target name="ascii2native">
        <native2ascii  encoding="cp1252" src="${classes.dir}/resources" dest="${resources.dir}"  includes="*.properties" reverse="true"/>
      </target>
    

    这很容易适应您的环境。

    【讨论】:

    • 1) 发布者使用的是 Maven(尽管我意识到可以使用另一个) 2) 目标必须产生 XML 转义;不是 Java Unicode 转义。
    • @McDowell 1) 不能集成到 maven 中吗? 2) OP 要求“转换为 unicode”。
    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多