NSBundle+MJRefresh.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // NSBundle+MJRefresh.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 16/6/13.
  6. // Copyright © 2016年 小码哥. All rights reserved.
  7. //
  8. #import "NSBundle+MJRefresh.h"
  9. #import "MJRefreshComponent.h"
  10. @implementation NSBundle (MJRefresh)
  11. + (instancetype)mj_refreshBundle
  12. {
  13. static NSBundle *refreshBundle = nil;
  14. if (refreshBundle == nil) {
  15. // 这里不使用mainBundle是为了适配pod 1.x和0.x
  16. refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[MJRefreshComponent class]] pathForResource:@"MJRefresh" ofType:@"bundle"]];
  17. }
  18. return refreshBundle;
  19. }
  20. + (UIImage *)mj_arrowImage
  21. {
  22. static UIImage *arrowImage = nil;
  23. if (arrowImage == nil) {
  24. arrowImage = [[UIImage imageWithContentsOfFile:[[self mj_refreshBundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  25. }
  26. return arrowImage;
  27. }
  28. + (NSString *)mj_localizedStringForKey:(NSString *)key
  29. {
  30. return [self mj_localizedStringForKey:key value:nil];
  31. }
  32. + (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value
  33. {
  34. static NSBundle *bundle = nil;
  35. if (bundle == nil) {
  36. // (iOS获取的语言字符串比较不稳定)目前框架只处理en、zh-Hans、zh-Hant三种情况,其他按照系统默认处理
  37. NSString *language = [NSLocale preferredLanguages].firstObject;
  38. if ([language hasPrefix:@"en"]) {
  39. language = @"en";
  40. } else if ([language hasPrefix:@"zh"]) {
  41. if ([language rangeOfString:@"Hans"].location != NSNotFound) {
  42. language = @"zh-Hans"; // 简体中文
  43. } else { // zh-Hant\zh-HK\zh-TW
  44. language = @"zh-Hant"; // 繁體中文
  45. }
  46. } else {
  47. language = @"en";
  48. }
  49. // 从MJRefresh.bundle中查找资源
  50. bundle = [NSBundle bundleWithPath:[[NSBundle mj_refreshBundle] pathForResource:language ofType:@"lproj"]];
  51. }
  52. value = [bundle localizedStringForKey:key value:value table:nil];
  53. return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
  54. }
  55. @end