有时候我们会遇到这样一种情况:
一些字符串资源要从原始项目A移植到现在我们开发的项目B中
比如移植app名字
<string name="app_label">Calendar</string>我们需要做的是:
在新项目对应的语言资源中查找是否有app_label这个资源。
有:则查看新旧资源是否一致
一致:则什么也不做
不一致:删除旧的,添加新的资源
没有:添加新的资源
工作内容很简单,但是,语言种类可能达到五六十种,移植的资源往往也不是一两个,所以工作量不可忽视
我觉得这种毫无技术含量的体力活还是交给脚本处理的好,为此特意写了个工具,希望能帮助大家提高效率
使用方法:
1.先将需要移植的资源的key统一放到一个文本中,用换行分隔。
比如:
$ cat /home/su1216/string_list app_name button_name company_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_list src_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." exit fi touch results.txt IFS_OLD=$IFS IFS=$'\n' for line in $result_list; do echo "${line#*res/}" >> results.txt done IFS=$IFS_OLD make_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 version="1.0" encoding="utf-8"?>' > "$xml_path" #line1 echo '<resources>' >> "$xml_path" #line2 echo '</resources>' >> "$xml_path" #line3 } insert_line() { #</resources> 插入到这行之前 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 } #MERGE while 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://su1216.iteye.com/
http://blog.csdn.net/su1216/