Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the strings that Android needs to translate?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the strings that Android needs to translate". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what strings Android needs to be translated.

Catalogue

1. Problem description

2. General ideas

3. Code parsing

1. Problem description

The project needs to do Russian internationalization, some fields in the history code have Russian translation, some have no translation, and the untranslated Chinese needs to be translated into Russian. (because there are too many moduleized modules of the project, I find it troublesome to organize one by one. If there is not much module, you can deal with it manually without having to do the following.)

2. General ideas

List all res directories and divide them into two groups according to whether they contain values-ru or not (semi-automatic)

Copy the Chinese documents that need to be translated in the "not included" group (semi-automatic)

Copy out the fields that need supplementary translation in the include group (manual only)

Convert copied xml files that need to be translated into excel for translation (automatic)

Convert the translated files into xml through excel's formula and put them into the project according to the previously recorded res directory (semi-automatic)

3. Code parsing

List all string.xml file paths

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 ());}

Manually find out the modules that do not contain ru, and then see which file should be translated in the project, and put the file path that needs to be translated into a txt, for example:

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

Copy these files to the translate folder

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 iTuno; (i=fis.read (b))! =-1 ) {fos.write; fos.flush ();} fos.close (); fis.close ();}} else {listResPath (item.getPath ());}

Manually find the module that contains ru, see which fields need to be translated in the project, copy these fields into the new xml file, and put these xml files in the translate folder.

Read the files in the translate folder to 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 ("Source path does not exist"); 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;}

Dependencies to be introduced:

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

Because there may be duplicate Chinese fields in different modules, the translator's colleague is going to repeat the translation, so after getting the translated excel, fill in the repeated translation. The idea is to read the pre-translated file to excelDataBeanList, read the translated file to translatedList, fill in the Russian field of excelDataBeanList according to the same Chinese field in the two lists, and then output it to the new file.

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) / / this read is actually read by column, not the EasyExcel.read (file, ExcelDataBean.class, new AnalysisEventListener () {@ Override public void invoke (ExcelDataBean bean, AnalysisContext analysisContext) {translatedList.add (bean) that matches the column title and class attribute name). } @ 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\ translation field .xlsx"; System.out.println ("excel list size:" + excelDataBeanList.size ()); EasyExcel.write (dst, ExcelDataBean.class) .sheet ("value") .doWrite (excelDataBeanList);}}

Convert excel to xml

Column A BCHKnamevaluetranslate add double quotation marks to name and concatenate the stringdetail_up in xml. Put away the strings in the xml. At this point, I believe you have a deeper understanding of what strings needs to be translated by Android. You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report