genstrings不适用于NSLocalizedString的macros

我想缩短“NSLocalizedString”为“_”,所以我使用macros_(x) NSLocalizedString(@x, @__FILE__)

但现在,当我想要生成与find . -name \*.m | xargs genstrings本地化的stringfind . -name \*.m | xargs genstrings find . -name \*.m | xargs genstrings find . -name \*.m | xargs genstrings它什么都不产生。

任何帮助?

你可以通过使用'-s'参数来告诉genstrings寻找不同的函数:

 genstring -s MyFunctionName .... 

但是 ,MyFunctionName必须遵循与内置的NSLocalizeStringmacros相同的命名和参数约定。

在你的情况下,你不能只指定string键,你还必须指定文档string。 事实上,如果没有string和文档,你永远不应该生成一个string文件。 有许多语言的实际短语或单词将取决于上下文。 德语是一个很好的例子,汽车是“达斯汽车”,不止一个是“死亡汽车”。 还有更多的例子,包括性别,数量,时间,问题与声明的变化,以及是与否。 文档string可帮助您的翻译员找出要使用的翻译。

另外,最好的做法是使用与母语不同的密钥。 这就是说使用NSLocalizedStringWithDefaultValue(键,表,捆绑,瓦尔,评论)。 您可以为表指定nil,为bundle参数指定[NSBundle mainBundle]。

你可以用简写包起来,但是你仍然需要遵循StringWithDefaultValue的名字和参数来起作用。

我强烈build议您查看关于本地化技巧和技巧的WWDC 2012会议。

莫里斯

您可以使用genstrings-s选项。 从手册页 :

例程
replaceNSLocalizedString的例程。 例如,-s MyLocalString将捕获对MyLocalString和MyLocalStringFromTable的调用。

所以我想你可以试试:

生日快乐-s _

我有同样的问题,当我的NSLocalizedStringmacros1采取1参数而不是2像genstrings期望,所以我写了我的Python脚本,做这项工作。

脚本的第一个参数是macros名称,第二个参数是项目的path。

 import fnmatch import os from xml.dom import minidom function = sys.argv[1] rootdir = sys.argv[2] # Generate strings from .m files files = [] for root, dirnames, filenames in os.walk(rootdir): for filename in fnmatch.filter(filenames, '*.m'): files.append(os.path.join(root, filename)) strings = [] for file in files: lineNumber = 0 for line in open(file): lineNumber += 1 index = line.find(function) if (index != -1): callStr = line[index:] index = callStr.find('@') if (index == -1): print 'call with a variable/macro. file: ' + file + ' line: %d' % lineNumber else: callStr = callStr[index+1:] index = callStr.find('")') callStr = callStr[:index+1] if callStr not in strings: strings.append(callStr) # Write strings to file f = open('Localizable.strings', 'w+') for string in strings: f.write(string + ' = ' + string + ';\n\n') f.close() 

我已经改进了阿贝尔的脚本,以便在一行中包含多个macros观调用的情况:

 import fnmatch import os from xml.dom import minidom import sys function = sys.argv[1] rootdir = sys.argv[2] # Generate strings from .m files files = [] for root, dirnames, filenames in os.walk(rootdir): for filename in fnmatch.filter(filenames, '*.m'): files.append(os.path.join(root, filename)) strings = [] for file in files: lineNumber = 0 for line in open(file): lineNumber += 1 index = line.find(function) startIndex = 0 while (index != -1): startIndex = index+1 callStr = line[index:] index = callStr.find('@') if (index == -1): print 'call with a variable/macro. file: ' + file + ' line: %d' % lineNumber else: callStr = callStr[index+1:] index = callStr.find('")') callStr = callStr[:index+1] if callStr not in strings: strings.append(callStr) index = line.find(function, startIndex) # Write strings to file f = open('Localizable.strings', 'w+') for string in strings: f.write(string + ' = ' + string + ';\n\n') f.close()