最近在做项目的时候,由于做的是国外的电信项目,因此经常涉及到多语言的国际资源化问题。
而项目组目前使用的是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;
}
}
}