NSBundle+KODLocalizable.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // NSBundle+KODLocalizable.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2021/5/19.
  6. // Copyright © 2021 SAGESSE. All rights reserved.
  7. //
  8. #import "NSBundle+KODLocalizable.h"
  9. #define kKODBundleName @"FrameworkTestBundle.bundle"
  10. //#define kDSADLanguageStyle @"DSADLanguageStyle"
  11. @implementation NSBundle (KODLocalizable)
  12. + (instancetype)KOD_localizableBundleWithBundleName:(NSString *)bundleName{
  13. static NSBundle *localizableBundle = nil;
  14. if (localizableBundle == nil) {
  15. if (!bundleName) {
  16. bundleName = kKODBundleName;
  17. }
  18. NSString *bundleType = nil;
  19. if (bundleName && ![bundleName hasSuffix:@"bundle"]) {
  20. bundleType = @"bundle";
  21. }
  22. NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:bundleType];
  23. localizableBundle = [NSBundle bundleWithPath:bundlePath];
  24. }
  25. return localizableBundle;
  26. }
  27. + (NSString *)KOD_localizedStringForKey:(NSString *)key{
  28. return [self KOD_localizedStringForKey:key value:nil];
  29. }
  30. + (NSString *)KOD_localizedStringForKey:(NSString *)key value:(NSString *)value{
  31. NSBundle *bundle = nil;
  32. NSString *language = [self getLanguageFromSystem];
  33. //NSString *language = [self getLanguageFromPlist];
  34. //NSString * language = [self getLanguageFromDevelopersSetup];
  35. //从fq.bundle中查找资源
  36. NSString *bundlePath = [[NSBundle KOD_localizableBundleWithBundleName:nil] pathForResource:language ofType:@"lproj"];
  37. bundle = [NSBundle bundleWithPath:bundlePath];
  38. value = [bundle localizedStringForKey:key value:value table:nil];
  39. return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
  40. }
  41. //这个设置语言是通过读取当前系统使用语言
  42. + (NSString *)getLanguageFromSystem{
  43. NSString *language = [NSLocale preferredLanguages].firstObject;
  44. if ([language hasPrefix:@"en"]) {
  45. language = @"en";
  46. } else if ([language hasPrefix:@"zh"]) {
  47. if ([language rangeOfString:@"Hans"].location != NSNotFound) {
  48. language = @"zh-Hans"; // 简体中文
  49. } else {
  50. language = @"zh-Hant"; // 繁體中文
  51. }
  52. } else if ([language hasPrefix:@"ja"]) {
  53. language = @"ja"; //日文
  54. }else if ([language hasPrefix:@"ko"]) {
  55. language = @"ko"; //韩文
  56. }
  57. return language;
  58. }
  59. //这个是设置语言通过Plist文件来读取
  60. //+ (NSString *)getLanguageFromPlist{
  61. // NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SDKInternationalizationDemoPlist.plist" ofType:nil];
  62. // if (!bundlePath) {
  63. // return nil;
  64. // }
  65. // NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
  66. // if (dict) {
  67. // NSInteger languageNum = [[dict valueForKey:@"language"] integerValue];
  68. // switch (languageNum) {
  69. // case 1:
  70. // return @"en"; //语言为英语:en
  71. // break;
  72. // case 2:
  73. // return @"zh-Hans";//语言为简中:zh-Hans
  74. // break;
  75. // case 3:
  76. // return @"zh-Hant";//语言为繁中:zh-Hanz
  77. // break;
  78. // default:
  79. // return @"en";
  80. // break;
  81. // }
  82. // }
  83. // return @"en";
  84. //}
  85. //
  86. ////这个是设置语言通过开发者手动调用,从NSUserDefaults里面去读kDSADLanguageStyle这个字段是哪一种语言
  87. //+ (NSString *)getLanguageFromDevelopersSetup{
  88. // NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  89. // NSInteger languageStyle = [[userDefaults valueForKey:kDSADLanguageStyle] integerValue];
  90. // if (!languageStyle) {
  91. // return nil;
  92. // }
  93. // switch (languageStyle) {
  94. // case 1:
  95. // return @"en";
  96. // break;
  97. // case 2:
  98. // return @"zh-Hans";
  99. // break;
  100. // case 3:
  101. // return @"zh-Hant";
  102. // break;
  103. // default:
  104. // return @"en";
  105. // break;
  106. // }
  107. //}
  108. @end