Simple cross-platform string localization for iOS and Android

Introduction

During our daily development, me and the team I work with, figured out that a couple of things didn’t work smooth in our apps strings localization.

The key factor that drived our problems was that iOS and Android have different methods for doing this job.

iOS

iOS uses its “Localizable.strings” file, whose syntax is the following:

//History not found
"HISTORYNOTFOUND" = "The history has not been found";
//Feedback
"FEEDBACKERROR" = "Error in sending feedback, please try it later.";

which is pretty straightforward and it allows to comment the strings you write.

Android

Android has a different format, indeed it uses XML. For clarity, an example is the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

this format is pretty straightforward too,  there is a little bit of overhead given by XML, but nothing really relevant.

At the beginning we started in the simplest way possible: we didn’t care. I used the iOS syntax and my Android developer buddy used the Android one.

The thing went bad the day we had to begin an application that should be absolutely identical over the two platform, every single string should be exactly the same. Moreover the guy who should validate and sometime change the strings, was the customer!! We couldn’t obviously send him two different files and ask him to read them all and write the changes twice!

As we had both some very practical methods to deal with localization, one of us wouldn’t have been happy if one of the two syntax were chosen, therefore we had to find a common solution. About two minutes later I came up with..

The solution

JSON! What could have been the simplest, powerful, ready to use, notation we could use if not JSON?

Briefly, the new syntax we choose is the following:

{
    "HELLO_WORLD" : "Hello world!",
    "THANKS" : "Thank you very much!"
}

I believe that any explanation would be redundant, but I’ll spend a few words for whom who don’t already know JSON.

What be described with the syntax above is a sort of an hash table, or more precisely (since there are no hashes) what c++ guy calls a map or Objective-C guys (like me! :D) calls an NSDictionary. This is an NSDictionary containing two key-value pair. The nice thing of JSON (like XML) is that there is no need to deal with regular expressions or anything like that, everything comes with iOS SDK (iOS 5.0 and later) or, for those who still need to support older SDK, with a bunch of open source JSON Kit, like my favorite JSONKit. With a couple of lines of code we can do this:

NSDictionary _dictionary = nil;
NSString *content = @"{"HELLO_WORLD" : "Hello world!","THANKS" : "Thank you very much!"}";
NSError *error;
if (error) {
    NSLog(@"%@",error.userInfo);
}
else{
    _dictionary = [NSJSONSerialization JSONObjectWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
}

and we just generated a two key-value pair delicious NSDictionary!

Localizing our files

Now what we have to do is find a way to get the correct file with the current language. The simplest way I found was using the locale identifier as a suffix for my file name, which I called “Localizable_xx.json” where xx is replaced by:the result of [[NSLocale preferredLanguages] objectAtIndex:0]; .

Back compatibility

When I finished this simple job I was thinking: “Now what with that sweet NSLocalizedString(key,comment) macro? Can I make my own macro as well?”.

The answer was yes! So I write my own macro:

#define DMLocalizedString(_key,_comment) [[DMLocalizationManager sharedInstance]localizedStringForKey:(_key) comment:(_comment)]

that calls the singleton instance where I stored the previously mentioned NSDictionary  (more about singletons here).

Now that I have my macro I can simply replace “NSLocalizedString” with “DMLocalizedString” all around my project and that’s it!!

Conclusions

You can find a demo app here: https://github.com/Lolloz89/DMLocalizedString, the license is MIT so you are free to do what the heck you want with this code but don’t blame me if it causes you problems (hope this never happen) 🙂

My Android developer buddy should have made something similar, if you are interested in having the source, feel free to contact me!

Thank you for your attention!

Advertisements

4 thoughts on “Simple cross-platform string localization for iOS and Android

  1. Hi!
    If you’re interested in a tool to collaboratively localize iOS or Android software, I suggest you give https://poeditor.com/ a shot. It’s a very user-friendly online translation platform that handles multiple popular language file formats. You’ll see that it has a sum of very useful features to aid your localization workflow, like set reference language and translation memory. Cheers and good luck with your projects!

  2. Pingback: Simple cross-platform string localization for iOS and Android : DM Digital Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s