自定义 NSLocalizedString?
是否可以将 NSLocalizedString 基础结构(基于 localized.strings)与自定义定义的“本地化”一起使用?
问题是,有些语言对男性和女性有不同的措辞。我想在首次启动时询问用户的性别,然后使用适当的短语。当然,两者都基于相同的语言。我可以用自己的代码来完成,但如果可能的话,我宁愿用简单的方法来完成。
Is it possible to use the NSLocalizedString infrastructure (based on localizable.strings) with a custom-defined "localization"?
The thing is, there are some languages that have different wordings for males and females. I want to ask the user's gender on first launch and then use the appropriate phrases. Of course, both are based on the same language. I can do it with my own code but I'd rather do it the easy way if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Xcode Project Navigator 中,
示例: NSLocalizedString CFLocalizedString NSBundleLocalizedString
此后,Xcode 中的“导出本地化”选项将生成一个 XLIFF 文件,其中包含自定义函数中提到的可翻译字符串。
In the Xcode Project Navigator,
Example: NSLocalizedString CFLocalizedString NSBundleLocalizedString
After this, option "export for localization" from Xcode will produce an XLIFF file that contains translatable strings mentioned in your custom function.
为此有一个特殊的机制。
来自Apple参考:使用 genstring 搜索自定义函数
要实现您自己的行为,请使用
NSBundle
方法localizedStringForKey:value:table:
There is a special mechanism for this.
From Apple reference: Searching for Custom Functions With genstrings
To implement your own behaviour use
NSBundle
methodlocalizedStringForKey:value:table:
这是我的自定义实现,它使用 NSLocalizedString 并使用注释作为默认值:
-->这里我转换为 2 种语言(英语和意大利语)
-->将此方法放在appdelegate类中
--> 来调用此方法。
通过转换回所选语言
Here is my Custom implementation that use NSLocalizedString that use comment as default value:
--> Here I convert in 2 Language (English and Italian)
--> Put this method in appdelegate class
--> call this method by
that convert Back in selected language.
@Guillaume 提供了最好的解决方案,但没有实际展示实现。由于问题的作者无法弄清楚,我决定将其包含在未来的人们中。我发现该线程很有帮助,现在它不仅仅是有帮助。 :)
正如@Guillaume 所提到的,最好的解决方案是创建一个在表上查找的自定义宏。 “表”只是包含变体的其他文件的名称。在这种情况下,我假设您将文件命名为“Female.strings”,
这确保了最大的灵活性,并为您提供了一个不需要您为这两种情况定义条目的解决方案。这实际上对您的情况非常有用,因为并非所有短语都可能具有女性形式。
因此,以下解决方案不需要重复的条目,这非常酷!
首先定义宏:
然后定义扩展函数,如下所示:
@Guillaume provides the best solution, without actually showing the implementation. Since the author of the question wasn't able to figure it out, I decided to include it for future people. I found the thread helpful, now it is more than helpful. :)
As mentioned by @Guillaume, the best solution is to create a custom macro with a lookup on the table . The 'table' is just the name of the other file that contains the variants. In this case, I am assuming you will name your file 'Female.strings'
This ensures the most flexibility and gives you a solution that doesn't require you to define entries for BOTH cases. This is actually really useful for your case as not all phrases may have feminine forms.
As a result, the following solution doesn't require duplicate entries, which is pretty cool!
First define your macro:
Then define the expanded function as such:
几年来,Xcode 已经有了
LOCALIZED_STRING_MACRO_NAMES
,因此您可以放置自定义的NSLocalizedString
替代宏名称(如果它们遵循相同的参数结构),并且 Xcode 会正确导出/导入它们,类似于 < a href="https://stackoverflow.com/a/15732244/2105993">genstrings 答案For couple of years Xcode has
LOCALIZED_STRING_MACRO_NAMES
, so you can put your customNSLocalizedString
alternative macro names if they follow same parameters structure and Xcode will correctly export/import them, similar to genstrings answer这是我的自定义实现,它使用 NSLocalizedString 并使用注释作为默认值:
(NSLocalizedStringWithDefaultValue 不能与 genstring 正常工作,这就是我提出这个解决方案的原因)
1 。在预编译头文件(.pch 文件)中,重新定义“NSLocalizedString”宏:
2.创建一个类来实现本地化处理程序
3.使用它!
确保在应用程序构建阶段添加运行脚本,以便您的 Localized.strings 文件将在每次构建时更新,即新的本地化字符串将添加到您的 Localized.strings 文件中:
我的构建阶段脚本是一个 shell 脚本:
因此,当您在代码中添加此新行时:
然后执行构建,您的 ./Localized.scripts 文件将包含此新行:
由于 'view_settings_title' 的 key == value,因此自定义LocalizedStringHandler 将返回注释,即“设置”
Voilà :-)
Here is my Custom implementation that use NSLocalizedString that use comment as default value:
(NSLocalizedStringWithDefaultValue does not work properly with genstring, that's why I proposed this solution)
1 . In your pre compiled header (.pch file) , redefine the 'NSLocalizedString' macro:
2. create a class to implement the localization handler
3. Use it!
Make sure you add a Run script in your App Build Phases so you Localizable.strings file will be updated at each build, i.e., new localized string will be added in your Localized.strings file:
My build phase Script is a shell script:
So when you add this new line in your code:
Then perform a build, your ./Localizable.scripts file will contain this new line:
And since key == value for 'view_settings_title', the custom LocalizedStringHandler will returns the comment, i.e. 'Settings"
Voilà :-)
NSLocalizedString 只是 NSBundle.h 中定义的宏
重新定义它或创建一个新的,例如 NSGenderAwareLocalizedString 以满足您的需求。
使用新的宏,您可以自由地做任何您想做的事情:每种语言两个字符串文件,一个用于男性,一个用于女性。或者,您可以定义一个约定,从男性本地化字符串的键中派生出女性本地化字符串的键。
NSLocalizedString is just a macro defined in NSBundle.h
Redefine it or create a new one, for example NSGenderAwareLocalizedString to fit your needs.
With the new macro, you are free to do whatever you want: two strings files per language, one for male and one for female. Or you can define a convention to derivate the key for a localized string for a female from the key of the male localized string.