genstrings 不适用于 NSLocalizedString 的宏

发布于 2024-12-27 22:56:53 字数 191 浏览 2 评论 0原文

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

但现在,当我想生成用于本地化的字符串时 <代码>查找 . -名称 \*.m | xargs genstrings 它什么也不产生。

有什么帮助吗?

I would like to shorten "NSLocalizedString" to "_" so I'm using macro
_(x) NSLocalizedString(@x, @__FILE__)
.

But now, when I want to generate strings for localization with
find . -name \*.m | xargs genstrings
it generates nothing.

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尘曦 2025-01-03 22:56:53

您可以使用“-s”参数告诉 genstrings 查找不同的函数:

genstring -s MyFunctionName ....

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

在您的情况下,您不能只指定字符串键,还必须指定文档字符串。事实上,您应该永远生成没有字符串和文档的字符串文件。在许多语言中,实际的短语或单词取决于上下文。德语就是一个很好的例子,其中一辆车是“das auto”,而不止一辆是“die autos”。还有更多的例子,包括性别、数字、时间、问题与陈述、是与否的变化。文档字符串可以帮助您的翻译人员确定要使用的翻译。

此外,最佳做法是使用与母语单词不同的键。这就是说使用 NSLocalizedStringWithDefaultValue(key, table, bundle, val, comment)。
您可以为表指定 nil,为包参数指定 [NSBundle mainBundle]。

您可以将其包装为简写,但您仍然必须遵循 StringWithDefaultValue 名称和 genstring 的参数才能工作。

我强烈建议您查看 WWDC 2012 的本地化提示和技巧会议。

莫里斯

You can tell genstrings to look for a different function by using the '-s' argument:

genstring -s MyFunctionName ....

However, MyFunctionName must follow the same naming and argument conventions as one of the built in NSLocalizeString macros.

In your case, you can not just specify the string key, you must also specify the documentation string. In fact, you should never generate a strings file without both the string and documentation. There are many languages where the actual phrase or word will depend on context. German is a great example where a car is "das auto" and more than one is "die autos". There are many more examples that include changes for gender, number, time, question versus statement, and yes versus no. The documentation string helps your translator figure out what translation to use.

In addition, the best practice is to use a key that is different from the native language word. That says use NSLocalizedStringWithDefaultValue(key, table, bundle, val, comment).
You can specify nil for the table and [NSBundle mainBundle] for the bundle argument.

You can wrap this in a shorthand, but you still have to follow the StringWithDefaultValue name and the arguments for genstrings to work.

I strongly recommend you look at the WWDC 2012 session on Localization Tips and Tricks.

Maurice

歌枕肩 2025-01-03 22:56:53

您可以使用 genstrings-s 选项。来自 手册页

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

所以我想你可以尝试:

genstrings -s _

You can use the -s option of genstrings. From the man page :

-s routine
Substitutes routine for NSLocalizedString. For example, -s MyLocalString will catch calls to MyLocalString and MyLocalStringFromTable.

So I think you could try :

genstrings -s _

花心好男孩 2025-01-03 22:56:53

当我的 NSLocalizedString 宏采用 1 个参数而不是 genstrings 期望的 2 个参数时,我遇到了同样的问题,所以我编写了完成这项工作的 python 脚本。

脚本的第一个参数是宏名称,第二个参数是项目的路径。

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()

I had the same problem when my NSLocalizedString macro was taking 1 argument instead of 2 like genstrings expects, so i wrote i python script that does the job.

the first argument for the script is the macro name and the second is the path to your project.

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()
み青杉依旧 2025-01-03 22:56:53

我改进了 Or Arbel 的脚本,以包含单行上有多个宏调用的情况:

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()

I have improved Or Arbel's script to include the cases where there's multiple macro-calls on a single line:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文