How translations work internally on Android and how you can change them dynamically

Kris Pypen
Kunstmaan | Accenture Interactive
2 min readApr 10, 2018

On Android every translation string is defined in a strings.xml file. At compile time, aapt wil put every values resource in a resources.arsc file, after which the R.java file (containing ids) is generated. But this resources.arsc file can not be changed while running the application. Android also does not provide a way to dynamically change the translations.

When you start digging into the Android source code, you’ll find lots of places where translation keys/ids get translated. In the end, all of them will end up in the AssetManager class. This AssetManager contains a private array mStringBlocks containing the translation values and blockIds. Other then that, the AssetManager contains a private method loadResourceValue which returns the blockId based on the resourceId. With reflection we can work some magic, turn them public, and call them. Using this method, we can now get translations from memory.

Based on a resId, AssetManager.loadResourceValue returns an index to get the correct StringBlock from the mStringBlocks array. The TypedValue object that is passed into the AssetManager.loadResourceValue method is updated with a data property which is the index of the SparseString within that StringBlock’s mSparseStrings array. With these 2 indexes, we have what’s needed for getting and setting the translated String value.

If we want to live change the translation values, the only thing we have to do is load the original value, and then change the in-memory array of StringBlocks.

To wrap it up in a proof of concept, we created a live translation editor where you can view the currently used translations and change them live. And here can you find the Util class itself to get and change the translations.

Our live translation editor also adds a way to export the changes you made so they can be imported in you Android or iOS projects.

Originally published at labs.kunstmaan.be.

--

--