- 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
+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2015 David Jennes <david.jennes@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+64
View File
@@ -0,0 +1,64 @@
# DJLocalization
[![Version](https://img.shields.io/cocoapods/v/DJLocalization.svg?style=flat)](http://cocoadocs.org/docsets/DJLocalization)
[![License](https://img.shields.io/cocoapods/l/DJLocalization.svg?style=flat)](http://cocoadocs.org/docsets/DJLocalization)
[![Platform](https://img.shields.io/cocoapods/p/DJLocalization.svg?style=flat)](http://cocoadocs.org/docsets/DJLocalization)
Localization system that allows language switching at runtime. Supports both code strings (NSLocalizedString) and storyboards (base internationalization).
## Demo
To try the example project, just run the following command:
pod try DJLocalization
## Requirements
Requires iOS 6 or higher.
## Installation
### From CocoaPods
DJLocalization is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
pod "DJLocalization"
### Manually
_**Important note if your project doesn't use ARC**: you must add the `-fobjc-arc` compiler flag to all 'DJLocalization' files in Target Settings > Build Phases > Compile Sources._
* Drag the `DJLocalization/DJlocalization` folder into your project.
## Usage
### Importing headers
Then simply add the following import to your prefix header, or any file where you use the NSLocalizedString macro (or variants).
#import <DJLocalization/DJLocalization.h>
#####*Important:*
You must import the header wherever you use the NSLocalizedString macro, as this header replaces the default NSLocalizedString macro with a custom one. If you don't, runtime switching of languages won't work.
### Switching languages
At runtime, you can switch the language at any time by setting the language property:
[DJLocalizationSystem shared].language = @"en";
Be careful to only use language codes (en, nl, fr, etc...) for localizations that exist in your project.
#####*Important:*
Views are translated on load. This means that if you switch the language at some point, only subsequently loaded views will have the correct language strings. Ideally, when you switch languages, you should pop your navigation controller to it's root view controller.
## Credits
DJLocalization is brought to you by [David Jennes](https://twitter.com/davidjennes). The code is inspired by [Rolandas Razma's](https://twitter.com/rolandas_razma) work with [RRBaseInternationalization](https://github.com/RolandasRazma/RRBaseInternationalization) (especially the way to load strings for storyboard/xib views).
Props to Rolandas Razma
## License
DJLocalization is available under the MIT license. See the LICENSE file for more info.