5

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

例如,一个键中没有错字,这样 NSLocalizedString 就找不到它正在寻找的键?

4

2 回答 2

10

好的,我编写了一个 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"
于 2013-09-04T17:12:21.120 回答
3

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

在此处输入图像描述

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

在此处输入图像描述

于 2018-09-08T15:43:58.270 回答