Different storyboard localization per target
I have a project with 3 targets. Each target needs to suppport only one language, but the language is not the same for each target.
Currently I have
- Target 1 (es)
- Target 2 (es)
- Target 3 (pt)
Most of my strings are in a Localizable.strings file, and I simply target a different file for each target. However, a number of strings are in my storyboards. My problem is that it does not seem possible to create different storyboard localization files for each target. If I change the targeting of the portuguese file, the targeting of the spanish file changes automatically.
A possible solution is to duplicate each Storyboard for each target, but this won’t scale well as the number of tagets grow. I could also modify all the text with NSLocalizedString in each ViewController, but this seems tedious and error prone.
Is there a better solution?
- Attempting to run multiple tests using Xcode's new UI testing fails
- Swift Playgrounds: Replacement for captureValue(_:withIdentifier:)
- Swift file - how to disable all warnings?
- 'invalid context 0x0' after Migration to iOS 9
- Xcode 7 Could not find a storyboard named
- WatchOS 2 (beta 5): watchAppInstalled returns false
3 Solutions Collect From Internet About “Different storyboard localization per target”
Shared localizations across targets
- Assuming you already have multiple languages in your project, in the Project Navigator, select your storyboard (say Main.storyboard)
In the File Inspector, under Localization, add English. Pick Localizable Strings which is the default. This will create a
Main.stringfor each language, and share your Storyboard across all languages.
Merely clicking that checkbox in step 2. will add a
Main.string (English)for that language, specifically for that
Main.Storyboard. Pay special attention to the new hierarchy, these are not
Main.strings, derived from
- Localize in each language file to your heart content.
For example, starting with a
UILabel which message was in Esperanto, it will appear in English when the device runs in English:
/* Class = "UILabel"; text = "Tiu mesaĝo estas en Esperanto"; ObjectID = "acC-pA-eMt"; */ "acC-pA-eMt.text" = "This message is in English";
Start here to add multiple languages to your project:
In the event you need to add more languages, follow these steps:
- In the Project Navigator, select your Project
In the Project and Target List, select, once again, the Project
- Select the Info tab
- Under Localizations, keep Use Base Internationalization (unless your UI itself must change across languages)
Click +, pick a new language. In the dialog presented to you, select every Storyboard you want to have multiple localizations for.
If you missed Storyboards at Step 5. you can simply add a new language to that a storyboard by following the steps in Assuming you already have multiple languages.
Shared Storyboard across targets
While this answer is the preferred approach, you can assign different Storyboard localizations to different targets, while sharing the same Storyboard across all targets.
Follow this instructions in this answer.
You want to have the
Main.storyboard in each target, but a different
Main.strings in each target. Additionally, you do not want all localizations to be available in each target.
To achieve this, you must manipulate the
.lproj directly, a process which is hidden when merely using the File Inspector.
Step by step:
- Create all the pieces by following the steps in this answer.
- From any target, remove the
Main.storyboard. Of course, select Remove Reverence to keep the storyboard around.
- In the Finder, locate Base.lproj. Drag and drop
Main.storyboardback to your project, and select every target.
- In the File Inspector, ensure that the languages are not selected, and that you are still using
- In the Finder again, locate
fr.lproj, etc. Notice that they each contain a version of
Main.strings. Drag these .lproj (the entire directories) back into your project. This time, do not select any target
- One last time, in File inspector, associate each
Main.stringsto the desired target. Repeat for each
By decoupling the
.lproj from the
.storyboard in the Project Navigator, you can associate files and targets freely.
See it at work using a French target, on a device with language set to Français in the Settings:
► Find this solution on GitHub and additional details on Swift Recipes.
All you need: to make common xcode workspace for your app, then to create a duplicates of main xcodeproj file for your set of languages and make different targets for all of them, also then you can set language for individual project settings in new xcodeproj’s. Also you easy can tune build phase ‘Copy Bundle Resourses’ and others. I use this method. If you have a Xcode’s crash during adding project in workspace – open project with Xcode, rename it and rename or duplicate targets (I think same names cause this issue).
- iPhone GPS Development – Tips + Tricks
- NSTableview View Based Scrolling Performance
- dispatch_semaphore_t reuse – What am I missing here?
- Ionic 2 Facebook Login – 'App Not Setup' & 'Not Logged in' & 'Given URL is not allowed' Errors
- Releasing CGImage (CGImageRef)
- Remove programmatically added UIImageView
- Programmatically setting tabBarItem title in Swift
- How to add two custom cell into tableview in storyboard in Swift?
- Switching from http to https. Invalid certificate
- Detecting the initial launch of an app using NSUserDefaults
- iOS: Can I get the pitch/yaw/roll from accelerometer data?
- Turning an iPhone or iPod into a wireless webcam
- UIImageView changes their position when the UITableViewCell is selected
- Changing an Int to a CGFloat in Swift to return heightForRowAtIndexPath tableview function
- NSDocument to hold a complete folder?