在xcode中是否有办法验证所有NSLocalizedString'键?

时间:2013-08-28 19:27:28

标签: xcode localization

除了运行其中包含NSLocalizedString的每个代码路径之外,有没有办法验证所有NSLocalizedStrings都有一个实际存在于所有bundle的所有Localizable.strings文件中的密钥?

E.g。一个键中没有输入错误,以至于NSLocalizedString找不到它正在寻找的键?

2 个答案:

答案 0 :(得分:8)

好的,我写了一个bash脚本来完成上述操作。这里是。这花了我几个小时所以如果你愿意,不要忘记向我投票。随意做出改进等。我添加了一些评论,暗示了潜在的改进。

#!/bin/sh

# VerNSLocalizedStrings

while getopts "vsl:" arg; do
    case $arg in
    v)
        verbose="yes"
        ;;
    s)
        stopOnMissing="yes"
        ;;
    l)
        lang=$OPTARG
        ;;
    esac
done

if [[ -z $lang ]] 
    then
    lang="en"
fi

searchDir=$lang.lproj
fileFound=`ls . | grep $searchDir`
if [[ -z $fileFound ]]
then
   echo "dir "$searchDir" not found."
   exit
fi

fileFound=`ls $searchDir/ | grep strings`
if [[ -z $fileFound ]]
then
   echo "No .strings files found in dir "$searchDir"."
   exit
fi

echo "Verifying NSLocalizationStrings in "$searchDir 

# Get all the NSLocalizedString Commands
output=$(grep -R NSLocalizedString . --include="*.m")

# Go thru the NSLocalizedString commands line for line
count=$(( 0 ))
missing=$(( 0 ))
unusable=$(( 0 ))
OIFS="${IFS}"
NIFS=$'\n'
IFS="${NIFS}"
for LINE in ${output} ; do
    IFS="${OIFS}"

    # Now extract the key from it
    # admittedly this only works if there are no line breaks between
    # NSLocalizedStrings and the entire key,
    # but it accounts for the keys it couldn't identify.
    quotes=`echo $LINE | awk -F\" '{ for(i=2; i<=NF; i=i+2){ a = a"\""$i"\"""^";} {print a; a="";}}'`
    key=`echo $quotes | cut -f1 -d"^"`

    # If we couldn't find the key then flag problem
    if [[ -z $key ]] 
    then
        (( unusable += 1 ))
    echo "Couldn't extract key: " $LINE
        if [ -n "$stopOnMissing" ] 
        then
            break
        else 
            continue
        fi
    fi

    # I don't know how grep works regarding length of string, only that
    # if the string is too long then it doesn't find it in the file
    keyLength=$(echo ${#key})
    if [ $keyLength -gt 79 ] 
    then
        (( unusable += 1 ))
    echo "Key too long ("$keyLength"): " $key
        if [ -n "$stopOnMissing" ] 
        then
            break
        else 
            continue
        fi
    fi

    # It would be nice if this were a regular expression that allowed as many 
    # spaces as you want, even a line break then forced the quotes on the 
    # other side of the equal sign.
    keyString=$key" ="

    # Search for the key
    found=$(iconv -sc -f utf-16 -t utf8 $searchDir/*.strings | grep "$keyString")

    # damned if I know why some strings files are utf-16 and others are utf8
    if [[ -z $found ]] 
    then
    found=$(grep -r "$keyString" $searchDir/ --include=*.strings)
    fi

    # analyze the result
    if [[ -z $found ]] 
    then
        (( missing += 1 ))
    echo "Missing:  " $key "\n from: " $LINE
        if [ -n "$stopOnMissing" ] 
        then
            break
        fi
    else
        if [ -n "$verbose" ]
        then
            echo "found:  " $key
        fi
    fi
    (( count += 1 ))

    IFS="${NIFS}" 
done

IFS="${OIFS}"

# It would also be nice if it went the other way and identified 
# extraneous unused items in the strings files.  But
# I've spent enough time on this for now

echo $count " keys analyzed"
echo $unusable " keys could not be determined"
echo $missing " keys missing"

答案 1 :(得分:0)

要验证所有NSLocalizedStrings是否都具有一个已存在于所有Localizable.strings文件中的密钥,或者您错过了本地化,可以在项目方案编辑器中启用Localization enable“显示非本地化的字符串”选项。

enter image description here

现在运行应用程序,您将看到缺少本地化字符串的控制台日志。

enter image description here