Bash & Python processing application translation table for international import Excel into string.xml table

The company's business development must be internationalized. In the process of internationalization, the most headache is to write an internationalized language. The establishment of a corresponding string.xml file can make an internationalized application. The corresponding translation of each country's language should be about 1000 lines. If you copy one by one, the whole person will go mad

Version is written to use bash script to deal with international language

So I learned a way to be lazy and wrote a script to be lazy. Maybe the script is not perfect, but you can modify it according to your own needs

1.bash mode (Python is below)

Original content

Generated content

 

1. The specific translation work of internationalization is done by special personnel, and the excel table is as follows

2. First, save the xml we get as a csv suffix file

3. Execute the following script translation.sh to give executable permission

#!/bin/bash
bak=$IFS
#Check for file input
if [ $# -ne 1 ];then
  echo "Usage $0 filename"
  exit
fi
#Check whether the input is a file
if [ ! -f $1 ];then
  echo "the $1 is not a file"
  exit
fi
IFS=$'\n'
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>">> string.xml
echo "<resources>">> string.xml
for line in `cat $1`
	do
	  read1=`echo $line|cut -d "," -f 1`
	  read1=${read1##*' '} #Cut the space before the key
          if [ -z "$read1" ];then #Remove empty string
	    continue
	  fi
	  read2=`echo $line|cut -d "," -f 3`
          echo "<string name=\"$read1\""">"$read2"</string>" #Print what to write
	  echo "<string name=\"$read1\""">"$read2"</string>" >> string.xml #Write content to
done
echo "</resources>">> string.xml
IFS=$bak

Execute. / translation.sh translation file.csv at the terminal

You can see that the terminal output is as follows: after the output is completed, the xml will be generated (note that gedit is selected as the opening mode, because many translations contain content that cannot be parsed by xml)

 

2.Python mode

#! /usr/bin/python
# coding=utf-8
import os, re
import xlrd
import sys, getopt
import operator


################International multilingual replacement start
def intl(file):
    print ('-------------------- Start intl--------------------')
    #print(sys.argv[0])
    #print(sys.argv[1])

    file_03_excel = "/Users/xulei/Desktop/int/012_i18n_android_part1_en_1027.xlsx"

    print (file)
    print (file_03_excel)

    # 1. Open file
    x1 = xlrd.open_workbook(file_03_excel)

    # 2. Get sheet object
    print ('sheet_names:', x1.sheet_names())  # Get all sheet names
    print ('sheet_number:', x1.nsheets ) # Get sheet quantity
    print ('sheet_object:', x1.sheets() ) # Get all sheet objects
    print ('By_name:', x1.sheet_by_name("Sheet1") ) # Find by sheet name
    print ('By_index:', x1.sheet_by_index(0))  # Find by index

    # Read first sheet
    table = x1.sheets()[0]
    # Statistical row number
    n_rows = table.nrows
    print ('n_rows:', n_rows)


    f1 = open('/Users/xulei/Desktop/int/intl.txt', 'w')

    data = []

    # Wechat article property: wechat_name wechat_id title abstract url time read like number
    for v in range(1, n_rows-1):
        # Each row of data forms a list
        values = table.row_values(v)
        # List form dictionary
        # data.append('<string name="',values[0],'">',values[1],'</string>')
        key = values[0]
        value = values[2]
        string = '<string name=\"%s\">%s</string>'% (key, value)
        f1.writelines(string)
        f1.writelines('\n')




################International multilingual replacement end

def usage():
    print ('Auto publish dmall')
    print ('Usage:')
    print ('  --help                     Show help information')
    print ('  --publish=[release|debug]   Auto publish build, release will auto git commit, debug will not')
    print ('  --patch                     Auto patch build')
    print ('  --upload                    Auto upload patch')

print (sys.argv)

intl(sys.argv[1])

 

 

Published 25 original articles, won praise 13, visited 10000+
Private letter follow

Tags: xml Python Excel encoding

Posted on Tue, 14 Jan 2020 04:13:35 -0500 by r2ks