Android Resources file (.xml)

 

Android Resources (.xml) is a file format used for storing localization data in Android apps. The file is based on XML, which makes it easy to read and modify.

Android Resources file example

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <string name="app_name">Demo app</string>

  <!-- Multiline intro message -->
  <string name="app_intro">Welcome to our app,\nwhere technology and innovation meet creativity and design.</string>

  <string name="common_back">Back</string>

  <string name="common_next">Next</string>

  <string name="common_welcome">Welcome!</string>

  <string name="common_welcome_name">Welcome, %s!</string>
  
  <string name="common_welcome_fullname">Welcome, %1$s %2$s!</string>

  <!-- List of devices -->
  <string-array name="common_devices">
    <item>Mobile</item>
    <item>Tablet</item>
    <item>Desktop</item>
  </string-array>

  <!-- Different languages may require different plural forms -->
  <plurals name="common_selected_items">
    <item quantity="one">%d item selected</item>
    <item quantity="other">%d items selected</item>
  </plurals>
</resources>

Placeholder

Placeholders allow you to dynamically insert values into translated messages. The syntax for placeholders in Android Resources files is simple. Placeholders are represented by the %s symbol, where the s can be replaced with other letters to represent different data types. For example, %d represents a decimal integer, %f represents a floating-point number, and similar.

Positional placeholders (e.g., %1$s, %2$d, and similar), unlike non-positional placeholders, are identified by their index position. They are particularly useful when a message has multiple placeholders, and when the order of the placeholders may vary in different languages.

<string name="common_greeting_name">Hi, %s!</string>
  
<string name="common_gretting_fullname">Hi, %1$s %2$s!</string>

Plural

Pluralization is an important concept in localization that enables you to handle the various grammatical forms of words and phrases based on the quantity of the item being referred to. By using the correct pluralization rules in your localization files, you can ensure that your app's messages are grammatically correct and appropriate for the language and culture of your users. Different languages have different pluralization rules, and it's important to define the correct plural forms for each language to ensure a professional and seamless user experience.

<plurals name="common_items">
  <item quantity="one">%d item</item>
  <item quantity="other">%d items</item>
</plurals>

Array

Using arrays in localization can be helpful when translating text that contains multiple values, such as lists or menus.

<string-array name="common_devices">
  <item>Mobile</item>
  <item>Tablet</item>
  <item>Desktop</item>
</string-array>

Comment

Comments can serve various purposes, such as explaining the intended use of a resource, providing additional context or information, or even serving as a reminder for future updates.

<!-- This is a comment -->

What are the most frequent errors in Android Resources files?

The most frequent errors in Android Resources files include incorrect syntax, missing or misspelled keys.

What characters need to be escaped in Android Resources files?

In Android Resources files, several characters need to be escaped to prevent errors and ensure proper handling. Some of them are: single (') and double (") quotes, less-than (<) and greater-than (>) signs, and ampersands (&).

Tired of manually editing translation files?

Our platform streamlines software localization for you.

Copyrights 2024 © Localizely