Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Android xliff:g

Tags:

Can someone please explain the xliff:g for strings/localization.

I understand that xliff:g is not supposed to translate anything inside the <> things, but I'm confused how exactly I'd use this in code.

An example I have in my case is the practice spanish translations that I have has:

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

I am now trying to get localized strings with xliff:g to work. What is id here and what does it do? And what does it call?

Also what is the %d and what does it do? What is the point of example? Also, how would I call that into code, if at all?

Why can't someone just do the following code to insert the following xml:

<string name="quant">Quantity: </string> 

into java like so:

getString(R.string.quant) + quantity 

so that way it concactenates the quantity variable into the getString?

like image 784
Tal C Avatar asked Jan 25 '16 23:01

Tal C


People also ask

What is Xliff in Android?

If you're looking to localize your software to different languages, you may have run across the term 'XLIFF file'. XLIFF (short for XML Localization Interchange File Format) is a translation industry standard for exchanging localized strings between applications.

How localization works in Android?

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.

In which folder can you find the string resource file strings XML?

You can find strings. xml file inside res folder under values as shown below.


1 Answers

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.

like image 101
Kevin Coppock Avatar answered Oct 21 '22 08:10

Kevin Coppock