It's never been as easy as it is now to launch into global markets via mobile apps. You must be wondering why I wrote this blog because we can easily support varying locales to understand and be usable by all users, right? Let's read why.
Earlier, providing L10n (localization) was just possible natively by integrating .strings
, .stringsdict
and .plist
files in Xcode. This approach had various drawbacks such as:
- App's language would be set to the device's language by default instead of choosing the preferred language.
- There was no out-of-the-box support for
.json
. However, JSON support could be accomplished natively by additional code. - The only way to access the strings was through a decentralized data store, which would make the app unknown to the latest localizations until updated manually.
These limitations led me to find a better solution to provide localization strings to the app. That's when I discovered this excellent library called L10n-swift
.
What is L10n-swift?
L10n-swift is a library that provides developers the ability to change the app's language "on the fly". It also supports .json
, in addition to .strings
, .stringsdict
and .plist
.
Other essential features that the L10n library provides include localized numbers, localized dates, and support for plurals. Read here to know more about the L10n library.
Let's dive into how this library works with the help of some examples.
How L10n-swift consumes .json?
Consider this sample JSON:
The above JSON can be accessed within the code using the dot notation, every dot denoting one level in the hierarchy. So let's say you want to access Dhruv's email, then you can access it via employee.email
, and if you want to access the city where he lives, then it can be done by employee.address.city
.
How to implement the L10n-swift library in Xcode?
You can install the library through CocoaPods using this command pod 'L10n-swift'
.
Post the pod installation, you need to set up some basic configurations according to your app requirement to set the local language and show the data from the respective JSON.
To localize the content, you can set app language and selected language JSON into the library at runtime.
Let's say we store the above JSON in a variable named localResponse
having dictionary datatype. This is how we can insert the JSON response into the library for localization:
L10n.shared.language = "en"
L10n.shared.inject(dictionary: localResponse)
With the help of inject
function, we can assign the JSON to the library at runtime.
To get the string localized, let's make an extension of String
datatype for localized
function.
extension String {
func localized() -> String {
return self.l10n()
}
func localized(_ arg: CVarArg) -> String {
return self.l10n(args: [arg])
}
func localized(_ args: [CVarArg]) -> String {
return self.l10n(args: args)
}
func localizedPlural(arg: CVarArg) -> String {
return self.l10nPlural(args: [arg])
}
func localizedPlural(args: [CVarArg]) -> String {
return self.l10nPlural(args: args)
}
}
Here’s how we can utilize it:
emailLabel.text = "employee.email".localized()
emailLabel
will have the text as dhruv@demo.com
.
Similarly, if we want to provide the welcome_title
with custom strings and display it in localized format, we can do it as:
welcomeTitleLabel.text = "employee.welcome_title".localized(["Dhruv", "Localization Demo"])
This will result in welcomeTitleLabel
displaying Hi Dhruv, welcome to Localization Demo
.
Another use case to identify singular and plurals and separate them from each other:
contributionLabel.text = "number_of_contributions".localizedPlural(5)
The above will pick the "number_of_contributions.other"
, resulting in displaying You have 5 contributions
.
We store the Localization.json
dictionary in our project file locally and in case of poor connectivity, we inject it at runtime. This ensures that the app is always in a localized state.
Note: Fallback JSON does not need to have the updated strings. It depends on your logic to pull the latest JSON from the server and keep the Localization.json
file up to date.
If you're looking to provide a similar localization solution in android, you can follow How to replace strings.xml dynamically in Android?.