我可以通过无线方式将 localizable.strings 文件加载到 iOS 应用程序中吗 [英] Can I load a localizable.strings file into an iOS app over-the-air

查看:19
本文介绍了我可以通过无线方式将 localizable.strings 文件加载到 iOS 应用程序中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的应用程序仅以英语运行.但我不想每次添加新语言时都发布新版本.我的建议是将此 localizable.strings 文件远程加载到我的应用程序中.

Lets say my app only runs in English. But I do not want to release a new version for every time I add a new language. My proposal is to load this localizable.strings file remotely into my app.

我的应用可以从 ftp 站点加载文件.

My app has facility to load files from ftp sites.

你们认为可以通过这种方式加载语言吗?还是App必须在编译时编译语言文件?

Do you guys think is possible to load languages this way? Or does the App has to compile the language file at compile time?

推荐答案

所有本地化的字符串资源(以及许多其他类型的资源)都是从包中提取的.通常,应用程序使用主包",它是由 XCode 与您的应用程序一起创建的.但是你可以单独创建任何其他包,只要你使用正确的结构,然后你可以将它下载到你的应用程序中,最后使用 NSLocalizedStringFromTableInBundle() 函数提取本地化字符串.

All localized string resources (plus many other kind of resources) are extracted from the bundle. Usually an app uses the "main bundle" which is the one that is created by XCode together with your app. But you can create separately any other bundle, provided you make it with the correct structure, then you can download it in your app and finally extract a localized string using the NSLocalizedStringFromTableInBundle() function.

假设你提取一个键KEY"的语言翻译,那么正常的语法是:

So let's say you extract for a key "KEY" the language translation, then the normal syntax would be:

NSString *translated = NSLocalizedStringFromTable(@"key",nil,nil);

但此选项的一个变体允许您指定捆绑包:

but there is a variant of this option that allows you to specify the bundle:

NSString *translated = NSLocalizedStringFromTableInBundle(@"key",nil,myBundle,nil);

在标准情况下,您将 myBundle 替换为 [NSBundle mainBundle] 但如果您想使用另一个包,您可以通过以下方式指定它:

In the standard case you replace myBundle with [NSBundle mainBundle] but if you want to use another bundle you can specify it in this way:

NSString *myBundlePath = "the path to the downloaded bundle";
NSBundle *myBundle = [NSBundle bundleWithPath:myBundlePath];
NSString *translated = NSLocalizedStringFromTableInBundle(@"key",nil,myBundle,nil);

可以在 Apple 文档的Bundle Programming Guide"中查看包的完整结构,但在您的情况下,您可以简单地以这种方式创建:

The full structure of a bundle can be seen in the "Bundle Programming Guide" in the Apple docs, but in your case you can simply create in this way:

  • 在您的 Mac 中创建一个目录,并将其命名为MyBundle"
  • 在此目录中移动您的本地化字符串(如果您在捆绑包中有多种语言,则 localizable.strings 文件将位于 lproj 目录中:en.lproj、it.lproj、fr.lproj...)
  • 然后将目录重命名为MyBundle.bundle"

您会在最后一个操作中注意到,现在这个对象被视为一个独立的对象,但实际上它是一个目录.

You will notice with the last operation that now this object is seen as a standalone object but in fact it is a directory.

现在您可以决定采用多包方法还是采用单包技术:在后一种情况下,您可以打包所有语言,然后通过使用自动系统本地化规则,使用唯一更新的包进行语言翻译;在另一种情况下,您可以为每种语言制作一个包,然后 - 基于当前选择的语言 - 加载适当的包并为您的翻译选择它.

Now you can decide to have a multiple-bundle approach or follow a single-bundle technique: in the latter case you can package all the languages and then use the unique updated bundle for language translation by using the automatic system localization rules; in the other case you can make a bundle for each language and then - based on the currently selected language - load the appropriate bundle and choose it for your translations.

这篇关于我可以通过无线方式将 localizable.strings 文件加载到 iOS 应用程序中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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