How to use Android xliff:g

Minor typo in your example, there should be a closing tag:

<string name="order_quantity">Cantidad: <xliff:g id="quantity" example="2">%d</xliff:g></string>

The id attribute is just used to identify what the substitution parameter represents (in your case, it represents the quantity). It's as you said, a note, and not actually used programmatically.

That said, in Android Studio, if you have Code Folding enabled for strings, it will substitute in the ID when it shows the collapsed string. You'd see something like this:

// This...
mTextView.setText(getString(R.string.order_quantity, 2));

// Will show as this when folded:
mTextView.setText("Cantidad: {quantity}");

As for your second question, why not just use string concatenation? In other languages, the substitution may not go at the end of the string. You could have something like:

values/strings.xml
    <string name="order_quantity">%d items</string>

values-es/strings.xml
    <string name="order_quantity">Cantidad: %d</string>

So you can see that in this case, simply appending the strings together would not give you a valid result.

Tags:

Android

Xliff