9 min read
Translate an Android strings.xml or XLIFF file in Excel: what works
By Quentin Delepierre, Salesforce Commerce Cloud consultant
The texts of an Android app live in a strings.xml file; those of a translation project are often in an XLIFF file. For a translator, an Excel table beats raw XML. ExcelifyXML turns these files into a table and rebuilds the XML after translation. We ran invented test files through the engine: here is what works, what doesn't (strings whose text is mixed with tags), and the fallbacks.
Got your own XML? Drop it in and see it as a table, free and with no account. For an Excel file you can edit and then rebuild into an XML that keeps its structure, a free account is all you need.
Simple Excel table, free with no account, for files up to 1 MB. File that rebuilds into XML: free account, 3 free credits, no credit card.
In short: what works, what doesn't
- Works: a strings.xml of simple strings (<string name="…">text</string>), with their escaped apostrophes, &, %1$s, \n, @string/… references and CDATA text; an XLIFF 1.2 with <trans-unit>, <source> and <target>; a simple XLIFF 2.0, tried on a small example.
- Works with limits: <plurals> (you translate the forms that exist, you can't add any), <string-array> (one cell per array, one value per line), a file mixing strings, arrays and plurals (the table holds the <string> elements only).
- Doesn't work: a string whose text is mixed with sub-tags (<b>, <xliff:g> in a strings.xml, <g> or <x/> in an XLIFF). The text around the sub-tag isn't taken into the table, so it is lost at rebuild. Handle those strings in the original file.
- ExcelifyXML doesn't translate: it puts the texts in a table and puts your translations back into the XML. The file to translate and the translated file are two files you keep and file away yourself.
A simple strings.xml, end to end
An invented test file, with one string that isn't translated (translatable="false"). The engine detects <string> as the row tag, gives 4 rows and 3 columns, and shows no warning. In the Excel file, the headers read @name, @translatable and #text (“#” can't start an XML tag name: #text stands for the row's text).
- Copy the source-language strings.xml and keep the original: it's your way back.
- Drop the copy on ExcelifyXML: the free preview, with no account, shows one row per <string>. Confirm with “Transform to CSV — 1 credit”, then choose the Excel file (.xlsx).
- In Excel, replace the text of the #text column with the translation. Don't touch the @name column (it's the identifier Android code calls) or the headers.
- Delete the rows that aren't translated (translatable="false"): a deleted row disappears from the XML, as in the example. Keep the row if you want it back as is in the translated file.
- Save, then drop the Excel file, alone, on the Rebuild page (free).
- Save the resulting XML in the project folder for the language. For French that's res/values-fr/strings.xml, just as Android does for Polish with res/values-pl/strings.xml.
- A working column added on the right of the table (“Translation”, “Translation FR”) must be deleted before you save. Left filled, it stops the rebuild on the “Filled cells won't be in the XML” screen: a <string> can't keep both its text and an extra tag, and the XML would lose the string's text. Paste it as values onto #text, delete it, then drop the file again. “Rebuild anyway” doesn't keep your translation: depending on the column's name, it becomes a tag in place of the string's text, or it is left out.
- With the standalone Excel workbook and its button (.xlsm), a column with a one-word name (“Translation”) doesn't trigger this stop: the macro writes the tag and the string's text disappears from the XML. Delete every working column before you generate the XML.
- The rebuilt XML is indented with two spaces: a file indented with four spaces changes layout, not content.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
<string name="welcome">Welcome</string>
<string name="save">Save</string>
<string name="brand_code" translatable="false">APP-01</string>
</resources>@name , @translatable , #text
app_name , , My App
welcome , , Welcome
save , , Save
brand_code , false , APP-01@name , @translatable , #text
app_name , , Mon appli
welcome , , Bienvenue
save , , Enregistrer<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mon appli</string>
<string name="welcome">Bienvenue</string>
<string name="save">Enregistrer</string>
</resources>Apostrophes, quotes, & and %1$s: what stays intact
In a strings.xml, some characters are written in a particular way. Android's documentation asks you to escape apostrophes and quotes with a backslash (\' and \"), to write \n for a line break and \@ for an at sign; special XML characters are written as entities (&, <), and format arguments look like %1$s. ExcelifyXML changes none of these. Tried on a test file:
- In the cell, “&” and “<” are written as they are: entities are read on the way in and written back on the way out. CDATA text stays CDATA.
- Keep the backslashes in the translation: “Vous n\'êtes pas connecté”. ExcelifyXML doesn't add them for you.
- A cell like “@string/app_name” is a reference: don't translate it.
- The markup of a CDATA text (<b>terms</b> above) sits in the cell: it isn't the “text mixed with tags” case below, since the file holds no XML sub-tag. It is translated as text, keeping the tags.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
<string name="not_signed_in">You\'re not signed in</string>
<string name="press_save">Press \"Save\"</string>
<string name="terms">Terms & conditions</string>
<string name="greeting">Hello %1$s, you have %2$d messages</string>
<string name="two_lines">Line one\nLine two</string>
<string name="reference">@string/app_name</string>
<string name="html_terms"><![CDATA[Read the <b>terms</b>]]></string>
</resources>My App
You\'re not signed in
Press \"Save\"
Terms & conditions
Hello %1$s, you have %2$d messages
Line one\nLine two
@string/app_name
Read the <b>terms</b>Arrays (<string-array>) and plurals (<plurals>)
Both blocks have <item> elements inside, and the table has only one kind of row. What the engine does, tried on small files:
- A file with several <string> elements plus arrays and plurals: the row is <string>, and only those strings are in the table. The <string-array> and <plurals> are written back as they are in the rebuilt XML, but they aren't translated: translate them in the file.
- A file of <plurals> only: one row per <plurals>, with columns item[0].@quantity, item[0], item[1].@quantity, item[1]. You translate the texts of the forms that exist (one, other). Adding a form the language needs (few, many…) doesn't go through: an added item[2] column isn't written, and the rebuild stops on the screen about filled cells that won't be in the XML. For languages that need more forms than English, add them in the file.
- A file of <string-array> only: one row per array, with the values in a single cell, one per line. In Excel each value sits on its own line in the cell; translate them in the same order.
- A very small file: with a single <string> next to an array and plurals, detection prefers <item> and refuses the file (“The <item> rows of this file sit in different elements”). In the preview, “Not the right row?” lets you type the string tag in the “Another tag” field. A real file, with several strings, doesn't have this problem.
<resources>
<plurals name="messages">
<item quantity="one">%d message</item>
<item quantity="other">%d messages</item>
</plurals>
<plurals name="files">
<item quantity="one">%d file</item>
<item quantity="other">%d files</item>
</plurals>
</resources>@name , item[0].@quantity , item[0] , item[1].@quantity , item[1]
messages , one , %d message , other , %d messages
files , one , %d file , other , %d filesWhat doesn't work: text mixed with tags
In a strings.xml you can put a word in bold with <b>, or mark a format argument with <xliff:g>. The string's text is then cut by a sub-tag. ExcelifyXML turns the sub-tags into columns, but doesn't take the text around them: at rebuild, that text is gone. The engine flags it with the “Text mixed with tags” warning, naming the tag concerned (here <string>). Invented test file:
- The loss is silent in the rebuilt file: only the warning announces it. So don't translate these strings this way, even if the rest of the file goes through fine.
- The plain strings of the same file (app_name here) aren't touched. Spot the strings concerned in the table: they are the ones where #text is empty and a sub-tag column (b, xliff:g…) is filled.
- Fallback: take those strings out of the file before dropping it (keep them in a copy), translate the rest with ExcelifyXML, then add the tagged strings, translated by hand, to the resulting XML.
- Another way: a translation tool made for localization files, which knows how to keep inline tags as elements not to be touched. ExcelifyXML isn't one.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">My App</string>
<string name="hint">Tap <b>Save</b> to continue</string>
<string name="hello">Hello <xliff:g id="name" example="Marie">%1$s</xliff:g>, welcome</string>
</resources>@name , #text , b , xliff:g.@id , xliff:g.@example , xliff:g
app_name , My App , , , ,
hint , , Save , , ,
hello , , , name , Marie , %1$s<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">My App</string>
<string name="hint">
<b>Save</b>
</string>
<string name="hello">
<xliff:g id="name" example="Marie">%1$s</xliff:g>
</string>
</resources>XLIFF 1.2: source, target, state
An XLIFF 1.2 file puts each text in a <trans-unit> with its <source> and its <target>. Invented test file, with one missing translation (the third target is empty). The engine detects <trans-unit> as the row tag: 3 rows, 7 columns. It gives a single piece of information, about empty tags (<target>) that don't come back; here that's harmless, since a unit with no <target> is a unit to translate.
- An empty target cell writes no tag; filling it creates one, right after <source>. So you translate by filling the target column.
- The target's attributes are columns: a state (state="needs-translation") shows up in a target.@state column. It doesn't change by itself when you translate: update it yourself, for example to “translated”. An empty target that carries a state is written back as <target state="…"/>.
- <note> elements become a note column and come back in place. A file with several <file> elements keeps the file.@original attribute on each row: a file's rows stay in their <file>.
- No <target> tag in the whole file, but some <note>? A target column added by hand is written at the end of the unit, so after the note. In XLIFF 1.2 the content of a <trans-unit> goes in the order source, target, then notes: some tools complain. Put an empty <target> in at least one unit before the transformation, or check the file in your translation tool.
- XLIFF 2.0: tried on a small file with no inline tags (<unit>, <segment>, <source>, <target>), the row is <segment>, with columns file.@id, unit.@id, source and target, and the XML comes back with its structure. Same limit on inline tags.
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="fr" datatype="plaintext" original="app">
<body>
<trans-unit id="1">
<source>Save</source>
<target>Enregistrer</target>
</trans-unit>
<trans-unit id="2">
<source>Cancel</source>
<target>Annuler</target>
</trans-unit>
<trans-unit id="3">
<source>Welcome back</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>file.@source-language , file.@target-language , file.@datatype , file.@original , @id , source , target
en , fr , plaintext , app , 1 , Save , Enregistrer
en , fr , plaintext , app , 2 , Cancel , Annuler
en , fr , plaintext , app , 3 , Welcome back ,<trans-unit id="3">
<source>Welcome back</source>
<target>Bon retour</target>
</trans-unit>What doesn't work: an XLIFF's inline tags
XLIFF 1.2 allows inline tags inside <source> and <target> (<g>, <x/>, <bx/>, <ex/>, <ph>, <mrk>…) to mark a link, a line break or an argument. Same flaw as above: the text around the tag isn't taken. Invented test file:
- The table has columns like source.g, source.g.@id, target.g: the sentence is cut up there, without its text. The engine announces “Text mixed with tags”, naming <source> and <target>.
- Units with no inline tag in the same file stay correct (“Plain” and “Simple” in our test).
- Fallback: translate these units in a translation tool that handles inline tags, or directly in the XML. With ExcelifyXML, take them out of the file before the transformation, then put them back after.
<trans-unit id="1">
<source>Click <g id="1">here</g> to continue</source>
<target>Cliquez <g id="1">ici</g> pour continuer</target>
</trans-unit><trans-unit id="1">
<source>
<g id="1">here</g>
</source>
<target>
<g id="1">ici</g>
</target>
</trans-unit>Before you start: a quick check
- Drop the file in the free preview (no account) and read the warnings: “Text mixed with tags” means strings to handle another way.
- Look at the chosen row (<string>, <trans-unit>, <plurals>) and at the columns.
- Try a file cut down to a few strings first, then read the rebuilt XML before bringing it into the project.
Have a strings.xml or an XLIFF to translate? Drop it to see the free preview, no account needed:
Transform a file