博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android多语言项目中字符串的移植(bash工具)
阅读量:4144 次
发布时间:2019-05-25

本文共 2692 字,大约阅读时间需要 8 分钟。

有时候我们会遇到这样一种情况:

一些字符串资源要从原始项目A移植到现在我们开发的项目B中

比如移植app名字

Calendar
我们需要做的是:

在新项目对应的语言资源中查找是否有app_label这个资源。

    有:则查看新旧资源是否一致

        一致:则什么也不做

        不一致:删除旧的,添加新的资源

    没有:添加新的资源

工作内容很简单,但是,语言种类可能达到五六十种,移植的资源往往也不是一两个,所以工作量不可忽视

我觉得这种毫无技术含量的体力活还是交给脚本处理的好,为此特意写了个工具,希望能帮助大家提高效率

使用方法:

1.先将需要移植的资源的key统一放到一个文本中,用换行分隔。

比如:

$ cat /home/su1216/string_list app_namebutton_namecompany_name……

注意:string_list这个文件如果是在windows下制作的,需要先将它转换成unix格式,方法如下:

用vi打开脚本,修改文件格式,命令如下

:set ff=unix
然后保存退出

2.然后执行下面命令即可:

merge_strings android_project_src android_project_dest /home/su1216/string_list

#!/bin/bash#example#merge_strings project_src/packages/apps/Settings project_dest/packages/apps/Settings string_listsrc_dir="$1"dest_dir="$2"string_list="$3"#确保list文件中含有\n,如果已经含有\n,那么不会再增加sed -i -e '$a\' "$string_list"regex_with_all_string=""while read line; do    regex_with_all_string=$regex_with_all_string"name=\"$line\"|"done < "$string_list"regex_with_all_string=${regex_with_all_string%|*}result_list=`grep -Pr "$regex_with_all_string" $src_dir/res/values*/*.xml`#echo "grep -Pr '"$regex_with_all_string"' $src_dir/res/values*/*.xml"if [ -f "results.txt" ]; then    echo "rm results.txt first please."    exitfitouch results.txtIFS_OLD=$IFSIFS=$'\n'for line in $result_list; do    echo "${line#*res/}" >> results.txtdoneIFS=$IFS_OLDmake_new_xml_file() {    local country="$1"    local folder=${country%/*}    if [ ! -d "$dest_dir/res/$folder" ]; then        mkdir "$dest_dir/res/$folder"    fi    local xml_path="$dest_dir/res/$country"    touch "$xml_path"    echo '
' > "$xml_path" #line1 echo '
' >> "$xml_path" #line2 echo '
' >> "$xml_path" #line3}insert_line() {# 插入到这行之前 local string_file="$1" local line="$2" local trim_line=`echo $2 | grep -Po '\S.*\S'` local name=`echo $trim_line | grep -Po "(?<=name=\").*?(?=\")"` local line_no=`grep -n "\b$name\b" "$string_file" | grep -Po "^\d+"` #a.检查是否有这个字段 if [ "$line_no" != "" ]; then #echo "line_no=$line_no" "$string_file" local result=`grep -n "$trim_line" "$string_file"` #b.检查是否能完整匹配。如果不能,则删除旧的,添加新的 if [ "$result" = "" ]; then echo "sed command :""$line_no""d" sed -i "$line_no""d" "$string_file" sed -i '/<\/resources>/i\'"$line" "$string_file" fi else sed -i '/<\/resources>/i\'"$line" "$string_file" fi}#MERGEwhile read line; do country_new=`echo "$line" | grep -Po "^.*?\.xml"` string_file="$dest_dir/res/$country_new" line=`echo "$line" | grep -Po "(?<=:).*"` if [ ! -f "$string_file" ]; then make_new_xml_file "$country_new" fi #echo "$line" insert_line "$string_file" "$line"done < results.txt

转贴请保留以下链接

本人blog地址

转载地址:http://ructi.baihongyu.com/

你可能感兴趣的文章
TCP协议三次握手、四次挥手以及TCP窗口滑动机制
查看>>
【排序算法】- 希尔排序
查看>>
Int和Integer的区别
查看>>
java如何避免死锁
查看>>
【排序算法】- 快速排序
查看>>
【排序算法】- 归并排序
查看>>
【查找算法】- 二分查找算法
查看>>
java中的静态变量、静态方法与静态代码块区别
查看>>
JAVA中静态块、静态变量加载顺序详解
查看>>
Spring Cloud 学习(六)分布式配置中心
查看>>
Intellij Idea的常用快捷键
查看>>
分布式架构的演进过程
查看>>
分布式系统架构设计
查看>>
幂等性问题及解决方案
查看>>
Spring与HikariCP的结合使用
查看>>
如何设置连接池的大小?
查看>>
Lombok介绍
查看>>
elastic-job的原理简介和使用
查看>>
两种方法上传本地文件到github
查看>>
linux下开启SSH,并且允许root用户远程登录,允许无密码登录
查看>>