xcode 静态库中字符串的本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2335664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Localization of strings in static lib
提问by AOO
I have a project that uses a static library (SL). In that SL, there are a couple of strings I'd like to localize and the project includes all of the localization files. The localization works just fine when storing all text translations in the same file. The thing is that I'd like to separate the SL strings from the other strings. I have tried to put two different *.strings files (Localizable.strings and Localizable2.strings) in the language folder of interest but that did not work. I have also tried to use two *.strings file with the same name (Localizable.strings) but with different paths. It didn't work either. It seems that only one localization file is supported, right? Could anyone suggest a good way of doing this? I'm using SDK 3.2 beta 2.
我有一个使用静态库 (SL) 的项目。在该 SL 中,有几个字符串我想本地化,并且该项目包含所有本地化文件。将所有文本翻译存储在同一文件中时,本地化工作得很好。问题是我想将 SL 琴弦与其他琴弦分开。我试图将两个不同的 *.strings 文件(Localizable.strings 和 Localizable2.strings)放在感兴趣的语言文件夹中,但没有奏效。我还尝试使用两个具有相同名称(Localizable.strings)但路径不同的 *.strings 文件。它也没有用。好像只支持一个本地化文件吧?任何人都可以建议这样做的好方法吗?我正在使用 SDK 3.2 beta 2。
回答by abuharsky
It is not possible to bundle it in a static lib, but you can create new bundle like "MyStaticLibraryName.bundle", put inside all localizations and use the code below instead "NSLocalizedString()". All you need to do: add a static library and resource bundle.
不可能将它捆绑在静态库中,但您可以创建新的包,例如“ MyStaticLibraryName.bundle”,放入所有本地化并使用下面的代码代替“ NSLocalizedString()”。您需要做的就是:添加一个静态库和资源包。
NSString *MyLocalizedString(NSString* key, NSString* comment) {
static NSBundle* bundle = nil;
if (!bundle) {
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyStaticLibraryName.bundle"];
bundle = [[NSBundle bundleWithPath:path] retain];
}
return [bundle localizedStringForKey:key value:key table:nil];
}
回答by marcelnijman
Putting files with the same name intro one project never works, because in the resulting app they end up all in the same location. (Xcode doesn't preserve your directory structure.)
将同名文件放在一个项目中是行不通的,因为在生成的应用程序中,它们最终都在同一个位置。(Xcode 不会保留您的目录结构。)
But you can put part of your localization into Localizable2.strings and then use:
但是您可以将部分本地化放入 Localizable2.strings 中,然后使用:
NSLocalizedStringFromTable(@"key", @"Localizable2", @"")
NSLocalizedStringFromTable(@"key", @"Localizable2", @"")
回答by jithinroy
Make the localizable string for the static library, then place that string file in a folder "YourLibraryResource". Rename the folder "YourLibraryResource.bundle".
为静态库制作可本地化的字符串,然后将该字符串文件放在文件夹“YourLibraryResource”中。重命名文件夹“YourLibraryResource.bundle”。
Now you include this bundle also in the project along with the library. Then use the code given by abuharsky.
现在,您还将此包与库一起包含在项目中。然后使用 abuharsky 给出的代码。