//
// IMLocalization.m
// MyProject
//
// Created by haibara on 6/10/15.
//
//

#import "IMLocalization.h"

@interface LocalizationSystem ()
@property (nonatomic, strong) NSArray *bundles;
- (instancetype)initPrivate;
@end

@implementation IMLocalization

#pragma mark - Protected

- (instancetype)init {
	return [self initPrivate];
}

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
	for (NSBundle *bundle in self.bundles) {
		NSString *msg = [bundle localizedStringForKey:key value:value table:tableName];
		if (msg) {
			return msg;
		}
	}
	return nil;
}

#pragma mark - Public

+ (IMLocalization *)sharedInstance {
	static dispatch_once_t onceToken;
	static IMLocalization *instance;
	dispatch_once(&onceToken, ^{
		instance = [[self alloc] init];
	});
	return instance;
}

- (BOOL)hasPreferredLanguage {
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	return [userDefaults objectForKey:@"ICPreferredLanguage"];
}

- (NSString *)localizedPathForResource:(NSString *)name ofType:(NSString *)extension {
	for (NSBundle *bundle in self.bundles) {
		NSString *path = [bundle pathForResource:name ofType:extension];
		if (path) {
			return path;
		}
	}
	return nil;
}

@end
