in Android, Development, iOS

String/Localization handling for iOS and Android

How to handle text localization without going insane?

Every application has those embedded texts: You need to display an error message, have some basic text, etc. When you want to use the same strings for both your iOS and android applications you typically have to do some nice and frustrating copy/pasting.

At least I did.

No more:

Requirements

  • Install Twine
    gem install twine

  • Xcode and Android Studio

Conventions

We need a simple naming convention for our localization keys to work on both platforms. I personally prefer lowercase with underscore.

And for iOS there must not be any keys in interface builder files (because of Km8-1Y-A80.text)

Setup

Start from iOS

if you wanna start with an android project, please goto the android section below.

Generate Strings from source files:
genstrings -o en.lproj *.m

You should now find a Localizable.strings file in your project folder.
Now generate a twine file out of the apps strings

touch strings.txt
twine consume-all-string-files strings.txt ./en.lproj --developer-language en --consume-all --consume-comments

Generate the xml file for android:

twine generate-string-file strings.txt path-to-android-projects-string.xml

Now you should have a file containing the generated strings in your android project.

Start from Android

if you wanna start with an iOS project please goto the iOS section above.

Generate a twine strings file:

twine consume-all-string-files strings.txt path-to-android-config --developer-language en --consume-all --consume-comments

Generate the .strings file for iOS:

twine generate-string-file strings.txt path-to-ios-projects-localizable.strings

Simple Workflow

Edit the content of twine’s string.txt and export the platform specific translation/string files.
For UI editing please refer to https://github.com/mobiata/twine/tree/v0.6.0#user-interface

Advanced (automated) Workflow

A proposed automatic solution is to embed the strings.txt as a git subproject in the respective repos of iOS and android.
Now for every new build, the platform specific resource files will be generated.

Both iOS and android build processes allow you to add so called buildsteps:

iOS Workflow setup

Add the repo with the strings.txt as git submodule

git submodule add your-translation-git-repo ./twine

The needed setup for iOS is explained here: https://github.com/mobiata/twine/tree/v0.6.0#twine-and-your-build-process

Android workflow setup

Add the repo with the strings.txt as git submodule

git submodule add your-translation-git-repo ./twine

Add the following task to app/build.gradle:

task generateStrings {
String script = 'if hash twine 2>/dev/null; then twine generate-string-file ../twine/strings.txt ./src/main/res/values/generated_strings.xml; fi'
exec {
executable "sh"
args '-c', script
}
}

Now every time you build your app the strings are generated from the twine repo

Update strings file

With every update to the central strings repository you have to pull the changes in the submodules.

cd twine
git pull

Build your projects and enjoy the correct strings

Sample Implementation

iOS/Xcode

android/Android Studio

Translation repository

  1. There is an interesting service that provides sdk and online update for ios apps translation: http://localize.io
    You can manage you localisation files without the need to think about which version of the app you’re updating. You also don’t need to recompile the app each time you add a new language.

    • Nice platform, but I can’t find any information for android.
      The whole point of this exercise was to provide a ‘single point of edit’ for shared strings on both platforms.