在grails中向i18n国际化文件引入常量字符串的好方法
在 grails 中,i18n 文件中有没有办法引用常量值。我正在尝试做这样的事情:
constant.sitename=Fancy SiteName
intro.headline.label=Welcome to {constant.sitename}
home.headline=You're at {constant.sitename}
原因是如果我们决定更改名称,我不想更改每个字符串中的站点名称,我只想更改一次。有没有办法实现这个要求呢?
我意识到我也可以在 Config.groovy 中设置一个常量,但这需要在每条需要它的消息上传递参数,我不想这样做,因为这会让开发人员的生活变得更糟。
In grails, is there a way in the i18n file to reference a constant value. I'm trying to do something like this:
constant.sitename=Fancy SiteName
intro.headline.label=Welcome to {constant.sitename}
home.headline=You're at {constant.sitename}
The reason is I don't want to change the sitename in every single string if we decide to change the name, I only want to do it once. Is there a way to accomplish this requirement?
I realize I could also set a constant in the Config.groovy, but then that would require passing in the param on every single message that required it, which I'd rather not have to do this as it would make developers lives worse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 resources.groovy 中的自定义实现来覆盖 Grails 的标准 messageSource bean。默认情况下,Grails i18n 插件使用 PluginAwareResourceBundleMessageSource 来实现此目的。
因此,对其进行子类化并重写必要的代码以添加属性替换。心里不知道这些具体方法是什么,但是用调试器应该很容易弄清楚。
You might override Grails' standard messageSource bean by a custom implementation in resources.groovy. By default the Grails i18n plugin uses PluginAwareResourceBundleMessageSource for this.
So, subclass this and override the necessary code to add property replacement. Don't know by heart what exact methods these are, but that should be easy to figure out with a debugger.