【问题标题】:iOS Localizable Strings Word CountiOS 可本地化字符串字数
【发布时间】:2025-12-15 13:35:01
【问题描述】:

我需要计算翻译一个 iOS 应用程序的成本。价格以字数为准。我已经在 Localizable.strings 文件中拥有了我需要翻译的所有内容。

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

【问题讨论】:

  • 您是否考虑过基本的命令行工具,例如wc
  • 据我所知,这不会从我的 .string 文件中排除 cmets 和密钥。
  • 如果没有样本就无法知道您的文件是什么样子。

标签: ios localization translation localizable.strings


【解决方案1】:

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

# 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 脚本技能很差!如果您的字符串包含转义的 " 或 = 字符,此脚本可能会中断,因此需要稍微收紧。它也做得很糟糕,但应该做您需要做的事情!

【讨论】:

    【解决方案2】:

    我结束了用 Swift 编写的 sn-p,我在一个新的 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)
    

    【讨论】: