IOS/iPhone:嵌套 Localized.strings 文件?
我正在创建一个有多种变体的应用程序。
这些变体将由多个 Localized.strings 文件中的几个字符串控制。
然而,90% 的字符串将保持不变。
我希望拥有它,以便每个唯一的 Localized.strings 文件都会导入“主”文件,这样当我想要更改公共字符串时,我就不必遍历每个应用程序变体文件。
这可能吗?
I'm creating an app that will have a number of variants.
These variants will be controlled by a few strings in multiple Localizable.strings files.
However, 90% of the strings will remain the same.
I'd like to have it so that each of the unique Localizable.strings files imports the "master" one, so that I don't have to go through each of the app variant files when I want to change a common string.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两种替代方案:
使用
NSLocalizedString
周围的自定义包装函数来为您进行分层查找,然后回退到默认情况下的NSLocalizedString
机制。我之前写过一个关于如何执行此操作的答案:可以在运行时添加 .strings 资源文件吗?
每种语言使用多个 .strings 文件。默认情况下,内容在“Localized.strings”中查找,但 NSLocalizedStringFromTable() 函数允许您指定自定义“表”名称。因此,如果您传入“Configuration”作为表名,它将在“Configuration.strings”文件中查找内容。
我开发了一个可以执行此操作的应用程序。我们有两个不同的 .strings 文件。一个控制品牌设置(使用什么名称、使用什么图像、使用什么服务器等),另一个包含所有实际翻译的字符串。应用程序包中只有一个品牌信息副本,我们选择了在编译时与某些自定义脚本一起使用的正确变体。它工作得非常好。
Two alternatives:
Use custom wrapper functions around
NSLocalizedString
that do your hierarchical lookup for you, and then fall back to theNSLocalizedString
mechanism for the default case.I previously wrote an answer about how to do this: Can .strings resource files be added at runtime?
Use multiple .strings files per language. By default things are looked up in "Localizable.strings", but the
NSLocalizedStringFromTable()
function allows you to specify a custom "table" name. So if you passed in "Configuration" as the table name, it would look things up in the "Configuration.strings" file.I worked on an app that did this. We had two different .strings files. One controlled the branding settings (what name to use, what images to use, what servers to use, etc), and the other contained all of the actual translated strings. There would only be a single copy of the branding information in the app bundle, and we chose the correct variant to use at compile time with some custom scripts. It worked really really well.
不幸的是,Apple 的
Localized.strings
文件不支持这一点。您可以通过自定义 Ruby 或 Python 脚本或使用
sed
的 Bash 脚本来模拟该功能;该脚本可以获取根.strings
文件并将内容复制到每个变体的.strings
文件中。它甚至可以是 Xcode 中的自定义构建步骤。Unfortunately, Apple doesn't support that for its
Localizable.strings
files.You could mimic that feature through a custom Ruby or Python script or just a Bash script using
sed
; the script could take the root.strings
file and copy the contents into each variant's.strings
file. It could even be a custom build step in Xcode.我不确定你想要什么,但如果你只想翻译一些字符串,你可以。
你可以只翻译这些字符串的一部分,如果没有翻译,ios将选择应用程序语言中的字符串并显示它。因此,应用程序语言中的 Localized.strings 可以被视为主字符串。
I'm not sure of what you want but if you just want to translate a few strings you could.
You can just translate a part of these strings, if there is no translation, ios will select the strings in the application language and display it. So the Localizable.strings in the application language could be consider like the master one.
好的。这就是我为解决这个问题所做的事情。
我从 Git 存储库中删除了各种 Localized.strings 文件,但将它们保留在各自的位置(每个应用程序变体目录中都有一个)。
我只取了每个变体之间不同的几行,并将它们分成一个单独的文本文件,称为“MyLocalized.strings”。我将这些都添加到了 Git 中,并将它们添加到了项目中,但没有将它们与目标关联。我留下了与每个目标关联的 Localized.strings 文件。
因此,我们所拥有的是每个目标都有一个与目标关联的 Localized.strings 文件,并将被复制到捆绑包中。每个目标目录都有一个名为“MyLocalized.strings”的文件,其中仅包含几个因目标而异的字符串。
然后,我将绝大多数字符串 - 那些不会改变的字符串 - 并将它们放入另一个名为“MyLocalizes.strings”的文件中,并将其放置在中央(公共)目录中。我再次将其添加到 Git 和项目中,但没有将其与目标关联。
然后,我编写了一个非常小、可怜的 Perl 脚本,在给定目标中从公共文件构造一个 Localized.strings 文件,并附加特定于目标的文件。这将覆盖现有的 Localized.strings 文件。这意味着 Localized.strings 文件对于脚本的每次运行都是新的。该脚本被放置在公共区域中,并添加到项目中,而没有将其与目标关联。该脚本有一个参数,即目标的名称(及其目录)。该脚本的语法是:
然后,我在复制捆绑资源步骤之前添加运行脚本构建步骤。该脚本使用以下语法调用 Perl 脚本:
进行构建时,将组合文件,并将组合后的文件放入包中。
OK. Here's what I did to solve it.
I removed the various Localizable.strings file from the Git repository, but left them in their locations (one in each of the app variant directories).
I took just the few lines that varied between each variant, and broke them into a separate text file, called "MyLocalizable.strings". I added each of these to Git, and added them to the project WITHOUT ASSOCIATING THEM WITH A TARGET. I left the Localizable.strings file associated with each target.
So, what we have, is each target has a Localizable.strings file that is associated with a target, and will be copied into the bundle. Each target directory has a file called "MyLocalizable.strings", that contains only the few strings that vary from target to target.
I then took the vast majority of the strings -the ones that don't change- and put them into another file called "MyLocalizable.strings", and placed this within the central (common) directory. Again, I added this to Git and to the project, WITHOUT ASSOCIATING IT WITH A TARGET.
I then wrote a very small, pathetic Perl script to construct a Localizable.strings file in a given target, from the common file, with the target-specific file appended. This overwrites the existing Localizable.strings file. This means that the Localizable.strings file is new for each run of the script. This script was placed in the common area, and added to the project WITHOUT ASSOCIATING IT WITH A TARGET. The script has one argument, which is the name of the target (and its directory). The syntax of this script is:
I then add a Run Script build step BEFORE the Copy Bundle Resources step. This script calls the Perl script with the following syntax:
When the build is made, the files are combined, and the combined file is put into the bundle.
为所有具有公共字符串的目标创建一个
Localized.strings
。还为每个目标创建TargetSpecificLocalized.strings
并将目标特定字符串放入其中。然后对于通用字符串使用和目标特定字符串使用
Create one
Localizable.strings
for all targets with common strings. Also createTargetSpecificLocalizable.strings
for each target and put target specific strings in it. Then for common strings useand for target specific strings use