Explorar el Código

添加配置类, 用于修改默认配置, 暂时只加了语言选项 (#1232)

默认不设置, 走原有功能.
如果设置后, 走设置后的语言选项
Frank hace 6 años
padre
commit
38875f5d55

+ 25 - 0
MJRefresh/MJRefreshConfig.h

@@ -0,0 +1,25 @@
+//
+//  MJRefreshConfig.h
+//
+//  Created by Frank on 2018/11/27.
+//  Copyright © 2018 小码哥. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface MJRefreshConfig : NSObject
+
+/** 默认使用的语言版本, 默认为 nil. 将随系统的语言自动改变 */
+@property (copy, nonatomic, nullable) NSString *languageCode;
+
+/** @return Singleton Config instance */
++ (instancetype)defaultConfig;
+
+- (instancetype)init NS_UNAVAILABLE;
++ (instancetype)new NS_UNAVAILABLE;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 24 - 0
MJRefresh/MJRefreshConfig.m

@@ -0,0 +1,24 @@
+//
+//  MJRefreshConfig.m
+//
+//  Created by Frank on 2018/11/27.
+//  Copyright © 2018 小码哥. All rights reserved.
+//
+
+#import "MJRefreshConfig.h"
+
+@implementation MJRefreshConfig
+
+static MJRefreshConfig *mj_RefreshConfig = nil;
+
++ (instancetype)defaultConfig {
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        mj_RefreshConfig = [[self alloc] init];
+    });
+    return mj_RefreshConfig;
+}
+
+
+
+@end

+ 8 - 2
MJRefresh/NSBundle+MJRefresh.m

@@ -8,6 +8,7 @@
 
 #import "NSBundle+MJRefresh.h"
 #import "MJRefreshComponent.h"
+#import "MJRefreshConfig.h"
 
 @implementation NSBundle (MJRefresh)
 + (instancetype)mj_refreshBundle
@@ -38,8 +39,13 @@ + (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value
 {
     static NSBundle *bundle = nil;
     if (bundle == nil) {
-        // (iOS获取的语言字符串比较不稳定)目前框架只处理en、zh-Hans、zh-Hant三种情况,其他按照系统默认处理
-        NSString *language = [NSLocale preferredLanguages].firstObject;
+        NSString *language = MJRefreshConfig.defaultConfig.languageCode;
+        // 如果配置中没有配置语言
+        if (!language) {
+            // (iOS获取的语言字符串比较不稳定)目前框架只处理en、zh-Hans、zh-Hant三种情况,其他按照系统默认处理
+            language = [NSLocale preferredLanguages].firstObject;
+        }
+        
         if ([language hasPrefix:@"en"]) {
             language = @"en";
         } else if ([language hasPrefix:@"zh"]) {

+ 6 - 0
MJRefreshExample.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		01A2CE0C21ACE01500BEE365 /* MJRefreshConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 01A2CE0B21ACE01500BEE365 /* MJRefreshConfig.m */; };
 		2D4698861D0EE6A400CB8025 /* NSBundle+MJRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4698851D0EE6A400CB8025 /* NSBundle+MJRefresh.m */; };
 		2D9BEB091BB15F4A00AED473 /* UIViewController+Example.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D9BEAE01BB15F4A00AED473 /* UIViewController+Example.m */; };
 		2D9BEB0A1BB15F4A00AED473 /* MJChiBaoZiFooter.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D9BEAE31BB15F4A00AED473 /* MJChiBaoZiFooter.m */; };
@@ -118,6 +119,8 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		01A2CE0A21ACE01500BEE365 /* MJRefreshConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJRefreshConfig.h; sourceTree = "<group>"; };
+		01A2CE0B21ACE01500BEE365 /* MJRefreshConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJRefreshConfig.m; sourceTree = "<group>"; };
 		2D4698841D0EE6A400CB8025 /* NSBundle+MJRefresh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle+MJRefresh.h"; sourceTree = "<group>"; };
 		2D4698851D0EE6A400CB8025 /* NSBundle+MJRefresh.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSBundle+MJRefresh.m"; sourceTree = "<group>"; };
 		2D9BEADF1BB15F4A00AED473 /* UIViewController+Example.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+Example.h"; sourceTree = "<group>"; };
@@ -405,6 +408,8 @@
 				2DB2EA161BECBE6700D58F6A /* UIView+MJExtension.m */,
 				2D4698841D0EE6A400CB8025 /* NSBundle+MJRefresh.h */,
 				2D4698851D0EE6A400CB8025 /* NSBundle+MJRefresh.m */,
+				01A2CE0A21ACE01500BEE365 /* MJRefreshConfig.h */,
+				01A2CE0B21ACE01500BEE365 /* MJRefreshConfig.m */,
 			);
 			path = MJRefresh;
 			sourceTree = SOURCE_ROOT;
@@ -710,6 +715,7 @@
 				2D9BEB191BB15F4A00AED473 /* MJTestViewController.m in Sources */,
 				2DB2EA211BECBE6700D58F6A /* MJRefreshBackStateFooter.m in Sources */,
 				2DB2EA281BECBE6700D58F6A /* UIScrollView+MJRefresh.m in Sources */,
+				01A2CE0C21ACE01500BEE365 /* MJRefreshConfig.m in Sources */,
 				2D9BEB151BB15F4A00AED473 /* MJTempViewController.m in Sources */,
 				2D9BEB171BB15F4A00AED473 /* MJCollectionViewController.m in Sources */,
 				2D9BEB0B1BB15F4A00AED473 /* MJChiBaoZiFooter2.m in Sources */,