Android 解析strings.xml国际化

1.国际化需要导出stringsxml中文,找了个小工具能够快速抽取strings.xml中的中文

步骤:
1. 新建 assets文件夹,内部新建一个 xxx.plist 文件(就是ios用的plist文件),
2. api ‘com.googlecode.plist:dd-plist:1.20’ 依赖,在app.config中

 工具类:
package com.locojoy.restart.utils;

import android.content.Context;

import com.core.logger.Logger;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.PropertyListFormatException;
import com.dd.plist.PropertyListParser;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

/**
 * Author: yangweichao
 * Date:   2019-05-18 10:54
 * Description: 懒汉Dcl 单例 解析Ios Plist文件
 */

public class PlistUtils {

    private static volatile PlistUtils plistUtil = null;

    public static PlistUtils getInstance() {
        if (plistUtil == null) {
            synchronized (PlistUtils.class) {
                if (plistUtil == null) {
                    plistUtil = new PlistUtils();
                }
            }
        }
        return plistUtil;
    }

    public <T> T parserPlist(Context context, T list) {
        try {
            NSArray ary = (NSArray) PropertyListParser.parse(context.getAssets().open("DPABookCIconDetails.plist"));
            for (int i = 0; i < ary.count(); i++) {//遍历当前NSArray
//将            # 当前遍历的结果也是字典。将其存放入dic(字典)中保存
                NSDictionary dic = (NSDictionary) ary.objectAtIndex(i);
                String iconId = (dic.objectForKey("iconId")).toJavaObject().toString();
                String color = (dic.objectForKey("color")).toJavaObject().toString();
                String iconname_h = (dic.objectForKey("iconname_h")).toJavaObject().toString();
                String iconname_d = (dic.objectForKey("iconname_d")).toJavaObject().toString();
                Logger.d(iconId + ":" + color + ":" + iconname_d + ":" + iconname_h);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (PropertyListFormatException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<String> parserList(Context context, List<String> list) {
        if (null == list)
            return new ArrayList<>();

        try {
            NSArray ary = (NSArray) PropertyListParser.parse(context.getAssets().open("DPABookCIconIds.plist"));
            for (int i = 0; i < ary.count(); i++) {//遍历当前NSArray
//将            # 当前遍历的结果也是字典。将其存放入dic(字典)中保存
                String value = String.valueOf(ary.objectAtIndex(i));
                list.add(value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (PropertyListFormatException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return list;
    }



}

3.在正确的位置调用

  List<String> strings=new ArrayList<>();
        PlistUtils.getInstance().parserList(this,strings);
        for (int i = 0; i < strings.size(); i++) {
            System.out.println(strings.get(i));
        }
    

输出结果如下:

Android 解析strings.xml国际化_第1张图片
新建class,使用Android studio 垂直选中工具,【在类中,右键选择 column selection Model】 直接选中,拷贝到excel中提供给翻译人就ok。

你可能感兴趣的:(android,基础知识)