iOS可本地化字符串字数

时间:2016-03-22 13:53:43

标签: ios localization translation localizable.strings

我需要计算翻译iOS应用的费用。价格基于单词数量。我已经在Localizable.strings文件中提供了我需要翻译的所有内容。

是否有工具或命令或其他东西可以用来告诉我这个文件中有多少单词?这不是将其粘贴到Word中的问题,因为我需要忽略所有键和所有注释。

2 个答案:

答案 0 :(得分:1)

试试这个bash脚本。保存到与counter.sh文件夹相同的目录中的文本文件(我将其命名为en.lproj)。

# Delete the comments
cat en.lproj/Localizable.strings | sed '/^\\\*/ d' | sed '/^\/\/*/ d' > temp

# Delete everything up to and including the equals sign
cat temp | sed s/.*\=// > temp.1

# Delete the remaining quotes and semi-colon
cat temp.1 | sed s/\"// | sed s/\"// | sed s/\;// > temp.2

# Use wc to sount and spit out the number of words
wc -w < temp.2 

# Remove the temp files
rm -f temp
rm -f temp.1
rm -f temp.2

在终端中打开该目录。

通过运行chmod +x counter.sh

为脚本提供可执行权限

键入./counter.sh运行脚本,它应该吐出Localizable.strings文件中的字数。

声明! - 我的bash脚本技巧很差!如果你的字符串包含转义的“或=字符”,那么这个脚本可能会破坏,所以它需要收紧一点。这也是可怕的,但应该做你需要做的事情!

答案 1 :(得分:0)

我在Swift中编写了一个片段,我在一个新的OSX应用程序中运行,以计算文件中的单词。

let stringFileContents = try! NSString(contentsOfFile: "/path/to/file/Localizable.strings", encoding: NSUTF8StringEncoding)
let stringsDictionary = stringFileContents.propertyListFromStringsFileFormat()

var words:[String] = []

stringsDictionary?.forEach({ (key, value) in
    words += value.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
})

// Ignore any word with % in it, since it's probably just a format substitution
let filteredWords = words.filter { $0.containsString("%") == false }

print(filteredWords.count)