How to export Translations Editor entries to Excel?
Asked Answered
M

1

6

In older versions of Android Studio, it was possible to simply select all entries and copy/paste them into excel.

Now I'm using Android Studio 4.0.1 and for some reason they disabled Cmd+A hotkey in the Translations Editor.

I looked into all available Plugins/3rd party tools, but none of them seem to work for me. Also checked similar SO threads and none of the ideas there helped me.

I have enormous string.xml files translated into 10 languages, so I'm looking for automated solutions only.

Menis answered 20/11, 2020 at 2:48 Comment(2)
Search the key map by pressing cmd+shift+A. Or you can edit your keymaps by going to - Android Studio -> Preferences -> Keymap (on Mac)Ferren
Thanks for the suggestion, my cmd+A is correctly mapped to select all, the problem is that they disabled selecting all(or even more than 1) in Translations Editor.Menis
H
5

I had the same issue in Android Studio, i didn't have access to copy all the entries from the Translation Editor, so i decided to do my own XmlResourceParser which reads all the keys and values from a specific strings.xml file. Of course, you can use and modify this script according to your needs.

private void parseResourceXML(XmlResourceParser parser) {

        try
        {
            int eventType = parser.getEventType();

            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            StringBuilder sbValues = new StringBuilder();
            StringBuilder sbKeys = new StringBuilder();

            String nameAttr = "";
            boolean translatableAttr = true;
            while (eventType != XmlResourceParser.END_DOCUMENT)
            {
                if (eventType == XmlResourceParser.START_DOCUMENT)
                {
                    //Log.d("XmlResourceParser", "Start Document");
                }
                else if (eventType == XmlResourceParser.START_TAG)
                {
                    //Log.d("XmlResourceParser", "Start Tag " + parser.getName());
                    String element = parser.getName();
                    if (!TextUtils.isEmpty(element))
                    {
                        switch (element)
                        {
                            case "string":
                                nameAttr = parser.getAttributeValue(null, "name");
                                translatableAttr = parser.getAttributeBooleanValue(null, "translatable", true);
                                map.put(nameAttr, nameAttr);
                                sbKeys.append(nameAttr);
                                sbKeys.append("\n");
                                break;
                            case "string-array":
                                nameAttr = parser.getAttributeValue(null, "name");
                                translatableAttr = parser.getAttributeBooleanValue(null, "translatable", true);
                                break;
                            case "item":
                                nameAttr = parser.getAttributeValue(null, "name");
                                translatableAttr = parser.getAttributeBooleanValue(null, "translatable", true);
                                map.put(nameAttr, nameAttr);
                                sbKeys.append(nameAttr);
                                sbKeys.append("\n");
                                break;
                        }
                    }
                }
                else if (eventType == XmlResourceParser.END_TAG) {
                    //Log.d("XmlResourceParser", "End Tag " + parser.getName());
                }
                else if (eventType == XmlResourceParser.TEXT) {
                    //get the String value text
                    String text = parser.getText();
                    map.put(nameAttr, text);
                    sbValues.append(text);
                    sbValues.append("\n");
                }
                eventType = parser.next();
            }

            String sKeys = sbKeys.toString();
            String sValues = sbValues.toString();

            int i = 0; //<------- PUT A BREAK POINT HERE and point to sKeys or sValues and click + button and then click on VIEW button (in debug mode) and copy them all. Then Paste all sKeys in Excel in Column1 and sValues in Column2

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And you can call it like below in your onCreate method of your Activity:

parseResourceXML(getResources().getXml(R.xml.strings));

and put your strings.xml or localised strings_fr.xml under res/xml folder. From the above script just put a break point into sKeys or sValues at the end and click the + button to view the data and then click on VIEW button (in debug mode) to see all data in plain text and now you can select them all and paste them in an Excel column. I hope it covers your case as well.

Hagridden answered 1/12, 2020 at 10:53 Comment(1)
Thanks for this script, it's a good start. I will modify it for my needs, but for the initial purpose it works.Menis

© 2022 - 2024 — McMap. All rights reserved.