Search code examples
androidstringlocalizationandroid-resources

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?


Solution

  • 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">