问题描述
我有一个包含3个目标的项目。每个目标只需要支持一种语言,但每种目标的语言都不一样。
目前我有
- 目标1(es)
- 目标2(es)
- 目标3(pt)
我的大多数字符串都在Localizable.strings文件中,我只是为每个目标定位一个不同的文件。但是,我的故事板中有许多字符串。我的问题是,似乎不可能为每个目标创建不同的故事板本地化文件。如果我更改了葡萄牙语文件的定位,则西班牙语文件的定位会自动更改。
可能的解决方案是为每个目标复制每个Storyboard,但这会赢得'随着塔格数的增长,规模也会很大。我也可以在每个ViewController中使用NSLocalizedString修改所有文本,但这看起来很乏味且容易出错。
有更好的解决方案吗?
跨目标的共享故事板
虽然
en.lproj
, es.lproj
, fr.lproj
等。请注意,它们每个都包含的版本Main.strings
。将这些.lproj(整个目录)拖回到项目中。这次,不选择任何目标 Main.strings
到期望的目标。对每个 .strings
重复上述步骤。 结论
通过从项目中的 .storyboard
中解耦 .lproj
导航器,您可以自由地关联文件和目标。
演示:
参见它在使用法语目标的工作中,在设置中语言设置为Français的设备上:
►在以及有关的其他详细信息。
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?
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:
- 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.storyboard
back to your project, and select every target. - In the File Inspector, ensure that the languages are not selected, and that you are still using
Localizable Strings
. - In the Finder again, locate
en.lproj
,es.lproj
,fr.lproj
, etc. Notice that they each contain a version ofMain.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.strings
to the desired target. Repeat for each.strings
.
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:
► Find this solution on GitHub and additional details on Swift Recipes.
这篇关于每个目标的不同故事板本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!