Android多语言工具类

最近项目是做的国际化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>";
        }
        //将 &gt; 替换为 >
        System.out.println(newContent);
        if (newContent.contains("&gt;")){
            newContent = newContent.replace("&gt;", '>'+ "");
        }
        //将 &lt;替换为 <
        if (newContent.contains("&lt;")){
            newContent = newContent.replace("&lt;", '<'+ "");
        }
        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();
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值