I have a project with multiple app targets and need to be able to add an existing English localized string file to one of the targets for development use only.
Here is my scenario:
However, to help during development we currently manually add the English strings to Target B's localization files (using the existing file from Target A when prompted), and remove it prior to App Store submission.
Since we run Fastlane setup/teardown scripts already, I would like to automate adding/removing the English strings from the scripts so we don't have to do it manually every time.
In the Fastfile, I know how to add a file to Target B, but since the Localization files/references are structured a bit differently in Xcode than regular files I am not sure what the proper way to do it is.
Here is what I have so far:
def add_english_localization()
require 'xcodeproj'
project = Xcodeproj::Project.open("../Code/#{XCODE_PROJ}")
app_target = project.targets.first #Target B
english_file_ref = project.main_group.new_file('../Code/TargetA/Application/Supporting Files/en.lproj') #Existing english file in Target A's directory
app_target.add_file_references([english_file_ref]) #This adds the file but doesn't properly update Xcode's Localization references...?
project.save
end
Screenshots:
Here's a little ruby script, using xcodeproj to remove and add localization:
To remove language (french in this example):
require 'xcodeproj'
project_path = './Whatever.xcodeproj'
project = Xcodeproj::Project.open(project_path)
for o in project.objects do
if o.is_a? Xcodeproj::Project::Object::PBXGroup
if o.hierarchy_path == "/TargetA/Localizable.strings"
group = o
break
end
end
end
files = group.files
for file in files do
if file.path == "fr.lproj/Localizable.strings"
file.remove_from_project
puts "Removed " + file.path
end
end
project.save
To add language (also french):
require 'xcodeproj'
project_path = './Whatever.xcodeproj'
project = Xcodeproj::Project.open(project_path)
for o in project.objects do
if o.is_a? Xcodeproj::Project::Object::PBXGroup
if o.hierarchy_path == "/TargetA/Localizable.strings"
group = o
break
end
end
end
file = project.new_file("fr.lproj/Localizable.strings")
file.move(group)
file.name = "fr"
project.save
You should be able to call it in fastfile, but I didn't check that... You could call it directly using sh("ruby ./name.rb")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With