aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/MASLocalization.m
blob: e0627db1be8e9c40ac3f59e34c9e6e4d4cd235c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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 wont make a separate framework
// and the Localized.strings file wont be bundled correctly.
// See https://github.com/shpakovski/MASShortcut/issues/74
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"];
        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 doesnt contain
                // the localization files. Lets use the framework bundle instead.
                localizationBundle = frameworkBundle;
            }
        } else {
            // CocoaPods bundle not present, use the framework bundle.
            localizationBundle = frameworkBundle;
        }
    });
    return [localizationBundle localizedStringForKey:key
        value:MASPlaceholderLocalizationString
        table:MASLocalizationTableName];
}