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,
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,
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.
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 this) and added all the relevant content to the files and wrote an extension for String in order to get the localized String as follows,
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,
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. 🧡
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. 🧡
At chlsoftech App localization companies in India
ReplyDelete, 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.