Handling Android String Resources

A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file).

Therefore, you can combine string resources with other simple resources in the one XML file, under one <resources> element.

Android String Resources have standardised XML logic to differentiate between certain types of string resources:

  • String
  • String Array
  • Quantity Strings (Plurals)

String

File location:

res/values/filename.xml

The filename is arbitrary. The <string> element's name will be used as the resource ID.

Requirements:

<resources> - this must be the root node.

Must have no attributes.

<string> - this is a string, must be one of the children of the <resources> root tag

Attributes:

name - attribute of the <string> tag. This will be used as resource ID

Example:



<?xml version="1.0" encoding="utf-8"?>

<resources>

  <string name="stringName">stringText</string>

</resources>

String Array

File location:

res/values/filename.xml

The filename is arbitrary. The <string-array> element's name will be used as the resource ID.

Requirements:

<resources> - this must be the root node.

Must have no attributes.

<string-array> - this is a string-array, must be one of the children of the <resources> root tag. It should contain one or more <item> children elements.

Attributes:

name - attribute of the <string-array> tag. This will be used as resource ID.

<item> - this is a string, must be one of the children of a <string-array> tag

Must have no attributes.

Example:



<?xml version="1.0" encoding="utf-8"?>

<resources>

  <string-array name="employees">

    <item>John</item>

    <item>Bill</item>

    <item>Jack</item>

  </string-array>

</resources>

Quantity Strings (Plurals)

Different languages have different rules to represent quantity. In English, for example, the quantity 1 is a special case. We write "1 car", but for any other quantity we would write "n cars".

This distinction between singular and plural is very common, but other languages have other distinctions types.

The full set supported by Android is zero, one, two, few, many, and other.

Don't be misled either by the fact that, say, three sounds like it could only apply to the quantity 3: a language may require that 3, 13, 103 (and so on) are all treated like one another but differently to other quantities.

The selection of which string to use is made solely based on grammatical necessity.

In English, a string for zero will be ignored even if the quantity is 0, because 0 isn't grammatically different from 2, or any other number except 1 ("zero cars", "one car", "two cars", and so on).

Please rely on your translator to know what distinctions their language actually insists upon.

File location:

res/values/filename.xml

The filename is arbitrary. The <plurals> element's name will be used as the resource ID.

Attributes:

name - attribute of the <plurals> tag. This will be used as resource ID

<item> - this can be a plural or a singular string. This value can also be a reference to other string resources. Must be one of the children of a <plurals> tag

Attributes:

quantity - this attribute of an <item> tag inside a <plurals> tag. Valid values are zero, one, two, few, many, and other.

Example:

We will use English and Romanian languages, because they have different number of accepted pluralisation forms:

English language has 2 pluralisation forms : one and other.

Romanian language has 3 pluralisation forms : one, few and other.

English XML file at res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>

  <plurals name="numberOfProducts">

    <item quantity="one">One product.</item>

    <item quantity="other">%d products.</item>

  </plurals>

</resources>

Romanian XML file at res/values-ro/strings.xml



<?xml version="1.0" encoding="utf-8"?>

<resources>

  <plurals name="numberOfProducts">

    <item quantity="one">Un produs.</item>

    <item quantity="few">%d produse.</item>

    <item quantity="other">%d de produse.</item>

  </plurals>

</resources>

Translating Android String Resources

As mentioned above, there are three types of string resources recognized by Android:

  • String
  • String Array
  • Quantity Strings (Plurals)
Take the following file, which can be passed to Passolo as source:


<?xml version="1.0" encoding="utf-8"?>

<resources>

  <!-- Example of simple string -->

  <string name="hello">Greetings !</string>

 

  <!-- Example of a string array -->

  <string-array name="array">

    <item>First</item>

    <item>Second</item>

    <item>Third</item>

  </string-array>

 

  <!-- Example of plural string -->

  <plurals name="numberOfProducts">

    <item quantity="one">One product.</item>

    <item quantity="other">%d products.</item>

  </plurals>

</resources>

The file contains string, string array and plurals (all types of string resources supported by Android.)

Before parsing <plurals> tags, follow these steps:

  1. Go to Files > Options.
  2. Select View from the left side.
  3. For the Display empty strings in translation list option, select Always from the drop-down.
  4. Click OK.

You can now create a Passolo project and add the XML file to it. Make sure to select Add-in Android Resource File Parser.

To follow the above plurals example

  1. Select Language of text: English (United States) and select Romanian as target language.
  2. Create string lists for both English (United States) and Romanian.
  3. Open and translate:

Now, you will have to generate the target file. It will look like the following.



<?xml version="1.0" encoding="utf-8"?>

<resources>

  <!-- Example of simple string -->

  <string name="hello">Salutare !</string>

  <!-- Example of a string array -->

  <string-array name="array">

    <item>Primul</item>

    <item>Al doilea</item>

    <item>Al treilea</item>

  </string-array>

  <!-- Example of plural string -->

  <plurals name="numberOfProducts">

    <item quantity="one">Un produs.</item>

    <item quantity="few">%d produse.</item>

    <item quantity="other">%d de produse.</item>

  </plurals>

</resources>