多语言资源文件的过滤工具
2012-12-04 10:48
309 查看
最近在做项目的时候,由于做的是国外的电信项目,因此经常涉及到多语言的国际资源化问题。
而项目组目前使用的是SSI框架,因此国际资源的读取等问题直接采用s:text标签即可。
但是,当我们在实现某一种语言之后,经常会发现两个不同语言的资源文件的 key值有遗漏等问题,很不利于整合。
为止,我自己写了一个很简单的小工具,主要是实现:当有多个资源文件时,查找对应的key,把key值不同的选项找出来。
并且,根据Key值新生成一个文本文件,以方便在整理一些不认识的语言的翻译等问题时带来方便。
以下为CombinationProperties.java源码:
而项目组目前使用的是SSI框架,因此国际资源的读取等问题直接采用s:text标签即可。
但是,当我们在实现某一种语言之后,经常会发现两个不同语言的资源文件的 key值有遗漏等问题,很不利于整合。
为止,我自己写了一个很简单的小工具,主要是实现:当有多个资源文件时,查找对应的key,把key值不同的选项找出来。
并且,根据Key值新生成一个文本文件,以方便在整理一些不认识的语言的翻译等问题时带来方便。
以下为CombinationProperties.java源码:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.Properties; import java.util.Map.Entry; public class CombinationProperties { private static Properties propertiesUS; private static Properties propertiesSA; /** *输出文件的文件夹路径。 **/ private static final String path = "F:\\workspace1\\PropertiesDoIt\\"; private static final String fileOutSa = "globalMessages_ar_SA.txt"; private static final String fileOutUs = "globalMessages_en_US.txt"; private static final String fileNoTranslate = "globalMessages_noTranslate.txt"; private static final String fileOut = "globalMessages.txt"; //资源文件名 private static final String filePathUS = "globalMessages_en_US.properties"; //资源文件名 private static final String filePathSA = "globalMessages_ar_SA.properties"; public static void main(String[] args) { //对合入的Map输出到指定的文件中。 FileOutputStream fosUs = null; FileOutputStream fosSa = null; FileOutputStream fos = null; FileOutputStream fosNoTranslate = null; try { fosUs = new FileOutputStream(path + fileOutUs); fosSa = new FileOutputStream(path + fileOutSa); fos = new FileOutputStream(path + fileOut); fosNoTranslate = new FileOutputStream(path + fileNoTranslate); //循环 Iterator<Entry<Object, Object>> propertiesUSIter = propertiesUS.entrySet() .iterator(); while (propertiesUSIter.hasNext()) { Entry<Object, Object> e = propertiesUSIter.next(); String strKey = (String) e.getKey(); String strValue = (String) e.getValue(); //如果该Key值不存在于propertiesSA中,则表示该资源文件只存在于US中,获取后把他保存在onlyUSProperties.txt中 if (null == propertiesSA.get(strKey)) { fosUs.write((strKey + "=" + strValue + "\n").getBytes("utf-8")); } //则表示在两个资源文件都存在,则需要把保存在combitionText.txt文件中。 else { String strValueSa =propertiesSA.getProperty(strKey); fos.write((strKey + "=" + strValue + "\n").getBytes("utf-8")); fos.write((strKey + "=" + strValueSa + "\n").getBytes("utf-8")); //如果两个资源文件中的Value相等,则表示有资源文件没有提交翻译 if(null!=strValue && strValue.equals(strValueSa)) { fosNoTranslate.write((strKey + "=" + strValue + "\n").getBytes("utf-8")); fosNoTranslate.write((strKey + "=" + strValueSa + "\n").getBytes("utf-8")); } //共同拥有的文件,需要移除(剩下的则为只有SA有的资源文件,保存在onlySAProperties.txt中) propertiesSA.remove(strKey); } } //过滤之后剩下的都只存在于SA中的资源文件 Iterator<Entry<Object, Object>> propertiesSAIter = propertiesSA.entrySet() .iterator(); while (null != propertiesSAIter && propertiesSAIter.hasNext()) { Entry<Object, Object> e = propertiesSAIter.next(); String strKey = (String) e.getKey(); String strValue = (String) e.getValue(); fosSa.write((strKey + "=" + strValue + "\n").getBytes("utf-8")); // propertiesSAOnlyMap.put(strKey, strValue); } } catch (UnsupportedEncodingException e1) { System.out.println(e1); e1.printStackTrace(); } catch (FileNotFoundException e) { System.out.println(e); e.printStackTrace(); } catch (IOException ioe) { System.out.println(ioe); ioe.printStackTrace(); } finally { try { fosSa.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { fosUs.close(); } catch (IOException e) { e.printStackTrace(); } try { fosNoTranslate.close(); } catch (IOException e) { e.printStackTrace(); } } } static { init(); } public static void init() { propertiesUS = new Properties(); propertiesSA = new Properties(); try { FileInputStream inputStreamUS = new FileInputStream(path + filePathUS); propertiesUS.load(inputStreamUS); FileInputStream inputStreamSA = new FileInputStream(path + filePathSA); propertiesSA.load(inputStreamSA); } catch (FileNotFoundException e) { System.out.println("properties文件不存在" + e); return; } catch (IOException e) { System.out.println("Client.properties文件解析失败" + e); return; } } }
相关文章推荐
- 多语言资源工具之制作类库资源文件
- 多语言资源文件制作工具
- SVG-Android开源库——SVG生成Vector资源文件的编辑预览工具
- 资源文件使用&多语言切换方法
- APP开发实战173-ActionBar资源文件的生成工具
- Android探索之旅(第二十七篇)Android屏幕适配资源文件批量分类工具
- 网络文件资源管理系统需要引用到的第三方工具或组件
- 用资源文件实现Asp.net程序的不同语言间的切换
- 【语言-C++】文件夹或文件加密 - 方案一使用7z-zip工具压缩加密
- Java语言对properties资源文件的处理——ResourceBundle 类的使用
- 【IOS】多语言资源文件加载的一种解决方案
- maven资源过滤打包后文件变大的处理方法
- build.gradle的高级运用,通过Groovy语言合理的编译打包代码,避免资源文件冗余
- 反编译apk生成程序的java源代码和图片、XML配置、语言资源等文件
- Windows Resx资源文件编辑工具
- 修改应用程序的配置的资源文件,并按配置发布应用程序工具
- Android 反编译得到图片、XML配置、语言资源等文件
- 利用apktool将APK反编译,生成程序的图片、XML配置、语言资源等文件
- 在.Net中嵌入资源文件到程序集中-.NET教程,VB.Net语言
- java所支持的全部国家和语言及资源文件的读取