.strings 资源文件可以在运行时添加吗? [英] Can .strings resource files be added at runtime?

查看:15
本文介绍了.strings 资源文件可以在运行时添加吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Mac 应用程序中可以将 .strings 文件添加到项目文件夹以添加本地化.

I know that in Mac apps one can add .strings files to the project folder to add localizations.

有没有什么方法可以将额外的本地化添加到应用程序(iOS 或 Mac OS),而无需在编译时从资源包中加载它们.比如说,下载一个额外的本地化文件并将其存储在 iOS 上的/Documents 中?

Is there any way that additional localizations can be added to an app (iOS or Mac OS) without loading them from the resources bundle at compile time. Say, downloading an additional localization and storing it in /Documents on iOS?

推荐答案

是的,但不是你想的那样.

Yes, but it's not what you think.

您可以获取一个字符串文件并将其加载到 NSString 中,然后使用 -[NSString propertyListFromStringsFileFormat].

You can take a strings file and load it into an NSString, and then transform it into a dictionary using -[NSString propertyListFromStringsFileFormat].

这将为您提供一种将自定义翻译的字符串存储在内存中的方法.

This will provide you with a way to store your custom-translated strings in-memory.

至于实际使用它,您必须定义自定义翻译函数.IE,你不能再使用 NSLocalizedString() 和朋友了.幸运的是,genstrings(用于生成字符串文件的实用程序)允许您指定 自定义函数名称:

As for actually using that, you'll have to define custom translation functions. IE, you can't use NSLocalizedString() and friends any more. Fortunately, genstrings (the utility used for generating strings files) lets you specify custom function names:

genstrings -s "JPLocalizedString" ...

这意味着在代码中,你可以定义:

This means that in code, you can define:

NSString* JPLocalizedString(NSString *key, NSString *comment) {
  return [myLoadedStrings objectForKey:key];
}

还有 JPLocalizedStringFromTable()JPLocalizedStringFromTableInBundle()JPLocalizedStringWithDefaultValue().genstrings 将获取所有这些.(换句话说,仅仅因为 NSLocalizedString 是一个宏并不意味着你的版本必须是)

As well as JPLocalizedStringFromTable(), JPLocalizedStringFromTableInBundle(), JPLocalizedStringWithDefaultValue(). genstrings will pick up all of those. (In other words, just because NSLocalizedString is a macro doesn't mean your version has to be)

如果您这样做并使用这些 JPLocalizedString 变体,那么 genstrings 仍会为您生成字符串文件(前提是您使用 -s 标志).

If you do this and use these JPLocalizedString variants, then genstrings will still generate your strings files for you (providing you use the -s flag).

一旦调用了这些函数,您就可以使用任何您想要的查找机制,如果找不到任何东西,则默认返回 NSLocalizedString 版本.

Once these functions are called, you can use whatever lookup mechanism you want, defaulting back to the NSLocalizedString versions if you can't find anything.

这篇关于.strings 资源文件可以在运行时添加吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆