Yet another localisation approach for mobile development

Dmitrijs Beloborodovs
Citadele Bank Developers
3 min readOct 23, 2018

You either do care about localisation in your project, or you don’t. This article for those who do.

“person using laptop” by Thomas Lefebvre on Unsplash

There are various (some are very complicated) tools to manage translations on single or many platforms. Let’s check simple one:

Twine

In other words: Twine it is a tool that allow you to keep all your translations in one text file and generate localisation files for many platforms.

Installation is pretty simple:

gem install twine

To start using just create empty strings.txt file.

Now run in console from same folder with strings.txt the following:

twine generate-localization-file strings.txt Localizable.strings --lang en --tags infoapp --format apple

This should produce Localizable.string file with content:

/**
* Apple Strings File
* Generated by Twine 1.0.3
* Language: en
*/
/********** General **********//* Welcome screen upper label */
"label_welcome" = "Hello world!";

All but tags is obviuos. Tags used to differentiate localisable string between targets or… platforms. Yes you may generate strings not for iOS project only but many others. For now, let’s stay with iOS only.

Shall we add translation? Let’s add text for two buttons, but for now they both share same translation:

[button_welcome_bottom_left]
en = Press me
tags = infoapp
ru = Жмяк
comment = Bottom button
[button_welcome_bottom_right]
ref = button_welcome_bottom_left
tags = infoapp

Let run generate once again and get content for our Localizable.strings:

/**
* Apple Strings File
* Generated by Twine 1.0.3
* Language: en
*/
/********** General **********//* Welcome screen upper label */
"label_welcome" = "Hello world!";
/* Bottom button */
"button_welcome_bottom_left" = "Press me";
/* Bottom button */
"button_welcome_bottom_right" = "Press me";

Now we have all texts for our simple app:

Copy Localizable.strings file into project and you are done!

Let’s automate!

Now we have all our translations in one place. Marketing team constantly work on translation: fix mistype, provide better words, etc. We wan’t get new updates ASAP/daily/weekly. Let’s put strings.txt into separate git repository. And add script.sh into it. This file will contain all generation calls to twine and copy produced files into our project:

# prepare workspace
rm -Rf generated
mkdir generated/en
mkdir generated/ru
# generate files
twine generate-localization-file strings.txt en/Localizable.strings --lang en --tags infoapp --format apple
twine generate-localization-file strings.txt ru/Localizable.strings --lang ru--tags infoapp --format apple
# copy files into projectcp en/Localizable.strings ../<project>/<target>/en.lproj
cp ru/Localizable.strings ../<project>/<target>/ru.lproj

With CI we can go further: call this script and upload changes to repository. To prevent some corrupted data appear in your strings.txt file there is a verification check:

twine validate-twine-file strings.txt --pedantic

Yet another task for your CI to run this after each commit in project where you store translation file. Good luck!

--

--