NSLog and NSLocalized String

If you have a localizable string format which contains 2 or more variables, then you should consider using positional specifiers so the translators can change the word order. 

Here is an example in our code today:

NSString *chatName = [NSString stringWithFormat:NSLocalizedString(@"%@'s Group Chat %d", @"Group chat name format"), mainAccountUser.uiDisplayName, self.groupChatIndex];

Instead, consider this:

NSString *chatName = [NSString stringWithFormat:NSLocalizedString(@"%1$@'s Group Chat %2$d", @"Group chat name format; the first variable is the user's display name; the second variable is the chat index"), mainAccountUser.uiDisplayName, self.groupChatIndex];

In other languages, the word order may be different.  If the translator changes the order of the variables, the existing code will crash.  The modified version works correctly, and the comment provides the translators with some helpful context. (Remember, they only see the two strings we pass to NSLocalizedString; they do not see the code and variable names.)

 

By the way,  you can get more details here:


%@     对象
%d, %i    整数
%u       无符整形
%f      浮点/双字
%x, %X    二进制整数
%o    八进制整数
%zu    size_t
%p    指针
%e    浮点/双字 (科学计算)
%g   浮点/双字
%s    C 字符串
%.*s    Pascal字符串
%c     字符
%C    unichar
%lld    64位长整数(long long)
%llu    无符64位长整数
%Lf     64位双字


http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html%23//apple_ref/doc/uid/TP40004265

http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值