Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a parameter into a localized string in Android?

I need to put a dynamic piece of text into a localized string but it has to go into different locations depending on the language.

For example, I need two strings like:

English: The current time is HH:mm:ss Another Language: HH:mm:ss is the current time

I can think of one approach, which is to have a preDate and postDate localized string where postDate is empty in English and preDate is empty in Other. This will work but doesn't seem very elegant or scalable.

Is there a better way to do this?

like image 378
brianestey Avatar asked Mar 16 '12 02:03

brianestey


2 Answers

I use this in my string resource.

<string name="duration"><xliff:g id="minutes" example="42" >%s</xliff:g> mins <xliff:g id="seconds" example="28" >%s</xliff:g> secs</string>

Which prints xx mins yy secs when you call:

getString(R.string.duration, minutes, seconds);

In another language, you can translate it by changing the location of mins and secs. It should print, for example: mins xx secs yy

A schema should be include at header:

<resources xmlns:xliff="rn:oasis:names:tc:xliff:document:1.2">
like image 61
Calvin Avatar answered Oct 20 '22 11:10

Calvin


Correct android xml strings format is <string name="tst">string %1$s integer %2$d</string>

like image 44
Roman Truba Avatar answered Oct 20 '22 11:10

Roman Truba