简体   繁体   中英

Is there a way to add variables into strings from string.properties file?

I was told to store all strings needed for the applications in the .properties (so that future app localizations are easier to maintain) file which I did Now I have to alter some of them so they contain some variables inside of them for example string "hey, you have a new message" should now be "hey, , you have a new message from " and the string should still be stored in the .properties file

What I have now:

  • notifications.properties file with strings stored like fcm.msg.new-message-body = "Hey, you have a new message"
  • Config class that initializes said strings so I can use them
@Configuration
@ConfigurationProperties(prefix = "fcm.msg")
@PropertySource("classpath:notification.properties")
class FCMConfig {
    lateinit var newMessageBody: String

here is the way-

store string data in a properties file

new-message-body = "Hey, you have a new message"

then where you want to use that string then just use this

@Value("${new-message-body}")
 private String message;

Then you can use the message variable anywhere in that class.

if you want do not want to create any variable then just

System.out.println(@Value("${new-message-body}"));

在 kotlin 中你可以这样做

var variable_name:String=getString(R.string.newMessageBody)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM