字符串本地化的使用
如果有一个按钮,上面的文字在中文系统上显示为“点击”,在英文系统上显示为“Click”,我们可以这么做:
<code class="hljs mel has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">button</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">text</span> = NSLocalizedString(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>click<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", nil);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
这里的click其实相当于字典的key,然后我们维护两套字典,英文字典中有这么一行:
“click” = “Click”;
在中文字典中则是:
“click” = “点击”;
运行时,系统先判断当前的语言,然后根据这个key,到对应语言的字典中找到它的“翻译”。
先后顺序问题
按照之前我了解的思路,只要再到工程文件中创建两套字典就可以了。不过有一问题是,NSLocalizedString宏的第二个参数好像没有意义。其实这个参数是为了genstrings命令准备的。
genstrings作为“generate Localizable.strings”的简称,顾名思义,就是查找文件中的宏,然后自动生成Localizable.strings文件。下面先来看看这个宏的使用方法。
首先进入项目文件目录
<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> Path/To/Project/Folder</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
建立中文和英文字典所在的文件夹
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ mkdir <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">zh</span>-Hans<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lproj</span>
$ mkdir en<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lproj</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
然后调用genstrings命令
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ genstrings -o en<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lproj</span> *<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.m</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
这个命令表示:
扫描当前文件夹下所有.m文件的宏,生成Localizable.strings文件并放到en.lproj文件夹下。
关于genstrings命令需要解释几点:
1. genstrings命令只扫描这个文件夹下的文件,并不是递归的。也就是不能扫描subfolder中的文件。如果想要扫描这个文件夹包括其子文件夹内的所有文件,调用如下命令:$ find . -name \*.m | xargs genstrings -o en.lproj
2. 语法为genstrings [-a] [-q] [-o <outputDir>] sourcefile
2. genstrings命令支持8种宏,分别是CFCopyLocalizedString、CFCopyLocalizedStringFromTable、CFCopyLocalizedStringFromTableInBundle、CFCopyLocalizedStringWithDefaultValue、NSLocalizedString、NSLocalizedStringFromTable、NSLocalizedStringFromTableInBundle、NSLocalizedStringWithDefaultValue,仔细观察发现它们是两两相对的四组,只要记NSLocalizedString系列即可,CFCopy是它们的C语言版本。
之前一直没到的宏的第二个参数终于发挥作用了,genstrings命令会读取这个参数,把它作为这一条记录的注释。
比如
<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">NSLocalizedString(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
在genstrings命令执行后会生成:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* My comment */</span>
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
value的值默认是和key相同。
NSLocalizedString系列简介
实习的时候,所有记录都放在同一个Localizable.strings文件中,随着项目的变大,维护变得非常困难。往往需要动用搜索才能定位。这时候就该看看NSLocalizedString宏的三个升级版本了。
NSLocalizedStringFromTable
宏定义如下:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> NSLocalizedStringFromTable(key, tbl, comment) \</span>
[[NSBundle mainBundle] localizedStringForKey:(key) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">@""</span> table:(tbl)]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
这里的table和UITable毫无关系,更像是一个category。如果我们的代码写的是
<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">NSLocalizedStringFromTable(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"TableName"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
会在指定的输出目录下生成TableName.strings文件,然后添加记录和注释。也就是说:
<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">NSLocalizedString(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span>);
<span class="hljs-function" style="box-sizing: border-box;">NSLocalizedStringFromTable(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Localizable"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span>
<span class="hljs-function" style="box-sizing: border-box;">NSLocalizedStringFromTable(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, nil, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
这三句的作用是一样的。第三种是苹果对NSLocalizedString宏的定义。
有了NSLocalizedStringFromTable宏,我们就可以对本地化字符串自由的归类了。
NSLocalizedStringFromTableInBundle
由于有了之前的经验,就不再赘述这个宏的作用了,它允许我们指定.strings文件放在哪个bundle里面。也就是说
<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-function" style="box-sizing: border-box;">NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,<span class="hljs-function" style="box-sizing: border-box;">NSLocalizedStringFromTableInBundle(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Localizable"</span>, [NSBundle mainBundle], @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span>)</span>;
<span class="hljs-function" style="box-sizing: border-box;">NSLocalizedStringFromTable(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Localizable"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"My comment"</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
这两个宏的效果是一样的。
NSLocalizedStringWithDefaultValue
之前说过,生成的键值对中,默认键和值是一样的,这个宏允许我们自定义默认值。
NSLocalizedString总结:
NSLocalizedString系列的四个宏,其实最终都是调用了[bundle localizedStringForKey:(key) value:(val) table:(tbl)]。根据我们的自定义程度不同可以选择不同的宏。
总结
与先建立文件然后往里面添加键值对相比,更快速的方式是调用genstrings命令。这个命令允许我们先写代码(NSLocalizedString系列宏),然后根据代码生成本地化文件。这样做不仅减少工程量,更可以充分利用NSLocalizedString系列宏的语法特性。