3

Please see my previous question here for the full code.

In case I want to internationalize my app, I need to create string objects for some statements in my project. The one bothering me most is this code block.

    ...
    else
        Toast.makeText(MainActivity.this, 
            "Good job! The answer was " + comp + ".\n" +
            "You made " + guesses + " guesses.\n" +
            "Restart the app to try again.", 
            Toast.LENGTH_LONG).show();
    }
};

The relative part to this in the strings.xml looks like:

<string name="correct">Good job! The answer was + comp\n
               You made + guesses + guesses.\n
               Restart the app to try again.</string>

I want the comp and guesses variables to show their respective values, but I don't know how.

I plan to do this to the code block

    ...
    else
        Toast.makeText(MainActivity.this, 
            R.string.correct, 
            Toast.LENGTH_LONG).show();
    }
};

Thank you.

Community
  • 1
  • 1
jjkim
  • 417
  • 5
  • 12

5 Answers5

14

In your xml file string.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="correct">Good job! The answer was %1$s\n
               You made %2$s guesses.\n
               Restart the app to try again.</string>
</resources>

If you want to put an other value in the text, you increment %2$s -> %3$s , etc ...

Then you can use it like in java like that :

Toast.makeText(MainActivity.this, getString(R.string.correct, comp, guesses), 
            Toast.LENGTH_LONG).show();
Andros
  • 4,069
  • 1
  • 22
  • 30
  • This is definitely the best way to handle this, and I did not know about *that* `getString` method - I've been using `String.format(Locale, String, Object...)`. Nice! – Phil Jan 09 '14 at 17:52
1

Store a format string in the resource and use String.format to replace the comp and guesses placeholders. Use the appropriate places holders for each and include the variables as arguments to format(). You can find details of what placeholders to use under the Formatter class.

The resource would then be something like:

<string name="correct">Good job! The answer was %s \n
           You made %d guesses.\n
           Restart the app to try again.</string>

And the code would be:

...
else
    String correctFormat = getString(R.string.correct);
    Toast.makeText(MainActivity.this, 
        String.format(correctFormat, comp, guesses),
        Toast.LENGTH_LONG).show();
}

};

brader24
  • 485
  • 2
  • 10
0

you would use

context.getResources().getString(r.id.correct);

then create a values folder for each language you want

tyczj
  • 71,600
  • 54
  • 194
  • 296
0

As @Andros said,

In your string.xml

<string name="correct">Good job! The answer was %1$s\n
           You made %2$s guesses.\n
           Restart the app to try again.</string>

In your code,

Resources res = getResources();
Toast.makeText(MainActivity.this, String.format(res.getString(R.string.correct),comp, guesses),Toast.LENGTH_LONG).show();
Srikanth R
  • 410
  • 5
  • 17
  • You don't need to get the Resource because getString() is directly available from a context. And you dont have to use String.format() because with getString(int, Object ...), you can do it directly. – pdegand59 Jan 09 '14 at 17:56
  • Yeah, you are right. It's just a generalized answer. It works even for, say nested classes too. – Srikanth R Jan 09 '14 at 17:57
0

In string.xml

<string name="correct" formatted="false">Good job! The answer was %s\n
               You made %s guesses.\n
               Restart the app to try again.</string>

In java :

Toast.makeText(MainActivity.this,String.format(context.getString(R.string.correct),comp,guesses), Toast.LENGTH_LONG).show();
AndRSoid
  • 1,777
  • 2
  • 13
  • 25