千家信息网

Android需要翻译的strings有哪些

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容主要讲解"Android需要翻译的strings有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android需要翻译的strings有哪些"
千家信息网最后更新 2025年11月07日Android需要翻译的strings有哪些

本篇内容主要讲解"Android需要翻译的strings有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android需要翻译的strings有哪些"吧!

目录
  • 1、问题描述

  • 2、大概思路

  • 3、代码解析

1、问题描述

项目需要做俄语国际化,历史代码里有的字段有俄语翻译、有的没有,需要把没翻译的中文整理出来翻译成俄文。 (因为项目组件化module太多了所以觉得一个一个整理很麻烦,如果module不多的话就可以直接手动处理不用整下面这些了)

2、大概思路

  1. 列出所有res目录,根据是否包含values-ru分成两组(半自动)

  2. 在"不包含"分组里把需要翻译的中文文件复制出来(半自动)

  3. 在"包含"组里把需要补充翻译的字段复制出来(纯手动)

  4. 把复制出来需要翻译的xml文件转换成excel用于翻译(自动)

  5. 把翻译好的文件通过excel的公式转换成xml,根据之前记录的res目录放到项目里(半自动)

3、代码解析

列出所有string.xml文件路径

public static void listResPath(String src) throws Exception {    File path2 = new File(src);    if (!path2.exists()) {        return;    }    File[] items = path2.listFiles();    if (items == null) return;    for (File item : items) {        if (item.isFile()) {            if (!item.getName().equals("strings.xml")) continue;            System.out.println(item.getPath());        } else {            listResPath(item.getPath());        }    }}

手工找出不包含ru的模块,然后在项目里看一下应该翻译哪个文件,把需要翻译的文件路径放到一个txt里,例如:

D:\work\aaa\src\main\res\values-zh-rCN\strings.xmlD:\work\bbb\src\main\res\values-zh-rCN\strings.xmlD:\work\ccc\src\main\res\values\strings.xmlD:\work\ddd\src\main\res\values-zh\strings.xml

复制这些文件到translate文件夹

private static List needCopyFiles = new ArrayList<>();private static void getNeedCopyFiles() {    try {        FileInputStream inputStream = new FileInputStream("D:\xxx\needCopy.txt");        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));        String str;        while ((str = bufferedReader.readLine()) != null) {            if (!str.isEmpty()) {                needCopyFiles.add(str);            }        }        bufferedReader.close();    } catch (IOException e) {        e.printStackTrace();    }}public static void listResPath(String src) throws Exception {    File path2 = new File(src);    File path3 = new File("D:\xxx\translate");    if (!path2.exists()) {        return;    }    File[] items = path2.listFiles();    if (items == null) return;    for (File item : items) {        if (item.isFile()) {            if (!item.getName().equals("strings.xml")) continue;            if (needCopyFiles.contains(item.getPath())) {                System.out.println(item.getPath());                FileInputStream fis = new FileInputStream(item);                String[] dir = item.getPath().split("\\");                String fileName = dir[7]+dir[8]+".xml";                FileOutputStream fos = new FileOutputStream(path3 + File.separator + fileName);                byte[] b = new byte[1024];                for (int i=0; (i=fis.read(b))!=-1;) {                    fos.write(b,0,i);                    fos.flush();                }                fos.close();                fis.close();            }        } else {            listResPath(item.getPath());        }    }}

手工找出包含ru的模块,在项目里看一下需要补充翻译哪些字段,复制这些字段到新建的xml文件里,把这些xml文件也放在translate文件夹。

把translate文件夹里的文件读取到excel

import com.alibaba.excel.EasyExcel;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import java.io.File;import java.util.*;public class Strings2Excel {    private static final List excelDataBeanList = new ArrayList<>();    public static void main(String[] args) {        try {            traverseFile("D:\xxx\translate");            write2Excel();        } catch (Exception e) {            e.printStackTrace();        }    }    private static void write2Excel() {        String dst = "D:\xxx\res.xlsx";        System.out.println("excel list size: " + excelDataBeanList.size());        EasyExcel.write(dst, ExcelDataBean.class).sheet("value").doWrite(excelDataBeanList);    }    public static void traverseFile(String src) throws Exception {        File path2 = new File(src);        if (!path2.exists()) {            System.out.println("源路径不存在");            return;        }        File[] items = path2.listFiles();        if (items == null) return;        for (File item : items) {            readXml(item);        }    }    private static void readXml(File file) throws DocumentException {        SAXReader reader = new SAXReader();        Document document = reader.read(file);        Element rootElement = document.getRootElement();        Iterator iterator = rootElement.elementIterator();        while (iterator.hasNext()) {            Element child = iterator.next();            String name = child.attribute(0).getValue();            String value = child.getStringValue();            System.out.println(name + " = " + value);            excelDataBeanList.add(new ExcelDataBean(name, value));        }    }}
@Datapublic class ExcelDataBean {    private String name;    private String value;    private String translate;}

需要引入的依赖:

            com.alibaba        easyexcel        2.2.4                    org.apache.poi        poi        3.17                org.apache.poi        poi-ooxml        3.17                    org.dom4j        dom4j        2.1.3                org.slf4j        slf4j-simple        1.7.5    

因为不同模块可能会有重复的中文字段,翻译的同事是去重了翻译的,所以拿到翻译好了的excel之后,要把重复的翻译填充一下。思路是读取翻译前的文件到excelDataBeanList、读取翻译后的文件到translatedList,根据两个列表中相同的中文字段填充excelDataBeanList的俄文字段然后输出到新文件。

import com.alibaba.excel.EasyExcel;import com.alibaba.excel.context.AnalysisContext;import com.alibaba.excel.event.AnalysisEventListener;import java.io.File;import java.util.ArrayList;import java.util.List;public class FillTranslated {    private static final List excelDataBeanList = new ArrayList<>();    private static final List translatedList = new ArrayList<>();    public static void main(String[] args) {        try {            readRes("D:\xxx\res.xlsx");        } catch (Exception e) {            e.printStackTrace();        }    }    private static void readRes(String s) {        File file = new File(s);        EasyExcel.read(file, ExcelDataBean.class, new AnalysisEventListener() {            @Override            public void invoke(ExcelDataBean bean, AnalysisContext analysisContext) {                excelDataBeanList.add(bean);            }            @Override            public void doAfterAllAnalysed(AnalysisContext analysisContext) {                readTranslated("D:\xxx\translated.xlsx");            }        }).sheet().doRead();    }    private static void readTranslated(String s) {        File file = new File(s);        //这个read其实是按列读的,并不是根据列标题和类属性名称匹配的        EasyExcel.read(file, ExcelDataBean.class, new AnalysisEventListener() {            @Override            public void invoke(ExcelDataBean bean, AnalysisContext analysisContext) {                translatedList.add(bean);            }            @Override            public void doAfterAllAnalysed(AnalysisContext analysisContext) {                fillTranslated();                write2Excel();            }        }).sheet().doRead();    }    private static void fillTranslated() {        for (ExcelDataBean bean : translatedList) {            excelDataBeanList.forEach(a -> {                if (a.getValue() != null && a.getValue().equals(bean.getValue())) {                    a.setTranslate(bean.getTranslate());                }            });        }    }    private static void write2Excel() {        String dst = "D:\xxx\翻译字段.xlsx";        System.out.println("excel list size: " + excelDataBeanList.size());        EasyExcel.write(dst, ExcelDataBean.class).sheet("value").doWrite(excelDataBeanList);    }}

把excel转换成xml

A列BCHK
namevaluetranslate给name加双引号拼接xml里的string
detail_up收起убрать=""""&A2&""""=""&C2&""

到此,相信大家对"Android需要翻译的strings有哪些"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

文件 字段 项目 中文 代码 思路 文件夹 模块 目录 路径 内容 手动 手工 问题 俄文 俄语 学习 不同 实用 更深 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海天龙网络技术有限公司 华为软件开发工程第一牛人 龙之谷服务器没了 用个人电脑创建tcp服务器 嵌入式软件开发有什么副业 网络安全的预防图片 网络安全响应数据流程图 计算机学院网络安全 jsp查询数据库并循环输出 it大咖婉转网络安全 合肥知名软件开发团队 我的世界梦幻之都服务器幸运宝石 学不懂网络安全还要坚持吗 上海网上培训软件开发 p图怎样修改数据库 无法连接到配置数据库 数据库的资源复制是什么 南邮网络安全和信息化 服务器网站数据备份 网络安全宣传 班会ppt 告诉幼儿网络安全教育 公安特殊专技(网络安全防护) 数据项目软件开发属于服务 软件开发人员劳动合同模板 软件公司软件开发聚顶科技好在线 查找使用wifi无法连接服务器 网络安全大赛效果图制作 ai智能软件开发怎么样 nba2k20手游球员数据库 湘潭智能软件开发哪家便宜
0