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