Search code examples
javaandroidandroid-studiointernationalizationtranslate

Translate String AndroidStudio


I want to translate this code:

List.add("  Temperature: " + matrix[1][2] + " ºC");

I want to translate the word "Temperature:" but I don`t know how to put it on Strings.xml

EDIT: I want to know how to add Temperature on Strings.xml and then make a translation


Solution

  • To put it on Strings.xml you have to add this :

    <string name="Temperature">Temperature: </string>
    

    Then if you want to use it from Strings.xml you do this as follows :

    List.add(getString(R.string.Temperature) + matrice[1][2] + " ºC");
    

    To translate it you'll have to create different values directories for the language with the suffix of the language code.

    See the documents

    EDIT(parameters String)

    Adding the parameters as @trooper said, would be like this :

    Your Strings.xml should be

    <string name="Temperature">Temperature: %1$s ºC</string>
    

    Then in your java code you have to use this :

    List.add(String.format(getString(R.string.Temperature), matrice[1][2]));