Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 249
Martin Epsz Avatar asked Nov 13 '15 14:11

Martin Epsz


People also ask

How do I change localization in Xcode?

To do this, in Xcode, select Product > Scheme > Manage Schemes ... Then in the Run/debug tab, change the "Application Language" to your desired language. And now every time you build and run your app from Xcode, it will use that language as the preferred language.

What is localized in Swift?

Localization is the process of translating and adapting your app into multiple languages and regions.


1 Answers

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.

Prerequisites:

Follow this instructions in this answer.

Concept:

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:

  1. Create all the pieces by following the steps in this answer.
  2. From any target, remove the Main.storyboard. Of course, select Remove Reference to keep the storyboard around.
  3. In the Finder, locate Base.lproj. Drag and drop Main.storyboard back to your project, and select every target.
  4. In the File Inspector, ensure that the languages are not selected, and that you are still using Localizable Strings. enter image description here
  5. In the Finder again, locate en.lproj, es.lproj, 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
  6. One last time, in File inspector, associate each Main.strings to the desired target. Repeat for each .strings. enter image description here

Conclusion

By decoupling the .lproj from the .storyboard in the Project Navigator, you can associate files and targets freely.

Demo:

See it at work using a French target, on a device with language set to Français in the Settings:

French example on iPhone 4


► Find this solution on GitHub and additional details on Swift Recipes.

like image 106
SwiftArchitect Avatar answered Sep 28 '22 02:09

SwiftArchitect