Localization of strings in static lib

早过忘川 提交于 2019-12-02 20:49:25
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.

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];
}

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

But you can put part of your localization into Localizable2.strings and then use:

NSLocalizedStringFromTable(@"key", @"Localizable2", @"")

Make the localizable string for the static library, then place that string file in a folder "YourLibraryResource". Rename the folder "YourLibraryResource.bundle".

Now you include this bundle also in the project along with the library. Then use the code given by abuharsky.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!