Localizable.strings – How to load translation files dynamically from a web server and use them it inside your iPhone App

We all know we can use “Localizable.strings” to translate our Apps into different languages like this in our code:

NSLocalizedString(@"myKey", @"");

… if we have created our “Localizable.strings” file first and put in some keys and values… but what if I want my translation to be dynamic, that is, download it from a webserver and use the key value pairs of that file instead of our “locked” app resources like Localizable.strings after deployment?

Fortunately there is a solution to this problem! I’m also going to show you how to download the file from a webserver using ASIHTTPRequest, a wrapper around the CFNetwork API that makes it very easy for us to communicate with our webserver even through https!

Advertisement:

The first iPhone App ever that finds out who’s calling you before you answer the phone.

Step 1: Download the translation file (built up like Localizable.strings) from the web server

The first one, downloadLocalizableStringsFromServer is responsible – as the name already says – for downloading the translation file from the web server. Just adapt the parameters to your needs (URL of the resource, filename and foldername). The second method, saveData toFolder usingFilename, is responsible for actually storing our data persistently on our device’s memory so that we can access it later on when it comes to step 2. The third method, addSkipBackupAttributeToItemAtURL, I put there just to make things right and complete. Since your App nowadays would be rejected if you don’t consider this. You may not store data into the iCloud if the data is not “produced” by the user himself – caution: deployment target of your App should be at least 5.1!

Step 2: Tell NSBundle to use a different localization file

Now to make things comfortable for later usage we init a new cocoa bundle like this:

Very easy, right? 🙂
I defined here the static variable “staticCustomBundle” and a static getter so that we can access the bundle from everywhere we need it, for example in another class, etc… everything in the code above is pretty self-explaining. I have a method (theMethodThatUsesOurDynamicTranslationFile) that wants to use the value of a key inside our downloaded “Localizable.strings”. To access the bundle I call [THISCLASS getStaticCustomBundle]. The first time the method “getStaticCustomBundle” is called, we create automatically our “dynamic” bundle and prepare it so that we can access the translation file(s) in it. then we call the method:

Neat blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your theme. Bless you

Hi,
if we provide the complete path of the localizable.string file only, we are getting the value of the key. Simply mentioning the key and bundle name alone is not fetching the value.Please correct me if i am wrong.

Hi,
Is it possible to dynamically localize the settings bundle. My requirement is that i have set of language which is dynamically fetched from server. I need to localize text of my app in iPhone Setting screen. is there any way to do this.