一般要幫 iOS App 本地化,其中比較繁瑣的就是翻譯 xib 檔案中的字串,通常是需要下面三個步驟 (可參考 ICanLocalize 的教學):

  • 使用 ibtool 把字串從 xib 中抓出來
  • 找人翻譯字串
  • 使用 ibtool 再把翻譯好的字串放到 xib 中

不過我是不能忍受這些工作要手動進行的,所以最好的方式是用 rake 來自動化這些事情:

DEF_LPROJ = 'en.lproj'
REGIONS = Dir['*.lproj'].delete_if {|p| p == DEF_LPROJ}.map {|p| File.basename(p, '.lproj')}
XIBS = Dir["#{DEF_LPROJ}/*.xib"].map {|x| File.basename(x)}

namespace :l10n do

  task :regions do
    REGIONS.each {|region| puts region}
  end

  task :xibs do
    XIBS.each {|xib| puts xib}
  end

  desc 'Generate strings file for XIB'
  task :genstrings do
    REGIONS.each do |r|
      XIBS.each do |xib|
        sh "ibtool --generate-strings-file #{r}.lproj/#{xib}.strings #{DEF_LPROJ}/#{xib}"
      end
    end
  end

  desc 'Apply strings files to XIB'
  task :usestrings do
    REGIONS.each do |r|
      XIBS.each do |xib|
        sh "ibtool --strings-file #{r}.lproj/#{xib}.strings #{DEF_LPROJ}/#{xib} --write #{r}.lproj/#{xib}"
      end
    end
  end

end

首先請 rake 幫忙把字串檔都產生出來:

$ rake l10n:genstrings

翻譯完這些字串檔後,再請 rake 幫你放進去:

$ rake l10n:usestings

不管你有幾個語言或是幾個 xib 檔都是一樣的步驟。