This guide explains how to use Lara CLI with Android XML string resource files (.xml), commonly used in Android applications for internationalization.
Android XML files are the standard format for storing string resources in Android applications. Lara CLI uses the structure res/[locale]/strings.xml where [locale] is replaced with the locale code (e.g., res/en/strings.xml, res/es/strings.xml).
Lara CLI supports Android XML files that follow the standard Android string resource format, including:
- Simple string resources (
<string>) - Plural resources (
<plurals>) - String array resources (
<string-array>) - Non-translatable strings (
translatable="false")
To configure Android XML files in your lara.yaml:
files:
xml:
include:
- "res/[locale]/strings.xml"
- "app/src/main/res/[locale]/strings.xml"
exclude: []
lockedKeys: []
ignoredKeys: []Note: The configuration key is
xml(matching the file extension), but this parser is specifically designed for Android XML string resource files. Other XML formats are not supported.Important: Lara CLI uses
res/[locale]/strings.xmlstructure (e.g.,res/en/strings.xml,res/es/strings.xml), not the standard Androidvalues-[locale]structure. The[locale]placeholder is replaced directly with the locale code.
Android XML files can be configured using the [locale] placeholder pattern. Lara CLI uses a simplified structure where the locale code replaces [locale] directly in the path.
Use the standard structure with locale directories:
files:
xml:
include:
- "res/[locale]/strings.xml"
- "res/[locale]/errors.xml"This follows the structure:
res/
├── en/ (English)
│ ├── strings.xml
│ └── errors.xml
├── es/ (Spanish)
│ ├── strings.xml
│ └── errors.xml
├── fr/ (French)
│ ├── strings.xml
│ └── errors.xml
└── de/ (German)
├── strings.xml
└── errors.xml
You can also use a custom directory structure:
files:
xml:
include:
- "app/src/main/res/[locale]/strings.xml"
- "translations/[locale]/strings.xml"Lara CLI expects Android XML files to follow this structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
<string name="hello">Hello World</string>
</resources>The parser extracts string resources from the <resources> element and processes them for translation.
Basic string resources use the <string> element:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
<string name="welcome_message">Welcome to our application</string>
<string name="button_submit">Submit</string>
<string name="button_cancel">Cancel</string>
</resources>Plural resources handle singular and plural forms for different quantities:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="item_count">
<item quantity="zero">No items</item>
<item quantity="one">%d item</item>
<item quantity="two">%d items</item>
<item quantity="few">%d items</item>
<item quantity="many">%d items</item>
<item quantity="other">%d items</item>
</plurals>
</resources>Supported quantity values:
zero- Zero itemsone- One itemtwo- Two itemsfew- Few itemsmany- Many itemsother- Other (default/fallback)
String array resources contain an ordered list of string values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
</string-array>
<string-array name="sizes">
<item>Small</item>
<item>Medium</item>
<item>Large</item>
</string-array>
</resources>String arrays are mapped with index-based keys: colors/0, colors/1, colors/2, etc.
Strings marked with translatable="false" are preserved but not translated:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name" translatable="false">My App</string>
<string name="hello">Hello World</string>
</resources>You can combine strings, plurals, and string arrays in the same file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My App</string>
<string name="welcome">Welcome</string>
<plurals name="item_count">
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
</plurals>
<string-array name="colors">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
</string-array>
<string name="goodbye">Goodbye</string>
</resources>Android XML supports XML entities for special characters:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="message">Hello & Welcome</string>
<string name="quoted">Say "Hello"</string>
<string name="less_than">A < B</string>
<string name="greater_than">C > D</string>
<string name="apostrophe">Don't worry</string>
</resources>Lara CLI automatically handles XML entity encoding and decoding.
Lara CLI automatically:
- ✅ Extracts string resources from
<resources>elements - ✅ Preserves the original XML structure and formatting
- ✅ Handles plural resources with quantity-specific keys
- ✅ Handles string array resources with index-based keys
- ✅ Maintains resource order from the original file
- ✅ Preserves non-translatable strings (
translatable="false") - ✅ Handles XML entity encoding/decoding
- ✅ Preserves XML declaration and encoding
When using lockedKeys or ignoredKeys with Android XML files, use the resource name format:
Simple strings:
- Use the string name directly:
app_name,hello,welcome_message
Plural resources:
- Use the format
plural_name/quantity:item_count/one,item_count/other
String array resources:
- Use the format
array_name/index:colors/0,colors/1,colors/2
files:
xml:
include:
- "res/[locale]/strings.xml"
lockedKeys:
- "app_name" # Specific string resource
- "item_count/one" # Specific plural quantity
- "colors/0" # Specific string array item
- "api_*" # Wildcard pattern
ignoredKeys:
- "debug_*" # Ignore all debug keys
- "test_*" # Ignore all test keysLara CLI preserves the original order of resources in your XML file. Resources are sorted and written back in the same order they appeared in the source file.
Here's a complete configuration example for an Android project:
version: "1.0.0"
project:
instruction: "Mobile application, user-friendly tone"
locales:
source: en
target:
- es
- fr
- de
- it
memories:
- mem_abc123
glossaries:
- gls_xyz789
files:
xml:
include:
- "res/[locale]/strings.xml"
- "res/[locale]/errors.xml"
exclude:
- "res/[locale]/test-*.xml"
lockedKeys:
- "app_name"
- "app_version"
- "api_*"
ignoredKeys:
- "debug_*"
- "test_*"
fileInstructions:
- path: "res/[locale]/strings.xml"
instruction: "User-facing messages, friendly and professional tone"
- path: "res/[locale]/errors.xml"
instruction: "Error messages should be clear and helpful"If you already have Android XML files with translations:
- Run
lara-cli initto create your configuration - Ensure your file paths match the
includepatterns (use[locale]placeholder) - Run
lara-cli translate - Continue developing - Lara CLI tracks changes via checksums and only translates what's new or modified
If your Android XML files don't exist yet:
- Create your source locale file (e.g.,
res/en/strings.xml) - Run
lara-cli translate- Lara CLI will create locale-specific files automatically - The files will be created with the appropriate structure
- ✅ Must contain
<resources>root element - ✅ Must use valid XML syntax
- ✅ Supports
<string>,<plurals>, and<string-array>elements - ✅ Supports
translatableattribute - ❌ Does not support other resource types (generic arrays, etc.)
- ❌ Does not support resource references (
@string/...)
- ✅ Standard structure:
res/[locale]/strings.xml - ✅ Custom directory structure:
translations/[locale]/strings.xml - ✅ Multiple files per locale:
res/[locale]/*.xml - ❌ Files without
[locale]placeholder are not supported - ❌ Single XML files (must be organized by locale)
- ❌ Standard Android
values-[locale]structure is not supported (use[locale]instead)
Supported:
<string>- Simple string resources<plurals>- Plural resources with quantity items<string-array>- String array resources with indexed items
Not Supported:
<array>- Generic arrays (non-string arrays)- Resource references (
@string/...,@color/..., etc.) - Other Android resource types
If Lara CLI needs to create a new Android XML file and cannot find an existing one, it will use this fallback:
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>If Lara CLI cannot find your resources:
- Ensure your file contains
<resources>root element - Check that the file path in
lara.yamlmatches your actual file location - Verify the
[locale]placeholder is correctly positioned - Ensure the XML syntax is valid
If plural resources aren't being processed correctly:
- Ensure each
<plurals>element has anameattribute - Verify each
<item>has aquantityattribute - Check that at least one quantity is specified
- Ensure the XML structure is valid
Lara CLI preserves resource order. If you notice changes:
- Ensure your source XML file is properly formatted
- The parser maintains the order from the original file
- Resources are sorted based on their appearance in the source file
If special characters aren't displaying correctly:
- Lara CLI automatically handles XML entity encoding/decoding
- Ensure your source file uses proper XML entities (
&,", etc.) - The parser converts entities during parsing and re-encodes during serialization
If non-translatable strings are being translated:
- Ensure
translatable="false"is set correctly - Check that the attribute value is exactly
"false"(case-sensitive) - Verify the attribute is on the correct element
When using lockedKeys or ignoredKeys:
Simple strings:
lockedKeys:
- "app_name" # Exact string name
- "button_*" # Wildcard patternPlural resources:
lockedKeys:
- "item_count/one" # Specific quantity
- "item_count/*" # All quantities for item_count
- "*/one" # All 'one' quantities- Supported Formats - Overview of all supported file formats
- Files Configuration - General file configuration options
- Instructions - How to use translation instructions
- Locales - Supported locale codes