9
votes

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:

  1. Target A uses English + multiple non-English localized strings files.
  2. Target B uses only 1 non-English localized strings file.
  3. Target B cannot have English strings included in App Store builds.

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:

enter image description here
enter image description here


enter image description here
enter image description here

1
You might be able to do it using Xcodeproj, though I don't know how. I know that checking those adds some lines to your project.pbxproj file.Iulian Onofrei

1 Answers

9
votes

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")