android 国际化 String.xml Excel 转换

android 国际化 
产品提供的Excel

使用代码转换 避免复制粘贴
 浪费太多时间了

1  读取String文件 存放集合中 转成Excel 表格 产品填写
2 读取String文件 存放集合中 再读取 Excel数据 修改集合数据完善翻译 再转成 excel 或者 String.xml  

 


public class Test {
	static HashMap<String, Entity> list = new HashMap<String, Entity>();

	public static final String DEFAULT_LANGUAGE_FLAG = "values";

	public static final String[] LANGUAGE = { "values",
			"values-zh-cn",
			"values-en-rGB",
			"values-de-rDE",
			"values-fr-rFR", // values-fr-rFR
			"values-es-rES", "values-it-rIT", "values-pt-rPT", "values-nl-rNL",
			"values-sv-rSE", "values-no-rNO", "values-fi-rFI", "values-da-rDK",
			"values-hu-rHU", "values-pl-rPL", "values-cs-rCZ", "values-tr-rTR",
			"values-ru-rRU", "values-el-rGR", "values-ro-rRO", "android_id"

	};

	// 国家/地区 语言缩写代码 查询备用
	// 国家/地区 语言代码 国家/地区 语言代码
	// 简体中文(中国) zh-cn 繁体中文(台湾地区) zh-tw
	// 繁体中文(香港) zh-hk 英语(香港) en-hk
	// 英语(美国) en-us 英语(英国) en-gb
	// 英语(全球) en-ww 英语(加拿大) en-ca
	// 英语(澳大利亚) en-au 英语(爱尔兰) en-ie
	// 英语(芬兰) en-fi 芬兰语(芬兰) fi-fi
	// 英语(丹麦) en-dk 丹麦语(丹麦) da-dk
	// 英语(以色列) en-il 希伯来语(以色列) he-il
	// 英语(南非) en-za 英语(印度) en-in
	// 英语(挪威) en-no 英语(新加坡) en-sg
	// 英语(新西兰) en-nz 英语(印度尼西亚) en-id
	// 英语(菲律宾) en-ph 英语(泰国) en-th
	// 英语(马来西亚) en-my 英语(阿拉伯) en-xa
	// 韩文(韩国) ko-kr 日语(日本) ja-jp
	// 荷兰语(荷兰) nl-nl 荷兰语(比利时) nl-be
	// 葡萄牙语(葡萄牙) pt-pt 葡萄牙语(巴西) pt-br
	// 法语(法国) fr-fr 法语(卢森堡) fr-lu
	// 法语(瑞士) fr-ch 法语(比利时) fr-be
	// 法语(加拿大) fr-ca 西班牙语(拉丁美洲) es-la
	// 西班牙语(西班牙) es-es 西班牙语(阿根廷) es-ar
	// 西班牙语(美国) es-us 西班牙语(墨西哥) es-mx
	// 西班牙语(哥伦比亚) es-co 西班牙语(波多黎各) es-pr
	// 德语(德国) de-de 德语(奥地利) de-at
	// 德语(瑞士) de-ch 俄语(俄罗斯) ru-ru
	// 意大利语(意大利) it-it 希腊语(希腊) el-gr
	// 挪威语(挪威) no-no 匈牙利语(匈牙利) hu-hu
	// 土耳其语(土耳其) tr-tr 捷克语(捷克共和国) cs-cz
	// 斯洛文尼亚语 sl-sl 波兰语(波兰) pl-pl
	// 瑞典语(瑞典) sv-se 西班牙语 (智利) es-cl

	public static final String I18N_TEMP_DIR = "/Users/kylinhuang/Desktop/ExcelUtil/";
	public static final String StringDir = "/Users/kylinhuang/Desktop/ExcelUtil/values/";

	private static final String StringFileName = "strings.xml";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		readDirALLXml(StringDir + LANGUAGE[0], 0); // 读取默认
													// DEFAULT_LANGUAGE_FLAG,
		printMap();

		readDirALLXml(StringDir + LANGUAGE[1], 1); // 读取中文 "zh-cn",
		printMap();

		// readDirALLXml(StringDir + LANGUAGE[2] , 2); // 读取 "en-rGB"
		// printMap();

		readDirALLXml(StringDir + LANGUAGE[3], 3); // 读取 "de-rDE"
		printMap();

		readDirALLXml(StringDir + LANGUAGE[4], 4); // 读取 "fr-rFR" values-fr-rFR
													// values-fr-rFR
		printMap();

		String Excel_paht = I18N_TEMP_DIR + System.currentTimeMillis() + ".xls";
		onExportExcel(Excel_paht);

		String xmlDir = I18N_TEMP_DIR + System.currentTimeMillis() + "/values/";
		try {
			onExportXML(xmlDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void onExportXML(String xmlDir) throws IOException,
			DocumentException {
		File dir = new File(xmlDir);
		if (dir.exists() == false)
			dir.mkdirs();
		for (int i = 0, size = LANGUAGE.length; i < size; i++) {
			File f = new File(xmlDir + LANGUAGE[i] + "/string.xml");

			if (f.getParentFile().exists() == false) f.getParentFile().mkdirs();
			if (f.exists() == false) f.createNewFile();

			Document document = DocumentHelper.createDocument();
			Element resources = document.addElement("resources");
			for (String key : list.keySet()) {
				// System.out.println("key= " + key + " and value= " +
				// list.get(key).id + " " + list.get(key).LANGUAGE[0]);
				String value = list.get(key).LANGUAGE[i];
				if (null != value && value.length() > 0) {
					Element s = resources.addElement("string");
					s.addAttribute("name", key);
					s.addText(list.get(key).LANGUAGE[i]);
				}
			}
			
			try {
				OutputFormat outputFormat = OutputFormat.createPrettyPrint();
				outputFormat.setLineSeparator("\r\n");// 这是为了换行操作
				Writer writer = new FileWriter(xmlDir + LANGUAGE[i] + "/string1.xml");
				XMLWriter outPut = new XMLWriter(writer, outputFormat);

				outPut.write(document);
				outPut.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	private static void printMap() {
		StringBuffer str;
		for (String key : list.keySet()) {
			System.out.println("key= " + key + " and value= "
					+ list.get(key).id + " " + list.get(key).LANGUAGE[0]);
		}
	}

	private static void onExportExcel(String excel_paht) {
		File file = new File(excel_paht);
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}

		OutputStream excelStream;
		try {
			excelStream = new FileOutputStream(excel_paht);
			// create one book
			WritableWorkbook workbook = Workbook.createWorkbook(excelStream);
			// create sheet
			WritableSheet sheet = workbook.createSheet("1", 0);
			// add titles
			for (int x = 0, size = LANGUAGE.length; x < size; x++) {
				sheet.addCell(new Label(x, 0, LANGUAGE[x]));
			}

			int y = 0;
			// add data
			for (HashMap.Entry<String, Entity> entry : list.entrySet()) {
				String key = entry.getKey();
				Entity e = (Entity) entry.getValue();
				int size = e.LANGUAGE.length;
				for (int x = 0; x < size; x++) {
					if (x == 20) { // android_id
						sheet.addCell(new Label(x, y + 1, e.id));
					} else {
						String content = e.LANGUAGE[x] != null ? e.LANGUAGE[x]
								.toString() : "";
						if (null != content && content.length() > 0) {
							sheet.addCell(new Label(x, y + 1, content));
						}
					}
				}
				y++;
			}
			workbook.write();
			workbook.close();
			excelStream.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e1) {
			e1.printStackTrace();
		} catch (WriteException e1) {
			e1.printStackTrace();
		}
	}

	private static void readDirALLXml(String xml_path, int positiion) {
		if (null != xml_path && xml_path.length() > 0) {
			// File f = new File(xml_path);
			File files[] = new File(xml_path).listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.endsWith(".xml");
				}
			});
			for (File file : files) {
				readXml(file.getAbsolutePath(), positiion);
			}
		}
	}

	private static void readXml(String xml_path, int positiion) {
		if (null != xml_path && xml_path.length() > 0) {
			File f = new File(xml_path);

			SAXReader reader = new SAXReader();
			Document doc;
			try {
				doc = reader.read(f);
				Element root = doc.getRootElement(); // resources
				Element foo;

				Entity entity;
				for (Iterator i = root.elementIterator("string"); i.hasNext();) {
					foo = (Element) i.next();
					String v = foo.getText();
					String name = foo.attribute("name").getValue();

					Entity bean = list.get(name);
					if (null == bean) {
						bean = new Entity();
						bean.id = name;
					}
					bean.LANGUAGE[positiion] = v;
					list.put(name, bean);
				}
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
	}

}

 

下载 地址CSDN http://download.csdn.net/detail/kylin_huang/9911754

 

转载于:https://my.oschina.net/kylinhuang/blog/1489811

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值