Android XML

XML String Resource files to use with Android mobile app localization.

File Extensions .xml
API Extension xml
Format Options convert_placeholder, escape_linebreaks, enclose_in_cdata
Import Yes
Export Yes
Pluralization supported? Yes
Descriptions/Comments supported? Yes

Example

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <!-- This is the amazing comment for this commented key! -->
  <string name="commented_key">Check it out! This key has a comment! (At least in some formats)</string>
  <string name="empty_string_translation"/>
  <string name="key_with_line-break">This translations contains
a line-break.</string>
  <string name="nested.deeply.key">Wow, this key is nested even deeper.</string>
  <string name="nested.key">This key is nested inside a namespace.</string>
  <string name="null_translation"/>
  <plurals name="pluralized_key">
    <item quantity="one">Only one pluralization found.</item>
    <item quantity="other">Wow, you have %s pluralizations!</item>
    <item quantity="zero">You have no pluralization.</item>
  </plurals>
  <string-array name="sample_collection">
    <item>first item</item>
    <item>second item</item>
    <item>third item</item>
  </string-array>
  <string name="simple_key">Just a simple key with a simple message.</string>
  <string name="unverified_key">This translation is not yet verified and waits for it. (In some formats we also export this status)</string>
</resources>

Format Options

Option Type Upload Download Default Description
convert_placeholder boolean close check false Placeholder will be converted to match format specific requirements. Example: '$@' => '$s'
escape_linebreaks boolean close check false All linebreaks will be escaped as '\n'.
enclose_in_cdata boolean close check false Encloses translations containing html tags in CDATA

“My HTML tags are not displayed when setting the text via view.setText”

Use this to set the text of the view.

SpannedString text = new SpannedString(getResources().getText(R.string.my_text));
    view.setText(text);

“I can not use placeholders in formatted text”

To format text containing placeholders like “Hello <b>%1$s</b>”, you have two options.

  • Escape the HTML in PhraseApp
  • Escape the HTML in your code.

To do it in the code, set the text of the view like this:

SpannedString rawText = new SpannedString(getResources().getText(R.string.my_text));
String formattedString = String.format(Html.toHtml(rawText), "Jack");
SpannedString formattedText = new SpannedString(Html.fromHtml(formattedString));
view.setText(formattedText);

Which would result in the output “Hello Jack!”

“Using a colorcode (#ff0000) in my font tag causes the text not to show”

Escape the HTML in the string or use a method similar to the one above.

SpannedString rawText = new SpannedString(getResources().getText(R.string.my_text));
SpannedString text = new SpannedString(Html.fromHtml(Html.toHtml(rawText)));
view.setText(text);

“How do I escape my HTML in PhraseApp and use it?”

  1. Escape the HTML in PhraseApp (replace < with &lt; and > with &gt;)
Hello &lt;b&gt;World&lt;/b&gt;!
  1. Use the translation center or the API Client to download your translations.

  2. Set the text in Java like so

String rawString = getResources().getString(R.string.my_text);
SpannedString text = new SpannedString(Html.fromHtml(rawString));
view.setText(text);
  1. The output should be “Hello World!”

“Why is the ‘translatable=false’ tag ignored in PhraseApp?”

The translatable=false attribute on the <string> element is not evaluated and persisted in PhraseApp. However if a string should not be translated, you can define all your non-translatable strings in a seperate resource file, for example donottranslate.xml. Since these strings should never be translated, there is no need to push this file to PhraseApp. More information can be found here.

“Can I share my translations between iOS and Android?”

If you want to share your translations between iOS and Android there are several things you should have in mind:

  1. Use simple key names with no special characters and without white spaces - use underscores or dots instead. Most of the special characters are not supported in Android resource files. Rather than using the generated key names from Xcode like RMx-3f-FxP.title, use descriptive key names like ‘myniceapp.title’. This also makes them more readable.
  2. Avoid using CDATA tags because they are difficult to handle in iOS and can be avoided by correctly escaping the strings in most cases.
  3. Activate the “Escape line breaks” format option when exporting for Android to get an identical output on both platforms.

Further Reading