📜 ⬆️ ⬇️

Localization of Android applications using Google Sheets

Good day!

In this article, I would like to share my little experience of using Google Tables when localizing an Android application. Initially, it was required to translate the developed application only into the language of the customer, i.e. Norwegian. At this time, IntelliJ Idea did not have a Translations Editor, and Android Studio was preparing to exit beta.

The main non-negotiable requirement of the customer was that the translator should spend as little time as possible on the translation. So, the translator needs to provide a table with strings for translation, he translates everything he needs, and then do whatever you want with it. No additional software could be put to him, let alone SCM.
')
TL; DR finished document . To get the strings.xml files, click Export Files -> Export sheets to strings.xml. The Google Drive application, which should be installed on your machine, will download the result.

At the very beginning, we used a simple Excel document for translation and email ping pong with a translator to communicate with it. After manually transferring ~ 100 strings from strings.xml to the table, the desire to continue this shameful occupation disappeared. It was implemented to import from xml into a table using Excel. In the future, this document has become logical: various conditional formatting for visualizing newly added lines, support for the situation when the key of the line has been renamed.

However, this whole structure had one big drawback - the lack of versioning. Our translator received a new translation file containing already translated strings, as well as empty cells, where this translation had to be entered. Everything was covered with a copper basin, when another 2 languages ​​were added: Swedish and Danish. Once, when merging 3 documents from 3 translators, the columns went to me, because of this, the wrong translations almost went into release.

As a result, a new requirement was received from the customer, as well as a new requirement: no complicated logic in the table, only 4 columns - a key, English source code, Norwegian, Swedish and Danish translations. To avoid problems with multiple copies of the document from each of the translators, I persuaded the customer to use Google Sheets. I was no longer going to export the translations to strings.xml in any manual way - I was more than a little staring for inconsistencies in the strings after the ill-fated merging of different copies of the translation, when sweating on the forehead appeared in the monitor. Then it turned out that for Google services you can very easily write extensions in a JS-like language - Google Script.

Naturally, I was looking for ready-made services that solve this problem. The first 3 links in Google and the response to SO did not give satisfactory results. By the way, by the time of moving to Google Sheets, a new version of Idea was released, where Translations Editor appeared, which made life a little easier.

So, a table was created in Google documents with 5 columns - key, English, Norwegian, Swedish, Danish and comments, as well as access rights were distributed. To put data into a table from a project, you need to select everything in the Translation Editor and insert it into any table editor, delete extra columns (Untranslatable - we don’t use it) and insert the result into a Google document. It is also necessary to mention that our project consists of several modules with their own translations; therefore, there are 2 tabs in the document, and the described operations should be performed once for each project.

The result is a table with strings for translation, which translators can easily edit together. When the translation is completed, you need to transfer the result back to your project. I began to think about how I would get everything at once. The maximum effort I was willing to make was 1 copy. The only solution that came to my mind was to export the results to Google Drive, from where the desktop application would pull me to my PC. If the correct folder hierarchy was previously created, then I can copy and overwrite the strings.xml files into 1 action. Here, without writing a small script is not enough.

Click Tools-> Script Editor and get into an almost full-fledged IDE with auto-completion and debug directly in the browser! Also created a small piece of code that creates an additional menu, you Google tables UI. Clicking on this item will be the entry point.

function onOpen() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var entries = [{ name : "Export Sheets To strings.xml", functionName : "readAndExport" }]; spreadsheet.addMenu("Export Files", entries); }; 


Next, a simple code goes through the sheets (tabs), reads the column headings, compiles an xml string from the values ​​in the cells and writes it to the strings.xml file on your Google disk in the trunk / [sheet_name] / res / values- [column_name] / strings directory .xml. The source itself can be viewed directly in the submitted file. From the unusual one can mention that in Google drive the name of a directory or file is not unique, i.e. You can create 2 files or directories with the same name. For everything to work properly, you need to use something like this code:
 function getOrCreate(root, name) { var iterator = root.getFoldersByName(name); if(iterator.hasNext()) { return iterator.next(); } else { return root.createFolder(name); } } 


Conclusion


Google Apps have a truly simple yet powerful API. Another advantage is the fact that right out of the box there is access to the API of any Google application: calendar, disk, mail, etc. I quickly managed to create a simple and convenient tool for localizing our application. Of course, there are still places in it that I would like to improve, however, I got rid of the main problem: copying and editing multiple files with the same structure and names. This inevitably led to errors caused by inattention.

Source: https://habr.com/ru/post/251943/


All Articles