Localize app name on Android

Here you can see how to localize the name we show for an application (only Android). This is only a workaround I found for a problem I could not find any other solution. Once I start working on iOS I will try to do something similar)

The idea is use the native localization on Andriod which expects files string.xml on folders like res/values-ll-rCC where ll is language code and CC is country code.
My solution was create a hook on ./hooks/after_prepare/020_myhook.js which copies my specific string.xml language on the proper folder, so I have this kind of files:

./resources/android/values/string_es.xml
./resources/android/values/string_es-rES.xml

where the content is, for example:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string name="app_name">Name in spanish</string>
    <string name="launcher_name">@string/app_name</string>
    <string name="activity_name">@string/launcher_name</string>
</resources>

then my hook:

var filestocopy = [{
    "resources/android/values/string_es.xml": 
    "platforms/android/res/values-es/string.xml"
}, {
    "resources/android/values/string_es-rES.xml": 
    "platforms/android/res/values-es-rES/string.xml"
}];

var fs = require('fs');
var path = require('path');
 
// no need to configure below
var rootdir = process.argv[2];

filestocopy.forEach(function(obj) {
    Object.keys(obj).forEach(function(key) {
        var val = obj[key];
        var srcfile = path.join(rootdir, key);
        var destfile = path.join(rootdir, val);
        console.log("copying "+ srcfile + " to " + destfile);
        var destdir = path.dirname(destfile);
        if (fs.existsSync(srcfile) && !fs.existsSync(destdir)) {
            fs.mkdirSync(destdir);            
        }
        if (fs.existsSync(srcfile)) {
            fs.createReadStream(srcfile).pipe(
               fs.createWriteStream(destfile));
        }
    });
});    

so, when you run ionic build android this hook will copy the string files I defined on the proper folder under res folder, creating each needed folder (Android expect all the files are string.xml but each one is on a folder with the localization values-es / values-es-rES, please note the “r” before the country code).

Once you install the apk created you can change the language on the device and see the change on the icon name of your app (It will take some seconds until the system changes the name).

Hope someone found this useful.

I took the base of the hook from: http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/ and did some changes to create the folder.

1 Like