你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

three20代码阅读之TTGlobalCoreLocal

2022/5/17 7:47:26

///

//NSLocal可获取国家地区

NSLocale* TTCurrentLocale() {

//获取用户选择语言

  NSArray* languages = [NSLocale preferredLanguages];
  if (languages.count > 0) {
    NSString* currentLanguage = [languages objectAtIndex:0];
    return [[[NSLocale alloc] initWithLocaleIdentifier:currentLanguage] autorelease];

  } else {

//返回系统默认

    return [NSLocale currentLocale];
  }
}


///
NSString* TTLocalizedString(NSString* key, NSString* comment) {
  static NSBundle* bundle = nil;
  if (nil == bundle) {
    NSString* path = [[[NSBundle mainBundle] resourcePath]
          stringByAppendingPathComponent:@"Three20.bundle"];
    bundle = [[NSBundle bundleWithPath:path] retain];
  }

  return [bundle localizedStringForKey:key value:key table:nil];
}


///
NSString* TTDescriptionForError(NSError* error) {
  TTDINFO(@"ERROR %@", error);

  if ([error.domain isEqualToString:NSURLErrorDomain]) {
    // Note: If new error codes are added here, be sure to document them in the header.
    if (error.code == NSURLErrorTimedOut) {
      return TTLocalizedString(@"Connection Timed Out", @"");

    } else if (error.code == NSURLErrorNotConnectedToInternet) {
      return TTLocalizedString(@"No Internet Connection", @"");

    } else {
      return TTLocalizedString(@"Connection Error", @"");
    }
  }
  return TTLocalizedString(@"Error", @"");
}


///

//格式化数据

NSString* TTFormatInteger(NSInteger num) {
  NSNumber* number = [NSNumber numberWithInt:num];
  NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
  [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  NSString* formatted = [formatter stringFromNumber:number];
  [formatter release];
  return formatted;
}