iOS国际化字符串替换

使用

把localizedString.sh放在代码文件(*.m)所在的文件夹下,运行:
./localizedString.sh fullPathToLocalizableString

Example:

./localizedString.sh /Users/zhoujie/Documents/CMRead-iPhone/CMRead-iPhone/Localizable.strings

localizedString.sh

localizedString.sh文件内容

#!/usr/local/bin/bash
##author: zhoujie<[email protected]>
##Localizable.strings文件中字符串替换掉代码文件中字符串为NSLocalizedString(@“key”, nil)


##调试开关
#set -x

##必须先声明
declare -a ss
declare -a kk


echo "正在建立keys和values..."
##建立keys和values
##tip:Placing an assignment in front of a command causes it to be local to that command and does not change its value elsewhere in the script.
##############################################################
while IFS="=" read key value
do
    key=$( printf "%s" "${key}" | sed -n -E 's/(\s*)(".*")(\s*)/\2/p' );
    value=$( printf "%s" "${value}" | sed -n -E 's/(\s*)(".*")(;)/\2/p' )
    if [[ "${key}" == '""' || -z "${key}" || "${value}" == '""' || -z "${value}" ]]; then
        :  #do nothing    
    else
        ss[${#ss[@]}]=${value};
        kk[${#kk[@]}]=${key};        
    fi
    key=""
    value=""
done < "$1"
##############################################################


if [[ ${#ss[@]} != ${#kk[@]} ]]; then
    echo "键与值数量不相等"
    exit -1
fi

##字符串替换
##############################################################
for (( i = 0; i < ${#ss[@]}; i++ )); do    
    if [[ "${kk[$i]}" == '""' || -z "${kk[$i]}" ]]; then
        :  #do nothing    
    else        
        printf "正在替换:|%s| --> |%s|\n" "${ss[${i}]}" "${kk[${i}]}"
        sed -i -e "s/@${ss[${i}]}/NSLocalizedString(@${kk[${i}]}, nil)/" *.m
    fi
done
##############################################################

echo done!!!

推荐

https://github.com/ashen-zhao/ReadChinese
iOS国际化方案 & XLIFF

你可能感兴趣的:(iOS国际化字符串替换)