- Fix PNInstallation

This commit is contained in:
giuseppenuc committed 2016-04-05 15:46:06 +02:00
1 parent 1df32f64a7
commit 7752bf3e18
51 files changed
+14486 -12301

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
//
// DJLocalizableString.h
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DJLocalizableString : NSString
- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;
@end
+59
View File
@@ -0,0 +1,59 @@
//
// DJLocalizableString.m
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import "DJLocalizableString.h"
#import "DJLocalizationSystem+Private.h"
static NSString * const kDJLocalizableString = @"NSLocalizableString";
@interface DJLocalizableString ()
@property (nonatomic, strong) NSString *developmentLanguageString;
@property (nonatomic, strong) NSString *stringsFileKey;
@end
@implementation DJLocalizableString
+ (void)load {
@autoreleasepool {
[NSKeyedUnarchiver setClass: DJLocalizableString.class forClassName: kDJLocalizableString];
}
}
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.stringsFileKey = [aDecoder decodeObjectForKey: @"NSKey"];
self.developmentLanguageString = [aDecoder decodeObjectForKey: @"NSDev"];
}
return self;
}
#pragma mark - NSObject
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
return [DJLocalizationSystem.shared localizedStoryboardStringForKey: self.stringsFileKey
value: self.developmentLanguageString];
}
#pragma mark - NSString
- (NSUInteger)length {
return self.developmentLanguageString.length;
}
- (unichar)characterAtIndex:(NSUInteger)index {
return [self.developmentLanguageString characterAtIndex: index];
}
@end
@@ -0,0 +1,15 @@
//
// DJLocalization.h
// DJLocalization
//
// Created by David Jennes on 04/03/15.
// Copyright (c) 2015. All rights reserved.
//
#ifndef DJLocalization_h
#define DJLocalization_h
#import "DJLocalizationSystem.h"
#import "UIStoryboard+DJLocalization.h"
#endif
@@ -0,0 +1,24 @@
//
// DJLocalizationSystem+Private.h
// DJLocalization
//
// Created by David Jennes on 16/02/16.
// Copyright (c) 2015. All rights reserved.
//
#import "DJLocalizationSystem.h"
@interface DJLocalizationSystem (Private)
/*!
* Special version that tries to load a storyboard string from all the tables
* except Localizable and InfoPlist. Defaults to comment if not found.
*
* @param key The localization key
* @param comment The localization value
*
* @return The localized string
*/
- (NSString *)localizedStoryboardStringForKey:(NSString *)key value:(NSString *)comment;
@end
@@ -0,0 +1,64 @@
//
// DJLocalizationSystem.h
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import <Foundation/Foundation.h>
// replace Apple's macros with our own
#undef NSLocalizedString
#define NSLocalizedString(key, comment) \
[DJLocalizationSystem.shared localizedStringForKey:(key) value: @"" table: nil]
#undef NSLocalizedStringFromTable
#define NSLocalizedStringFromTable(key, tbl, comment) \
[DJLocalizationSystem.shared localizedStringForKey:(key) value:@"" table:(tbl)]
@interface DJLocalizationSystem : NSObject
/*!
* Get the system's global instance
*
* @return A localization system instance
*/
+ (instancetype)shared;
/*!
* Get the localized string for a given key/value
*
* @param key The localization key
* @param comment The localization value
* @param tableName The localization table name
* @return The localized string
*/
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment table:(NSString *)tableName;
/*!
* Get the localized string for a given key/value in the specified bundle
*
* @param key The localization key
* @param comment The localization value
* @param bundle The bundle to search in
* @return The localized string
*/
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment table:(NSString *)tableName bundle:(NSBundle *)bundle;
/*!
* Reset the localization to the system's default language
*/
- (void)resetLocalization;
/*!
* The bundle for the currently active language
*/
@property (nonatomic, readonly) NSBundle *bundle;
/*!
* The currently active language
*/
@property (nonatomic, strong) NSString *language;
@end
@@ -0,0 +1,136 @@
//
// DJLocalizationSystem.m
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import "DJLocalization.h"
#import "DJLocalizationSystem+Private.h"
@interface DJLocalizationSystem ()
@property (nonatomic, strong) NSBundle *bundle;
@property (nonatomic, strong) NSDictionary *storyboardStrings;
@property (nonatomic, strong) NSString *customLanguage;
@end
@implementation DJLocalizationSystem
+ (instancetype)shared {
__strong static DJLocalizationSystem *sharedSystem = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSystem = [self new];
});
return sharedSystem;
}
- (instancetype)init {
self = [super init];
if (!self)
return nil;
[self resetLocalization];
return self;
}
- (NSString *)localizedStoryboardStringForKey:(NSString *)key value:(NSString *)comment {
NSString *result = self.storyboardStrings[key];
if (!result)
result = comment;
return result;
}
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
return [self.bundle localizedStringForKey: key value: value table: tableName];
}
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName bundle:(NSBundle *)bundle {
// if no custom language, just get the standard localized string
if (!self.customLanguage)
return [bundle localizedStringForKey: key value: value table: tableName];
// check if our custom language exists
NSString *path = [bundle pathForResource: self.customLanguage ofType: @"lproj"];
if (path)
return [[NSBundle bundleWithPath: path] localizedStringForKey: key value: value table: tableName];
// otherwise return the default
return [bundle localizedStringForKey: key value: value table: tableName];
}
- (void)resetLocalization {
self.language = NSBundle.mainBundle.preferredLocalizations.firstObject;
}
- (void)setLanguage:(NSString*)language {
if ([language isEqualToString: self.customLanguage ])
return;
NSDictionary *languageInfo = [NSLocale componentsFromLocaleIdentifier: language];
// find bundle
NSString *path = [NSBundle.mainBundle pathForResource: language ofType: @"lproj"];
if (path == nil)
path = [NSBundle.mainBundle pathForResource: languageInfo[NSLocaleLanguageCode] ofType: @"lproj"];
// set bundle
if (path) {
self.bundle = [NSBundle bundleWithPath: path];
[self loadTables];
// store language id
self.customLanguage = language;
} else {
NSLog(@"Localization error: no bundle found, resetting!");
self.bundle = NSBundle.mainBundle;
self.customLanguage = nil;
}
}
- (NSString*)language {
if (self.customLanguage)
return self.customLanguage;
else {
NSString *language = NSLocale.preferredLanguages.firstObject;
NSDictionary *languageInfo = [NSLocale componentsFromLocaleIdentifier: language];
return languageInfo[NSLocaleLanguageCode];
}
}
#pragma mark - Helper methods
- (void)loadTables {
NSMutableDictionary *strings = [NSMutableDictionary new];
// find strings files
NSFileManager *fileManager = NSFileManager.defaultManager;
NSURL *bundleURL = self.bundle.bundleURL;
NSArray *contents = [fileManager contentsOfDirectoryAtURL: bundleURL
includingPropertiesForKeys: @[]
options: NSDirectoryEnumerationSkipsHiddenFiles
error: nil];
// load them
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"pathExtension == 'strings'"];
for (NSURL *fileURL in [contents filteredArrayUsingPredicate: predicate]) {
if ([fileURL.lastPathComponent isEqualToString: @"Localizable.strings"])
continue;
if ([fileURL.lastPathComponent isEqualToString: @"InfoPlist.strings"])
continue;
NSDictionary *table = [NSDictionary dictionaryWithContentsOfURL: fileURL];
[strings addEntriesFromDictionary: table];
}
self.storyboardStrings = strings;
}
@end
@@ -0,0 +1,21 @@
//
// UIStoryboard+DJLocalization.h
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIStoryboard (DJLocalization)
/*!
* Get the storyboard for the active language
*
* @param name The storyboard's name
* @return The desired storyboard
*/
+ (instancetype)dj_storyboardWithName:(NSString *)name;
@end
@@ -0,0 +1,19 @@
//
// UIStoryboard+DJLocalization.m
// DJLocalization
//
// Created by David Jennes on 15/02/15.
// Copyright (c) 2015. All rights reserved.
//
#import "UIStoryboard+DJLocalization.h"
#import "DJLocalizationSystem.h"
@implementation UIStoryboard (DJLocalization)
+ (instancetype)dj_storyboardWithName:(NSString *)name {
return [self storyboardWithName: name bundle: DJLocalizationSystem.shared.bundle];
}
@end