LocalizationSystem.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // LocalizationSystem.m
  3. // Battle of Puppets
  4. //
  5. // Created by Juan Albero Sanchis on 27/02/10.
  6. // Copyright Aggressive Mediocrity 2010. All rights reserved.
  7. //
  8. #import "LocalizationSystem.h"
  9. #import "NSBundle+Language.h""
  10. @implementation LocalizationSystem
  11. //Singleton instance
  12. static LocalizationSystem *_sharedLocalSystem = nil;
  13. //Current application bungle to get the languages.
  14. static NSBundle *bundle = nil;
  15. +(LocalizationSystem *)sharedLocalSystem {
  16. static dispatch_once_t once;
  17. dispatch_once(&once, ^ {
  18. _sharedLocalSystem = [[super allocWithZone:nil] init];
  19. // [_sharedLocalSystem setLanguage:[_sharedLocalSystem getLanguage]];
  20. });
  21. return _sharedLocalSystem;
  22. }
  23. - (id)init {
  24. if (_sharedLocalSystem) {
  25. return _sharedLocalSystem;
  26. }
  27. if (self = [super init]) {
  28. //
  29. }
  30. return self;
  31. }
  32. - (id)copy {
  33. return [[self class] sharedLocalSystem];
  34. }
  35. + (id)allocWithZone:(NSZone *)zone {
  36. return [self sharedLocalSystem];
  37. }
  38. +(id)alloc
  39. {
  40. @synchronized([LocalizationSystem class])
  41. {
  42. NSAssert(_sharedLocalSystem == nil, @"Attempted to allocate a second instance of a singleton.");
  43. _sharedLocalSystem = [super alloc];
  44. return _sharedLocalSystem;
  45. }
  46. // to avoid compiler warning
  47. return nil;
  48. }
  49. // Gets the current localized string as in NSLocalizedString.
  50. //
  51. // example calls:
  52. // AMLocalizedString(@"Text to localize",@"Alternative text, in case hte other is not find");
  53. - (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment
  54. {
  55. if (!bundle) {
  56. bundle = nil;
  57. }
  58. return [bundle localizedStringForKey:key value:comment table:nil];
  59. }
  60. // Sets the desired language of the ones you have.
  61. // example calls:
  62. // LocalizationSetLanguage(@"Italian");
  63. // LocalizationSetLanguage(@"German");
  64. // LocalizationSetLanguage(@"Spanish");
  65. //
  66. // If this function is not called it will use the default OS language.
  67. // If the language does not exists y returns the default OS language.
  68. - (void) setLanguage:(NSString*) l{
  69. #ifdef DEBUG
  70. NSLog(@"preferredLang: %@", l);
  71. #endif
  72. // 设置语言
  73. [NSBundle setLanguage:l];
  74. // [[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:@"AppleLanguages"];
  75. // [[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:@"AppleLanguages"];
  76. // [[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:KAppLanguageFirst];
  77. [[NSUserDefaults standardUserDefaults] setObject:l forKey:KAppLanguage];
  78. [[NSUserDefaults standardUserDefaults] synchronize];
  79. // NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
  80. //
  81. // if ([l isEqualToString:@"en"]) {
  82. // path = [[ NSBundle mainBundle ] pathForResource:@"Base" ofType:@"lproj" ];
  83. // }
  84. // if (path == nil) {
  85. // //in case the language does not exists
  86. // [self resetLocalization];
  87. // }
  88. // else {
  89. // bundle = [NSBundle bundleWithPath:path];
  90. // //
  91. // [[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:@"AppleLanguages"];
  92. // [[NSUserDefaults standardUserDefaults] setObject:@[l] forKey:KAppLanguageFirst];
  93. // //
  94. // [[NSUserDefaults standardUserDefaults] synchronize];
  95. // }
  96. }
  97. // Just gets the current setted up language.
  98. // returns "es","fr",...
  99. //
  100. // example call:
  101. // NSString * currentL = LocalizationGetLanguage;
  102. //- (NSString*) getLanguage{
  103. //
  104. // NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  105. //
  106. // NSString *preferredLang = [languages objectAtIndex:0];
  107. //
  108. // return preferredLang;
  109. //}
  110. // Resets the localization system, so it uses the OS default language.
  111. //
  112. // example call:
  113. // LocalizationReset;
  114. //- (void) resetLocalization
  115. //{
  116. // [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];
  117. // //
  118. // [[NSUserDefaults standardUserDefaults] synchronize];
  119. // //
  120. // NSString *path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj" ];
  121. // bundle = [NSBundle bundleWithPath:path];
  122. //}
  123. +(void)checkXibString:(NSString *)string{
  124. if (string.length < 1 || [string isEqualToString:@"(null)"]) {
  125. return;
  126. }
  127. #ifdef DEBUG
  128. //获取沙盒路径
  129. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  130. //获取文件路径
  131. NSString *fullName = [NSString stringWithFormat:@"%@.txt", @"chinese"];
  132. NSString *theFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fullName];
  133. //创建文件管理器
  134. NSFileManager *fileManager = [NSFileManager defaultManager];
  135. NSString *content = [NSString stringWithFormat:@"\"%@\" = \"%@\";\n",string,string];
  136. //如果文件不存在 创建文件
  137. if(![fileManager fileExistsAtPath:theFilePath]){
  138. [@"chinese.txt" writeToFile:theFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  139. }else{
  140. NSString *txtContent = [NSString stringWithContentsOfFile:theFilePath encoding:NSUTF8StringEncoding error:nil];
  141. // NSMutableArray *arr = [NSMutableArray array];
  142. NSArray *array = [txtContent componentsSeparatedByString:@";"];
  143. NSArray *newarr = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
  144. // NSLog(@"%@",newarr);
  145. // NSLog(@"%@",[newarr componentsJoinedByString:@";"]);
  146. }
  147. NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:theFilePath];
  148. [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
  149. NSData* stringData = [content dataUsingEncoding:NSUTF8StringEncoding];
  150. [fileHandle writeData:stringData]; //追加写入数据
  151. [fileHandle closeFile];
  152. #endif
  153. }
  154. @end