最近项目是做的国际化app,就需要多语言适配,每次都要将语言给翻译公司,然后回来各国语言后,再进行复制粘贴很是麻烦,因此站在了巨人的肩膀上写了一个工具类
package com.example.lizhiqiang.kotlindemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 多语言适配
* 使用方法resDirPath为项目res路径 newResDirPath为新增语言文件夹
* Created by lizhiqiang on 2018/7/20
*/
public class I18nUtils {
private static final String TAG = "I18nUtils";
public static void main(String[] args) {
//if (args == null || args.length != 2) {
// System.out.println("不合法的参数");
// return;
//}
// String resDirPath = args[0];
// String newResDirPath = args[1];
String resDirPath = "/Users/lizhiqiang/Public/workspace//app/src/main/res";
String newResDirPath = "/Users/lizhiqiang/Documents/android2.13";
File newResDir = new File(newResDirPath);
for (File file : newResDir.listFiles()) {
if (!file.isDirectory()) continue;
System.out.println(file.getName() + "...");
if ("values".equals(file.getName())) {
System.out.println("跳过 values 文件夹");
continue;
}// 跳过默认的文件夹
File toFile = new File(resDirPath + "/" + file.getName() + "/strings.xml");
if (!toFile.exists()) {
System.out.println(toFile.getAbsolutePath() + "文件不存在");
continue;
}
String mergedContent = merge(getContent(toFile), getContent(getStringsXmlFile(file)));
writeContent(toFile, mergedContent);
}
}
private static String merge(String oldContent, String newContent) {
int index = oldContent.lastIndexOf("</resources>");
if (index > 0) {
oldContent = oldContent.substring(0, index);
}
index = newContent.indexOf("<resources>");
if (index > 0) {
newContent = newContent.substring(index + "<resources>".length());
}
index = newContent.lastIndexOf("</resources>");
if (index <= 0) {
newContent += "</resources>";
}
//将 > 替换为 >
System.out.println(newContent);
if (newContent.contains(">")){
newContent = newContent.replace(">", '>'+ "");
}
//将 <替换为 <
if (newContent.contains("<")){
newContent = newContent.replace("<", '<'+ "");
}
System.out.println(newContent);
return oldContent + newContent;
}
private static File getStringsXmlFile(File dir) {
for (File file : dir.listFiles()) {
if (file.getName().endsWith(".xml")) return file;
}
return null;
}
private static String getContent(File file) {
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = null;
String tmp;
try {
bufferedReader = new BufferedReader(new FileReader(file));
while ((tmp = bufferedReader.readLine()) != null) {
sb.append(tmp).append(System.lineSeparator());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
private static void writeContent(File file, String content) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}