Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

objective c - Can I load a localizable.strings file into an iOS app over-the-air

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.

My app has facility to load files from ftp sites.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

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:

  • in your Mac create a directory, and call it "MyBundle"
  • inside this directory move your localized string(s) (if you have multiple languages in the bundle then the localizable.strings file will be inside the lproj directories: en.lproj, it.lproj, fr.lproj, ...)
  • then rename the directory to "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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...