繁体   English   中英

有没有办法将变量从 string.properties 文件添加到字符串中?

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

我被告知将应用程序所需的所有字符串存储在 .properties(以便将来的应用程序本地化更容易维护)文件中,我这样做了现在我必须更改其中的一些,以便它们在其中包含一些变量,例如字符串“嘿,你有一条新消息”现在应该是“嘿,你有一条来自”的新消息,并且该字符串仍应存储在 .properties 文件中

我现在所拥有的:

  • Notification.properties 文件中的字符串存储如下 fcm.msg.new-message-body = "嘿,你有一条新消息"
  • 初始化所述字符串的配置类,以便我可以使用它们
@Configuration
@ConfigurationProperties(prefix = "fcm.msg")
@PropertySource("classpath:notification.properties")
class FCMConfig {
    lateinit var newMessageBody: String

这是方法-

将字符串数据存储在属性文件中

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

那么你想在哪里使用那个字符串然后就用这个

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

然后您可以在该类中的任何位置使用 message 变量。

如果你不想创建任何变量,那么就

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

在 kotlin 中你可以这样做

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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