Handy Scripts For Localizing iOS XIB Files

Recently I have been working on more and more iOS projects that require localization. One of the things that I find needs to be done fairly often while implementing localized XIB files is to extract all the strings from your English XIBs. The corollary is equally important which is creating new localized XIB files for all your supported languages from the .strings files that are provided to you by your translator.

Doing this by hand for even one additional language would be tedious if there are more than a couple of XIB files. This is exactly the kind of repetitive task that scripts are perfect for. My scripting language of choice is Ruby so you will need that installed to use these or you should be able to easily adapt these to whatever language you prefer.

Here is my genxibstrings script that I use to generate the .strings files from all my English XIB files. To use it just run it in your en.lproj folder which should contain all your English XIB files.

#!/usr/bin/env ruby

# Script to generate strings for localization from all XIB files in the current directory.

xibs = []

puts "Processing files..."

Dir.glob("*.xib") do |file| 
  fname = File.basename(file, '.*')
  success = system "ibtool --generate-strings-file #{fname}.strings #{file}"
  if success == true
    puts "#{file} --> #{fname}.strings"
    xibs.push file
  else
    puts "Warning! String file for #{file} could not be generated."
  end
end
  
count = xibs.length

puts "Generated Strings For #{count} XIB Files."

Next, here is my stringstoxibs script that creates all the localized XIB files from your .strings files and English XIBs. This script needs to be run in a directory that contains all the localized language folders which have names like en.lproj, fr.lproj, zh-hant.lproj, es.lproj etc. The en.lproj folder must contain all your English XIB files that were used to create your .strings files that you sent off to be translated. All the other .lproj folders must contain the translated .strings files for their language. When this script is done it will have made all the localized XIB files in each .lproj folder and removed all the .strings files which you no longer need.

#!/usr/bin/env ruby

# Script to create localized XIBs from localized strings.

xibs = []

puts "Processing files..."

Dir.glob("**/*.strings") do |file| 
  dname = File.dirname(file)
  fname = File.basename(file, '.*')
  if fname != "Localizable"
    success = system "ibtool --strings-file #{file} ./en.lproj/#{fname}.xib --write #{dname}/#{fname}.xib"
    if success == true
      puts "#{file} --> ./#{dname}/#{fname}.xib"
      xibs.push file
      system "rm ./#{dname}/#{fname}.strings"
    else
      puts "Warning! XIB for #{file} could not be generated."
    end
  end  
end
  
count = xibs.length

puts "Generated #{count} XIB Files."

Hope this helps speed up the task of localizing your iOS apps.

Share and Enjoy:
  • printfriendly Handy Scripts For Localizing iOS XIB Files
  • digg Handy Scripts For Localizing iOS XIB Files
  • stumbleupon Handy Scripts For Localizing iOS XIB Files
  • delicious Handy Scripts For Localizing iOS XIB Files
  • facebook Handy Scripts For Localizing iOS XIB Files
  • yahoobuzz Handy Scripts For Localizing iOS XIB Files
  • twitter Handy Scripts For Localizing iOS XIB Files
  • googlebookmark Handy Scripts For Localizing iOS XIB Files
  • reddit Handy Scripts For Localizing iOS XIB Files

About jjob

I'm a technologist and a music producer. I make sound, write code and build things with electronics and microcontrollers.
This entry was posted in app, code, Interface Builder, iOS, iPad, iPhone, Ruby, script and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>