From 5a18ccc5e100a43d98ccaf4bdf0ad4f8e49bc76f Mon Sep 17 00:00:00 2001 From: Tomáš Znamenáček Date: Fri, 6 Nov 2015 08:59:52 +0100 Subject: Even more elaborate workaround for the CocoaPods localization problem (#74). Just testing if the CocoaPods bundle is present didn’t work, so now we even try to load a localized string from the bundle. --- Framework/MASLocalization.m | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'Framework/MASLocalization.m') diff --git a/Framework/MASLocalization.m b/Framework/MASLocalization.m index d5f0c8a..e0627db 100644 --- a/Framework/MASLocalization.m +++ b/Framework/MASLocalization.m @@ -1,6 +1,9 @@ #import "MASLocalization.h" #import "MASShortcut.h" +static NSString *const MASLocalizationTableName = @"Localizable"; +static NSString *const MASPlaceholderLocalizationString = @"XXX"; + // The CocoaPods trickery here is needed because when the code // is built as a part of CocoaPods, it won’t make a separate framework // and the Localized.strings file won’t be bundled correctly. @@ -9,10 +12,28 @@ NSString *MASLocalizedString(NSString *key, NSString *comment) { static NSBundle *localizationBundle = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ + NSBundle *frameworkBundle = [NSBundle bundleForClass:[MASShortcut class]]; NSURL *cocoaPodsBundleURL = [[NSBundle mainBundle] URLForResource:@"MASShortcut" withExtension:@"bundle"]; - localizationBundle = cocoaPodsBundleURL ? - [NSBundle bundleWithURL:cocoaPodsBundleURL] : - [NSBundle bundleForClass:[MASShortcut class]]; + if (cocoaPodsBundleURL) { + NSBundle *cocoaPodsBundle = [NSBundle bundleWithURL:cocoaPodsBundleURL]; + NSString *testingString = [cocoaPodsBundle + localizedStringForKey:@"Cancel" + value:MASPlaceholderLocalizationString + table:MASLocalizationTableName]; + if (![testingString isEqualToString:MASPlaceholderLocalizationString]) { + // We have a CocoaPods bundle and it works, use it. + localizationBundle = cocoaPodsBundle; + } else { + // We have a CocoaPods bundle, but it doesn’t contain + // the localization files. Let’s use the framework bundle instead. + localizationBundle = frameworkBundle; + } + } else { + // CocoaPods bundle not present, use the framework bundle. + localizationBundle = frameworkBundle; + } }); - return [localizationBundle localizedStringForKey:key value:@"XXX" table:@"Localizable"]; + return [localizationBundle localizedStringForKey:key + value:MASPlaceholderLocalizationString + table:MASLocalizationTableName]; } -- cgit v1.2.3