静态图书馆与国际化 [英] Static library and internationalization

查看:91
本文介绍了静态图书馆与国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对静态库有疑问.我需要在库中本地化一些文本.因此,我创建了一个捆绑包,在其中放置了不同的本地化文件.然后,我创建了一个像这样的函数:

I have a question regarding static libraries. I need to localize some text inside my library. So I created a bundle where I put my different localized files. Then, I created a function like this :

NSString *MyLocalizedString(NSString* key, NSString* comment)
{
    static NSBundle* bundle = nil;
    if (!bundle)
    {
        NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyStaticLib.bundle"];
        bundle = [[NSBundle bundleWithPath:path] retain];
    }

    return [bundle localizedStringForKey:key value:@"" table:nil];
}

但是当我使用它时,它总是返回英语本地化的字符串(除了我的电话语言是法语).我不知道为什么.

But when I use it, it always return the english localized string (besides my phone langage is French). I do not know why.

推荐答案

当执行完全相同的操作时,我遇到了一个完全相同的问题:我有一个静态库以及一个带有图像,本地化字符串等的同伴包文件.

I've got the very same issue when doing the exactly same: I've a static library and a companion bundle file with image, localized string, etc..

我发现静态函数似乎无法找出正确的设备本地化(很抱歉,但我无法找到此问题的原因),并且通过以下操作已解决:

I've figured out that seems that the static can't figure out the correct device localization (I'm sorry but I wasn't able to find the reason of this issue) and I've fixed by doing this:

@implementation NSBundle (KiosKitAdditions)

+ (NSBundle *)kioskitBundle
{
    static NSBundle* kioskitBundle = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{

        NSString *libraryBundlePath = [[NSBundle mainBundle] pathForResource:KKBundleName
                                                                      ofType:@"bundle"];

        NSBundle *libraryBundle = [[NSBundle bundleWithPath:libraryBundlePath] retain];
        NSString *langID        = [[NSLocale preferredLanguages] objectAtIndex:0];
        NSString *path          = [libraryBundle pathForResource:langID ofType:@"lproj"];
        kioskitBundle           = [[NSBundle bundleWithPath:path] retain];
    });
    return kioskitBundle;
}

@end

如您所见,我使用Class方法创建了一个NSBundle类别,该类别的行为与[NSBundle mainBundle]非常相似,并且为我返回了静态libray的正确捆绑包,因此我可以在任何地方使用它,例如:

As you can see I have created a category of NSBundle with a Class method that act very similar to [NSBundle mainBundle] and that return me the correct bundle for the static libray so I can use it everywhere I want, for example:

#define KKLocalizedString(key) NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle kioskitBundle], @"")

代码非常简单,首先找到静态库包的路径,找到当前的设备语言,然后创建一个新的NSBundle,其路径为library_path/device_language.lproj.

The code is very simple first I find the path for the static library bundle, find the current device language and then I create a new NSBundle whose path is library_path/device_language.lproj .

这种方法的缺点是您必须始终对所有资产进行本地化,如果捆绑包中有很多图像,这可能会很痛苦(但是我认为这不太可能).

A drawback of this approach is that you need to alway localize all of your asset and this can be a pain if you have a lot image in your bundle (but I think this is unlikely).

如果您不想采用我的分类方法,则可以这样更改代码:

If you don't want to adopt my category approach you can change your code like this:

NSString *MyLocalizedString(NSString* key, NSString* comment)
{
    static NSBundle* bundle = nil;
    if (!bundle)
    {
        NSString *libraryBundlePath = [[NSBundle mainBundle] pathForResource:@"MyStaticLib"
                                                                      ofType:@"bundle"];

        NSBundle *libraryBundle = [NSBundle bundleWithPath:libraryBundlePath];
        NSString *langID        = [[NSLocale preferredLanguages] objectAtIndex:0];
        NSString *path          = [libraryBundle pathForResource:langID ofType:@"lproj"];
        bundle                  = [[NSBundle bundleWithPath:path] retain];

    }

    return [bundle localizedStringForKey:key value:@"" table:nil];
}

这篇关于静态图书馆与国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆