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

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

问题描述

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



有任何方法可以添加额外的本地化应用程序(iOS或Mac OS),而无需在编译时从资源束加载它们。

解决方案

是的,但这不是你的想法。 / p>

您可以将字符串文件加载到 NSString ,然后使用 - [NSString propertyListFromStringsFileFormat]



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



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

  genstrings -sJPLocalizedString... 



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

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

以及 JPLocalizedStringFromTable() JPLocalizedStringFromTableInBundle() JPLocalizedStringWithDefaultValue() genstrings 将会拾取所有这些。 (换句话说,只是因为 NSLocalizedString 是一个宏并不意味着你的版本必须是)



你这样做并使用这些 JPLocalizedString 变体,然后 genstrings 仍然会为你生成你的字符串文件 -s 标志)。



一旦这些函数被调用,你可以使用任何你想要的查找机制,到 NSLocalizedString 版本。如果找不到任何东西。


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

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.

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.

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];
}

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)

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

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天全站免登陆