Android多語言工具類
阿新 • • 發佈:2019-02-07
最近專案是做的國際化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(); } } } } }