Java – how to handle internationalization “your input” XYZ is very good! “
I want to know what is the right way to handle internationalized statements and add runtime data for example
Double quotes are user data obtained or calculated at run time My platform is mainly Java / Android The correct solution of western language is weaker than the general method
Solution
Java
To retrieve localized text (messages), use Java util. ResourceBundle API. To format the message, use Java text. MessageFormat API.
Basically, first create a properties file, like this:
key1 = Your input {0} is excellent! key2 = You were {0} old when you switched from {1} to {2} shows.
{n} The thing is that you can pass the placeholder of the parameter of messageformat #format ()
Then load it like this:
ResourceBundle bundle = ResourceBundle.getBundle("filename",Locale.ENGLISH);
Then get the message through the key and do the following:
String key1 = bundle.getString("key1"); String key2 = bundle.getString("key2");
Then format and do the following:
String formattedKey1 = messageformat.format(key1,"xyz"); String formattedKey2 = messageformat.format(key2,"4 years","Barney and Friends","Spongebob");
You can also see:
> Trail: internationalization
Android
For Android, this process is relatively simple You just put all these string messages in RES / values / strings XML file You can then version the file in a different language and place it in the values folder that contains the language code For example, if you want to add Spanish support, you just need to create a folder called RES / values ES / and put the Spanish version of strings Put it there Android will automatically decide which file to use according to the configuration of the phone
You can also see:
> Developer guide – Localization