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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With