2012-09-18 104 views
3

我有一個關於靜態庫的問題。我需要在我的圖書館中本地化一些文本。所以我創建了一個捆綁包,放置了我的不同本地化文件。然後,我創造了這樣的功能:靜態庫和國際化

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

但是,當我使用它,它總是返回英文本地化的字符串(除了我的手機langage是法國人)。我不知道爲什麼。

回答

3

我有做的完全一樣的,當同樣的問題:我有一個靜態庫,並用圖像的同伴捆綁文件,本地化的字符串,等等。

我已經想通了,看來,靜態想不通正確的設備本地化(我很抱歉,但我沒能找到這個問題的原因),我已經做這個固定的:

@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 

正如你所看到的我用類方法創建了一個NSBundle類,它的行爲與[NSBundle mainBundle]非常相似,並且返回了靜態庫的正確包,因此我可以使用它ywhere我想要的,例如:

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

的代碼是很簡單的一我找到靜態庫包的路徑,找到當前設備的語言,然後我創建一個新的一個NSBundle的路徑是LIBRARY_PATH/device_language.lproj 。

這種方法的缺點是您需要全程本地化您的所有資產,如果您的包中有很多圖像(但我認爲這不太可能),這可能會很痛苦。

如果你不希望採納我的類中的方法,你可以改變你這樣的代碼:

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

感謝盧卡,它現在完美:) – Niko

+0

不客氣,請讓我知道,如果你能找到是什麼原因造成的問題。 –

+0

的確,但沒有問題,我只有很少的圖像。 – Niko

1

我不得不修改盧卡的回答得到它的工作我想怎樣。我的問題非常相似:從消費應用程序使用我捆綁的本地化字符串不能解決糾正[lang] .lproj文件夾。就我而言,我有一個我想查找的es.lproj文件夾。但是,如果用戶將其首選語言設置爲es-MX,則使用[NSLocale preferredLanguages] objectAtIndex:0]將嘗試查找不存在的es-MS.lproj文件夾。

@implementation NSBundle (CCPAdditions) 

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

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

     NSBundle *libraryBundle = [NSBundle bundleWithPath:libraryBundlePath]; 

     for (NSString *langID in [NSLocale preferredLanguages]) 
     { 
      NSString *path = [libraryBundle pathForResource:langID ofType:@"lproj"]; 
      if (path) 
      { 
       corePaymentBundle   = [NSBundle bundleWithPath:path]; 
       break; 
      } 
     } 
    }); 

    return corePaymentBundle; 
} 

@end 

這遍歷首選語言列表,並檢查是否存在該路徑。如果es-MX.lproj丟失,它將接下來檢查es.lproj等,並最終找到en.lproj,如果沒有其他東西存在。