Over the holidays, I pondered on how I could make these scripts for slurping up localizable strings in RubyMotion projects easier for myself or other developers.

I then came up with the idea to make this a ruby gem! Introducing LocaMotion.

It currently has two commands: ‘slurp’ and ‘generate’

Let’s start with the one you might already be familiar with: calling ‘locamotion generate’ from the command line will run the script from last post. That is, it looks at the strings in the English localizable file and copies them into those of the other languages, should these not already contain said string.

The second command is called ‘locamotion slurp’. What this does is it looks into the app folder of a RubyMotion project and extracts every localized string in the SugarCube format. That is, for example:


Once locamotion finishes gathering up the localizable strings, it looks in the ‘resources/en.lproj/Localizable.strings’ file, and adds each string if it doesn’t exist yet in the localizable file. The script is as follows

ruby Slurp matches = [] Dir.glob('app/**/*.rb') do |ruby_file| File.open(ruby_file).each do |line| matches += line.scan(/"([^"]+)"\._/) matches += line.scan(/'([^']+)'\._/) end end matches.flatten! FileUtils.mkdir_p('resources/en.lproj') unless File.exist?('resources/en.lproj') localizable = 'resources/en.lproj/Localizable.strings' system("touch #{localizable}") strings_added_count = 0 matches.each do |string| unless File.open(localizable).read =~ /"#{Regexp.quote(string)}"\s*=\s*"#{Regexp.quote(string)}";/ File.open(localizable, 'a+') { |f| f.write("\"#{string}\" = \"#{string}\";\n") } added = true end end

These two commands make setting myself up for localizing the strings in my RubyMotion apps a lot faster.

Creating the LocaMotion gem was also quite the learning experience! But that’s for another blog post. ‘Til then!

Buy me a coffee