Android Strings

Last updated: December 01, 2022Author: Jakub Pomykała

String resources (Android Strings) is a common format used in Android localization and translation. Files are a regular XML files with .xml extension.

Values from strings.xml can be used in Android code using a reference R.string.my_translation_key in Java/Kotlin code or @string/my_translation_key in your XML layout files. Learn more about String resources.

String resources sample file

Sample strings.xml file. The name attribute is a translation key, and the value of <string> and <item> tags is are localized messages.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- This is a comment -->
    <string name="hello_world">Witaj Świecie!</string>
    <string name="welcome"><![CDATA[Witaj <b>%s</b>!]]></string>
    <!-- This is a comment for all plurals -->
    <plurals name="numberOfSongsAvailable">
        <item quantity="one">Znaleziono %d piosenkę.</item>
        <item quantity="few">Znaleziono %d piosenki.</item>
        <item quantity="other">Znaleziono %d piosenek.</item>
    </plurals>
    <!-- This is a comment for all array elements -->
    <string-array name="planets_array">
        <item>Merkury</item>
        <item>Venus</item>
        <item>Ziemia</item>
        <item>Mars</item>
    </string-array>
</resources>

String resources files in Android Studio

In Android world, translation files are usually named strings.xml and placed in res/values-XX directories, where XX is a language code. For example, res/values-en for English, res/values-pl for Polish, res/values-de for German, etc.

android localization files structure

Pluralization

Pluralization is a feature that allows you to define different translations for different numbers. For example, in English you can say "1 song" or "2 songs", but in Polish you need to say "1 piosenka" or "2 piosenki". In order to support this feature, you need to use <plurals> tag instead of <string> tag. The quantity attribute defines the number of items that the translation is for. The following values are supported:

  • zero
  • one
  • two
  • few
  • many
  • other
<plurals name="numberOfSongsAvailable">
    <item quantity="one">Znaleziono %d piosenkę.</item>
    <item quantity="few">Znaleziono %d piosenki.</item>
    <item quantity="other">Znaleziono %d piosenek.</item>
</plurals>

Translation editor shows plurals as separate translation keys for each quantity.

  • numberOfSongsAvailable.one = 'Znaleziono %d piosenkę.',
  • numberOfSongsAvailable.few = 'Znaleziono %d piosenki.',
  • numberOfSongsAvailable.other = 'Znaleziono %d piosenek.'.

Arrays

You can define arrays of strings using <string-array> tag. The array can be accessed in Java code using R.array.array_name and in XML layout files using @array/array_name.

<string-array name="planets_array">
    <item>Merkury</item>
    <item>Venus</item>
    <item>Ziemia</item>
</string-array>

Translation editor shows string arrays as separate translation keys for each index.

  • planets_array.0 = 'Merkury',
  • planets_array.1 = 'Venus',
  • planets_array.2 = 'Ziemia'.

Comments and description

You can add comments and description to your strings, string-arrays and plurals using <-- and --> tags. The comments and description are not visible in the app, but they are visible in the source code. Comments above the translation keys are automatically imported to translation editor as code descriptions.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- This is a comment -->
    <string name="hello_world">Witaj Świecie!</string>
</resources>

HTML tags

You can use HTML tags in your strings. But be careful, because some tags are not supported by all Android versions. For example, <b> tag is supported by all versions, but <u> tag is not supported by Android 4.0 and lower. Always use<![CDATA[...]]> tag to wrap your HTML code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome"><![CDATA[Witaj <b>%s</b>!]]></string>
</resources>

Note: <![CDATA[ won't show up in the translation editor, but it will be added during file export to every string that contains HTML tags.

Upload strings.xml files

Upload multiple language files

Use {lang} variable in --uploadPath parameter path to upload multiple strings.xml files. The {lang} variable will be replaced with a language key from the Languages tab in your project.

simplelocalize upload \
  --apiKey $MY_API_KEY \
  --uploadFormat android-strings \
  --uploadPath ./values-{lang}/strings.xml

Upload one file

Use --languageKey parameter to download strings.xml file for one language. The en value in --languageKey parameter is a language key from the Languages tab in your project.

simplelocalize upload \
  --apiKey $MY_API_KEY \
  --languageKey en \
  --uploadFormat android-strings \
  --uploadPath ./values/strings.xml

Learn more about upload command.

Replace existing translations

By default, the CLI will not replace existing translations (not empty) from file, but if you want to apply changes from file, you can use the --uploadOptions REPLACE_TRANSLATION_IF_FOUND parameter.

simplelocalize upload \
  --apiKey $MY_API_KEY \
  --languageKey en \
  --uploadFormat android-strings \
  --uploadPath ./values/strings.xml \
  --uploadOptions REPLACE_TRANSLATION_IF_FOUND

Learn more about upload options.

Download strings.xml files

Download files for all languages

Use {lang} variable in --downloadPath parameter path to download strings.xml files for all languages for languages in your project. The {lang} variable will be replaced with a language key from the Languages tab in your project.

simplelocalize download \
  --apiKey $MY_API_KEY \
  --downloadFormat android-strings \
  --downloadPath ./values-{lang}/strings.xml

Download one file

Use --languageKey parameter to download strings.xml file for one language. The en value in --languageKey parameter is a language key from the Languages tab in your project.

simplelocalize download \
  --apiKey $MY_API_KEY \
  --languageKey en \
  --downloadFormat android-strings \
  --downloadPath ./values/strings.xml

Learn more about download command.