IOS/iPhone:嵌套 Localized.strings 文件?

发布于 2024-12-08 21:57:14 字数 199 浏览 0 评论 0原文

我正在创建一个有多种变体的应用程序。

这些变体将由多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

世态炎凉 2024-12-15 21:57:14

两种替代方案:

  1. 使用 NSLocalizedString 周围的自定义包装函数来为您进行分层查找,然后回退到默认情况下的 NSLocalizedString 机制。

    我之前写过一个关于如何执行此操作的答案:可以在运行时添加 .strings 资源文件吗?

  2. 每种语言使用多个 .strings 文件。默认情况下,内容在“Localized.strings”中查找,但 NSLocalizedStringFromTable() 函数允许您指定自定义“表”名称。因此,如果您传入“Configuration”作为表名,它将在“Configuration.strings”文件中查找内容。

    我开发了一个可以执行此操作的应用程序。我们有两个不同的 .strings 文件。一个控制品牌设置(使用什么名称、使用什么图像、使用什么服务器等),另一个包含所有实际翻译的字符串。应用程序包中只有一个品牌信息副本,我们选择了在编译时与某些自定义脚本一起使用的正确变体。它工作得非常好。

Two alternatives:

  1. Use custom wrapper functions around NSLocalizedString that do your hierarchical lookup for you, and then fall back to the NSLocalizedString mechanism for the default case.

    I previously wrote an answer about how to do this: Can .strings resource files be added at runtime?

  2. 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.

回忆那么伤 2024-12-15 21:57:14

不幸的是,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.

︶ ̄淡然 2024-12-15 21:57:14

我不确定你想要什么,但如果你只想翻译一些字符串,你可以。

你可以只翻译这些字符串的一部分,如果没有翻译,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.

挽梦忆笙歌 2024-12-15 21:57:14

好的。这就是我为解决这个问题所做的事情。

我从 Git 存储库中删除了各种 Localized.strings 文件,但将它们保留在各自的位置(每个应用程序变体目录中都有一个)。

我只取了每个变体之间不同的几行,并将它们分成一个单独的文本文件,称为“MyLocalized.strings”。我将这些都添加到了 Git 中,并将它们添加到了项目中,但没有将它们与目标关联。我留下了与每个目标关联的 Localized.strings 文件。

因此,我们所拥有的是每个目标都有一个与目标关联的 Localized.strings 文件,并将被复制到捆绑包中。每个目标目录都有一个名为“MyLocalized.strings”的文件,其中仅包含几个因目标而异的字符串。

然后,我将绝大多数字符串 - 那些不会改变的字符串 - 并将它们放入另一个名为“MyLocalizes.strings”的文件中,并将其放置在中央(公共)目录中。我再次将其添加到 Git 和项目中,但没有将其与目标关联。

然后,我编写了一个非常小、可怜的 Perl 脚本,在给定目标中从公共文件构造一个 Localized.strings 文件,并附加特定于目标的文件。这将覆盖现有的 Localized.strings 文件。这意味着 Localized.strings 文件对于脚本的每次运行都是新的。该脚本被放置在公共区域中,并添加到项目中,而没有将其与目标关联。该脚本有一个参数,即目标的名称(及其目录)。该脚本的语法是:

#!/usr/bin/perl

use strict;         # I'm anal. What can I say?
use Cwd;            # We'll be operating on the working directory.
use File::Path;

my $input1File = cwd()."/BMLT/Supporting\ Files/en.lproj/MyLocalizable.strings";
my $input2File = cwd()."/".$ARGV[0]."/en.lproj/MyLocalizable.strings";
my $outputFile = cwd()."/".$ARGV[0]."/en.lproj/Localizable.strings";

open ( MAIN_FILE, $input1File ) || die ( "Could not open main file!" );

my @file_data = <MAIN_FILE>;

close ( MAIN_FILE );

open ( PRODUCT_FILE, $input2File ) || die ( "Could not open product file!" );

push ( @file_data, <PRODUCT_FILE> );

close ( PRODUCT_FILE );

open ( FINAL_FILE, ">$outputFile" ) || die ( "Could not open destination file!" );

foreach ( @file_data ) print FINAL_FILE $_;

close ( FINAL_FILE );

然后,我在复制捆绑资源步骤之前添加运行脚本构建步骤。该脚本使用以下语法调用 Perl 脚本:

${PROJECT_DIR}/BMLT/Supporting\ Files/buildLocalizationFile.pl ${PRODUCT_NAME}

进行构建时,将组合文件,并将组合后的文件放入包中。

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:

#!/usr/bin/perl

use strict;         # I'm anal. What can I say?
use Cwd;            # We'll be operating on the working directory.
use File::Path;

my $input1File = cwd()."/BMLT/Supporting\ Files/en.lproj/MyLocalizable.strings";
my $input2File = cwd()."/".$ARGV[0]."/en.lproj/MyLocalizable.strings";
my $outputFile = cwd()."/".$ARGV[0]."/en.lproj/Localizable.strings";

open ( MAIN_FILE, $input1File ) || die ( "Could not open main file!" );

my @file_data = <MAIN_FILE>;

close ( MAIN_FILE );

open ( PRODUCT_FILE, $input2File ) || die ( "Could not open product file!" );

push ( @file_data, <PRODUCT_FILE> );

close ( PRODUCT_FILE );

open ( FINAL_FILE, ">$outputFile" ) || die ( "Could not open destination file!" );

foreach ( @file_data ) print FINAL_FILE $_;

close ( FINAL_FILE );

I then add a Run Script build step BEFORE the Copy Bundle Resources step. This script calls the Perl script with the following syntax:

${PROJECT_DIR}/BMLT/Supporting\ Files/buildLocalizationFile.pl ${PRODUCT_NAME}

When the build is made, the files are combined, and the combined file is put into the bundle.

路弥 2024-12-15 21:57:14

为所有具有公共字符串的目标创建一个 Localized.strings。还为每个目标创建 TargetSpecificLocalized.strings 并将目标特定字符串放入其中。然后对于通用字符串使用

NSLocalizedString("sample string", comment: "")

和目标特定字符串使用

NSLocalizedString("sample string", tableName: "TargetSpecificLocalizable", bundle: Bundle.main, value: "", comment: "")

Create one Localizable.strings for all targets with common strings. Also create TargetSpecificLocalizable.strings for each target and put target specific strings in it. Then for common strings use

NSLocalizedString("sample string", comment: "")

and for target specific strings use

NSLocalizedString("sample string", tableName: "TargetSpecificLocalizable", bundle: Bundle.main, value: "", comment: "")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文