Skip to main content

App Localization: The Sri Lankan Way



I cannot believe the fact that the last blog post that I have written was in 2018. So I decided to start writing again, while working in a new work place, Yeah.. Yes! I had to move out from the "bank" and this post that I am writing is something that I did while I was at the "bank".

So I got the opportunity to develop a mobile application for both Android and iOS for Sri Lankans where it should have the feature of changing the language of the application. Basically an option to switch the app's language between Sinhala, Tamil & English. At first, I was like "Alright!! Not a big deal. I'll do it." Then came the tricky part. they say they need to switch the language from with in, Basically, app's language cannot have a dependency on the device's region. So this is something that apps like "Pick Me" has done.

UX POV this is a quite reasonable functionality when we develop apps for countries like Sri Lanka, where the device is totally runs on English rather than the users' native language. In an environment like this, changing the apps' language via the region might not be the best idea. But for a country like China, this approach that I have done might be totally useless and wrong.

So the approach that I have followed is quite simple. Basically to reload the all the String content of the UIs when user changed the Language and navigate through out the application.

Let's get on to it...

Android

First Step that I did when I'm creating my layouts is that to declare all the String content inside strings.xml file. We all know that is the best practice when even the Android Studio is being a huge PIA about it.

1
<string name="welcome">Welcome</string>

Then I have two paths to choose, either I put contents of the 3 languages in 3 separate strings.xml files or to have all the content in one file with different IDs and switch at run time. IMO having 3 separate files could be the best approach but what I did was to put all the String content in one file with different IDs because in that way it was so easy to change rather than searching in 3 documents when a change occurs. So it looked like below,

1
2
3
<string name="welcome">Welcome</string>
<string name="welcome_SI">ආයුබෝවන්</string>
<string name="welcome_TA">வரவேற்பு</string>

Bit tricky part was to switch the content at runtime. So for that, what I did was to declare a static method where it gets the selected language from Shared Prefs and change the ID accordingly. And it looked like this,

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static String getLocalizedString(String ID, Context context) {

        String lang = SharedPreferenceManager.getLanguage(context);

        if (lang.equals(Language.ENGLISH.getKey())) {
            return context.getString(context.getResources().getIdentifier(ID, "string", context.getPackageName()));
        } else if (lang.equals(Language.SINHALA.getKey())) {
            return context.getString(context.getResources().getIdentifier(ID + "_SI", "string", context.getPackageName()));
        } else {
            return context.getString(context.getResources().getIdentifier(ID + "_TA", "string", context.getPackageName()));
        }
    }

The rest is pretty obvious. Every Activity/Fragment has a method named "setupUI" which sets the content from the strings.xml file which looked like this,


welcomeTextView.setText(UIHelper.getLocalizedString("welcome", getContext()));

To be honest, passing the context in every String content has been a huge PIA. So one way of improving this may be to remove it. Furthermore, even if you decide to have 3 strings.xml files all you have to do is switch between the 3 files according to the picked language.

iOS

Okey!! Let's see what's the approach that I did in my favorite programming language. Basically I followed the same approach but with 3 different .plist files instead of just putting all in one. So I have created 3 plists named en ,  si and ta (Please use better names than thisand added all the relevant content to the files and wrote an extension for String in order to get the localized String as follows,


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
extension String{
    func getLocalizedString() -> String{
        let language:String = Language.getLanguage();
        var languageDict:[String:String]
        var resourceName:String
        
        switch language {
        case Language.LanguageType.English.rawValue:
            resourceName = "en"
        case Language.LanguageType.Sinhala.rawValue:
            resourceName = "si"
        case Language.LanguageType.Tamil.rawValue:
            resourceName = "ta"
        default:
            resourceName = "en"
        }
        
        if let path = Bundle.main.path(forResource: resourceName, ofType: "plist"){
            languageDict = NSDictionary(contentsOfFile: path) as! [String : String]
            return languageDict[self]!
        }else{
            return "nil"
        }
    }
}

In here, Language is a class where it gets the user selected language from the User Defaults. And same as in Android there was a method called "setupUI" which sets the content in every View Controller as below,


welcomeLabel.text = "welcome".getLocalizedString()

And wollah!! There's your localized app, in Sri Lankan way. :)

Please note that this is not the only way of doing this. This is just an approach that I settled for after looking at many many options. If in case you find/came up with a better option please do let me know
so that I can learn a thing or two from you. 🧡

Comments

  1. At chlsoftech App localization companies in India
    , our involvement is complete right from understanding your market to providing accurate localised version in any language of your choice, the mobile apps within your timelines. We have a dedicated team of Project Managers, localization engineers and technical experts to take care of all your mobile localization in any international and Indian language.

    ReplyDelete

Post a Comment

Popular posts from this blog

Apple September Event: iPhone XS?

Source: Ars Technica   Writing after a very long time from a different workstation at a different company. (Yes, I left that company and yes, i'm writing while i'm at work. *F irst of all  I deserve a break and second of all  Shhhhhhhhhhh!!! * ) Decided to write this as soon as I heard that the Apple has released the official invite for their September Event which as you all know it's their hardware event. (BTW the event is on 12th September at 10 AM PDT) Basically many parties have predicted that they'll be releasing a new Apple Watch, iPhone, high end Mac Mini, and a low budget MacBook Pro. But in this post i'll just stick to the iPhone. WTH happened to iPhoneX So the Apple iPhoneX, which was released last year didn't meet their sales expectations mainly due to the price tag. As a result, Apple decided to launch low price editions of the devices (Thanks to China) due to this reason and hence now we enjoying the 9.7-inch iPad. So yes, as you guessed,...

WSO2: Integrate your WSO2 product with New Relic

As you may already know  New Relic can be use as a tool for monitoring your java app. It let's you see the whole stack, front to back end in real time. So basically you can get an idea about the CPU,Disk,Memory,N/W usage regarding any WSO2 product realtime once you install New Relic in the instance it self. Steps Configuring New Relic with WSO2 is not that hard. All you have to do is follow the below steps that I have mentioned. Download the New Relic agent from [1]. (In here I have used newrelic-java-3.31.0 with WSO2 AM 1.10.0) Unzip the agent to <CARBON_HOME> . (So once you unzip it you should be able to access it from <CARBON_HOME>/newrelic ) Change the license key and the application name from  <CARBON_HOME>/newrelic/newrelic.yml (License Key will be added automatically when you download the zip file but it is a good practice to check it again. Please refer [2] for more info) This step is very important. If you...