android多语言项目中字符串的移植(bash工具)

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

一些字符串资源要从原始项目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/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值