Skip to content

Commit

Permalink
Merge pull request CoderMJLee#426 from ttgb/master
Browse files Browse the repository at this point in the history
Add localization
  • Loading branch information
CoderMJLee committed May 20, 2016
2 parents 79d7caf + c5b012b commit a0c76cc
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 23 deletions.
3 changes: 3 additions & 0 deletions MJRefresh/Base/MJRefreshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ typedef void (^MJRefreshComponentRefreshingBlock)();
@property (copy, nonatomic) MJRefreshComponentRefreshingBlock refreshingBlock;
/** 设置回调对象和回调方法 */
- (void)setRefreshingTarget:(id)target refreshingAction:(SEL)action;

- (NSString *)localizedStringForKey:(NSString *)key;

/** 回调对象 */
@property (weak, nonatomic) id refreshingTarget;
/** 回调方法 */
Expand Down
28 changes: 28 additions & 0 deletions MJRefresh/Base/MJRefreshComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ - (void)setRefreshingTarget:(id)target refreshingAction:(SEL)action
self.refreshingAction = action;
}

- (NSString *)localizedStringForKey:(NSString *)key{
return [self localizedStringForKey:key withDefault:nil];
}

- (NSString *)localizedStringForKey:(NSString *)key withDefault:(NSString *)defaultString
{
static NSBundle *bundle = nil;
if (bundle == nil)
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MJRefresh" ofType:@"bundle"];

bundle = [NSBundle bundleWithPath:bundlePath];
NSString *language = [[NSLocale preferredLanguages] count]? [NSLocale preferredLanguages][0]: @"en";
if (![[bundle localizations] containsObject:language])
{
language = [language componentsSeparatedByString:@"-"][0];
}
if ([[bundle localizations] containsObject:language])
{
bundlePath = [bundle pathForResource:language ofType:@"lproj"];
}

bundle = [NSBundle bundleWithPath:bundlePath] ?: [NSBundle mainBundle];
}
defaultString = [bundle localizedStringForKey:key value:defaultString table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:defaultString table:nil];
}

#pragma mark 进入刷新状态
- (void)beginRefreshing
{
Expand Down
6 changes: 3 additions & 3 deletions MJRefresh/Custom/Footer/Auto/MJRefreshAutoStateFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:MJRefreshAutoFooterIdleText forState:MJRefreshStateIdle];
[self setTitle:MJRefreshAutoFooterRefreshingText forState:MJRefreshStateRefreshing];
[self setTitle:MJRefreshAutoFooterNoMoreDataText forState:MJRefreshStateNoMoreData];
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[self localizedStringForKey:MJRefreshAutoFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];

// 监听label
self.stateLabel.userInteractionEnabled = YES;
Expand Down
8 changes: 4 additions & 4 deletions MJRefresh/Custom/Footer/Back/MJRefreshBackStateFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:MJRefreshBackFooterIdleText forState:MJRefreshStateIdle];
[self setTitle:MJRefreshBackFooterPullingText forState:MJRefreshStatePulling];
[self setTitle:MJRefreshBackFooterRefreshingText forState:MJRefreshStateRefreshing];
[self setTitle:MJRefreshBackFooterNoMoreDataText forState:MJRefreshStateNoMoreData];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterPullingText] forState:MJRefreshStatePulling];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterRefreshingText] forState:MJRefreshStateRefreshing];
[self setTitle:[self localizedStringForKey:MJRefreshBackFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
}

- (void)placeSubviews
Expand Down
19 changes: 13 additions & 6 deletions MJRefresh/Custom/Header/MJRefreshStateHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ - (void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey

// 2.格式化日期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
BOOL isToday = NO;
if ([cmp1 day] == [cmp2 day]) { // 今天
formatter.dateFormat = @"今天 HH:mm";
formatter.dateFormat = @" HH:mm";
isToday = YES;
} else if ([cmp1 year] == [cmp2 year]) { // 今年
formatter.dateFormat = @"MM-dd HH:mm";
} else {
Expand All @@ -96,9 +98,14 @@ - (void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey
NSString *time = [formatter stringFromDate:lastUpdatedTime];

// 3.显示日期
self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"最后更新:%@", time];
self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@%@",
[self localizedStringForKey:MJRefreshHeaderLastTimeText],
isToday ? [self localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
time];
} else {
self.lastUpdatedTimeLabel.text = @"最后更新:无记录";
self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@",
[self localizedStringForKey:MJRefreshHeaderLastTimeText],
[self localizedStringForKey:MJRefreshHeaderNoneLastDateText]];
}
}

Expand All @@ -108,9 +115,9 @@ - (void)prepare
[super prepare];

// 初始化文字
[self setTitle:MJRefreshHeaderIdleText forState:MJRefreshStateIdle];
[self setTitle:MJRefreshHeaderPullingText forState:MJRefreshStatePulling];
[self setTitle:MJRefreshHeaderRefreshingText forState:MJRefreshStateRefreshing];
[self setTitle:[self localizedStringForKey:MJRefreshHeaderIdleText] forState:MJRefreshStateIdle];
[self setTitle:[self localizedStringForKey:MJRefreshHeaderPullingText] forState:MJRefreshStatePulling];
[self setTitle:[self localizedStringForKey:MJRefreshHeaderRefreshingText] forState:MJRefreshStateRefreshing];
}

- (void)placeSubviews
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions MJRefresh/MJRefreshConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ UIKIT_EXTERN NSString *const MJRefreshBackFooterPullingText;
UIKIT_EXTERN NSString *const MJRefreshBackFooterRefreshingText;
UIKIT_EXTERN NSString *const MJRefreshBackFooterNoMoreDataText;

UIKIT_EXTERN NSString *const MJRefreshHeaderLastTimeText;
UIKIT_EXTERN NSString *const MJRefreshHeaderDateTodayText;
UIKIT_EXTERN NSString *const MJRefreshHeaderNoneLastDateText;

// 状态检查
#define MJRefreshCheckState \
MJRefreshState oldState = self.state; \
Expand Down
24 changes: 14 additions & 10 deletions MJRefresh/MJRefreshConst.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@

NSString *const MJRefreshHeaderLastUpdatedTimeKey = @"MJRefreshHeaderLastUpdatedTimeKey";

NSString *const MJRefreshHeaderIdleText = @"下拉可以刷新";
NSString *const MJRefreshHeaderPullingText = @"松开立即刷新";
NSString *const MJRefreshHeaderRefreshingText = @"正在刷新数据中...";
NSString *const MJRefreshHeaderIdleText = @"MJRefreshHeaderIdleText";
NSString *const MJRefreshHeaderPullingText = @"MJRefreshHeaderPullingText";
NSString *const MJRefreshHeaderRefreshingText = @"MJRefreshHeaderRefreshingText";

NSString *const MJRefreshAutoFooterIdleText = @"点击或上拉加载更多";
NSString *const MJRefreshAutoFooterRefreshingText = @"正在加载更多的数据...";
NSString *const MJRefreshAutoFooterNoMoreDataText = @"已经全部加载完毕";
NSString *const MJRefreshAutoFooterIdleText = @"MJRefreshAutoFooterIdleText";
NSString *const MJRefreshAutoFooterRefreshingText = @"MJRefreshAutoFooterRefreshingText";
NSString *const MJRefreshAutoFooterNoMoreDataText = @"MJRefreshAutoFooterNoMoreDataText";

NSString *const MJRefreshBackFooterIdleText = @"上拉可以加载更多";
NSString *const MJRefreshBackFooterPullingText = @"松开立即加载更多";
NSString *const MJRefreshBackFooterRefreshingText = @"正在加载更多的数据...";
NSString *const MJRefreshBackFooterNoMoreDataText = @"已经全部加载完毕";
NSString *const MJRefreshBackFooterIdleText = @"MJRefreshBackFooterIdleText";
NSString *const MJRefreshBackFooterPullingText = @"MJRefreshBackFooterPullingText";
NSString *const MJRefreshBackFooterRefreshingText = @"MJRefreshBackFooterRefreshingText";
NSString *const MJRefreshBackFooterNoMoreDataText = @"MJRefreshBackFooterNoMoreDataText";

NSString *const MJRefreshHeaderLastTimeText = @"MJRefreshHeaderLastTimeText";
NSString *const MJRefreshHeaderDateTodayText = @"MJRefreshHeaderDateTodayText";
NSString *const MJRefreshHeaderNoneLastDateText = @"MJRefreshHeaderNoneLastDateText";

0 comments on commit a0c76cc

Please sign in to comment.