[iOS]给上拉下拉刷新PullToRefresh做本地化

相信很多小朋友都做过下拉刷新,上拉加载更多,或者是点一下加载更多吧,我在几个项目中都用到了上拉下拉刷新,用的是[EGOTableViewPullRefresh],是我觉得同样给力的EGOCache同一个作者的作品。

这份代码有两年没更新了,惊奇的发现 Commit 记录在两天前有点动静,不过它确实挺好用,只是没有支持 Block:

EGOTableViewPullRefresh Commits

使用方法很简单,这里就不说了,今天的需求是要把提示信息改为中文,于是我翻了翻代码,写的很不错,所有提示文本都用的 NSLocalizedStringFromTable 来处理,如:

_statusLabel.text = NSLocalizedStringFromTable(@"Pull up to load more...", @"PullTableViewLan",@"Pull down to load more status");
stringWithFormat:NSLocalizedStringFromTable(@"Updated %ld minute ago",@"PullTableViewLan",@"Last uppdate in minutes singular"),(long)timeToDisplay];

这就很方便让我们把这些信息改为我们想要的文本,首先得说说 NSLocalizedStringFromTable,其实在我们做本地化中用的最多的应该是 NSLocalizedString 了,我们看看它的定义:

#define NSLocalizedString(key, comment) \ [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

这是一个宏定义,其实是调用了 NSBundle 的 localizedStringForKey 方法,也就是在当前 Bundle 中查找资源文件名 Localizable.strings 中 key 所指向的字符串。那么 NSLocalizedStringFromTable 呢?我们再看看它的定义:

#define NSLocalizedStringFromTable(key, tbl, comment) \
	    [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]

可以看到这里的 table 参数已经不是 nil 了,而是你传进去的一个 strings 文件的文件名,EGOTableViewPullRefresh 所有的本地化操作都是传的 PullTableViewLan,也就是说它会在你的工程里找 PullTableViewLan.strings 文件来匹配本地化字符串。

那我们的做法就是在 EGOTableViewPullRefresh 的目录中新建 PullTableViewLan.strings 文件:

PullTableViewLan.strings

然后编辑文件,输入 EGOTableViewPullRefresh 定义的一些本地话字符串,我的是这样:

/*
  Localizable.strings

  Created by isaced on 13-8-6.
  Copyright (c) 2013年 isaced. All rights reserved.
*/

"Loading..." = "加载更多...";

"Pull down to refresh..." = "下拉刷新...";
"Release to refresh..." = "松开刷新...";

"Pull up to load more..." = "上拉加载更多...";
"Release to load more..." = "松开加载更多...";

"Updated %ld minute ago" = "%ld 分钟前更新";
"Updated %ld minutes ago" = "%ld 分钟前更新";

然后对比一下效果,怎么样,是不是不用修改一句代码就可以把原来的英文提示改为中文。

Pull Refresh

晕,整篇文章打字打“本地化”的时候总自动拼出“本地话”,郁闷!还推荐另一份代码:PullToRefresh

comments powered by Disqus