Skip to content

Instantly share code, notes, and snippets.

@tejasv02
Created April 28, 2015 22:51
Show Gist options
  • Save tejasv02/f3f47daebc8998ef233c to your computer and use it in GitHub Desktop.
Save tejasv02/f3f47daebc8998ef233c to your computer and use it in GitHub Desktop.
Ruby script to switch locale on Xcode 6.3 / IOS 8.3
require 'plist'
require 'colorize'
class SimLocale
LANG_HASH||={
"en_US" => {"AppleLanguages" => "en", "AppleLocale" => "en_US"},
"en_GB" => {"AppleLanguages" => "en", "AppleLocale" => "en_GB"},
"fr_FR" => {"AppleLanguages" => "fr", "AppleLocale" => "fr_FR"},
"th_TH" => {"AppleLanguages" => "th", "AppleLocale" => "th_TH"},
"es_ES" => {"AppleLanguages" => "es", "AppleLocale" => "es_ES"},
"tr_TR" => {"AppleLanguages" => "tr", "AppleLocale" => "tr_TR"}
}
def change_sim_locale(sim_os, sim_name, sim_locale)
puts "change_sim_locale"
puts "#{sim_os}, #{sim_name}, #{sim_locale}"
sim_path=""
found=false
#get home folder
home_folder=`echo ~`.strip
#navigate to core devices folder
core_devices="#{home_folder}/Library/Developer/CoreSimulator/Devices"
`cd #{core_devices}`
puts "Simulator location #{core_devices}"
#list all dirs
all_sims=Dir["#{core_devices}/*"]
puts all_sims
all_sims.each do |each_sim|
sim_info = Plist::parse_xml("#{each_sim}/device.plist")
if sim_info["name"] == sim_name && sim_info["runtime"].match(sim_os)
sim_path= core_devices+"/"+sim_info["UDID"]
found=true
break
end
end
if !found
puts "ERROR: No compatible simulator found".red
fail ""
end
#execute plist buddy command
global_pref_path=sim_path+"/data/Library/Preferences/.GlobalPreferences.plist"
puts global_pref_path
`echo #{global_pref_path}`
locale= sim_locale
puts LANG_HASH["#{sim_locale}"]
abort if LANG_HASH["#{sim_locale}"]==nil # if locale is not specifed stop tests
puts "#{LANG_HASH["#{locale}"]["AppleLanguages"]}"
puts "#{LANG_HASH["#{locale}"]["AppleLocale"]}"
`/usr/libexec/PlistBuddy #{global_pref_path} -c "Add :AppleLanguages:0 string '#{LANG_HASH["#{locale}"]["AppleLanguages"]}'"`
`/usr/libexec/PlistBuddy #{global_pref_path} -c "Set :AppleLocale '#{LANG_HASH["#{locale}"]["AppleLocale"]}'"`
end
#change_sim_locale(ENV['SIM_OS'],ENV['SIM_NAME'],ENV['LOCALE'])
end
SimLocale.new.change_sim_locale "8-3","iPhone 6 Plus","th_TH"
@lummyare
Copy link

Please how can i solve this issue, I cloned the gistfile1.rb and i cd t the folder and run ruby gistfile1.rb but i get the following error:

lummy:f3f47daebc8998ef233c seunare$ ruby gistfile1.rb
/usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- plist (LoadError) from /usr/local/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire'
from gistfile1.rb:1:in `

'

@kaphacius
Copy link

thanks! this helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment