在我当前的休息服务场景中,我需要能够向各个客户端提供本地化的错误消息。
想想像 void validate(Locale locale)
这样的方法,它在接收到一些请求时在参数对象上调用。
locale 信息可以从 http headers 中挤出。
当违规消息被插入时,我如何指示/配置 validator 使用某种语言?
据我所知, validator 通过调用 Locale.getDefault()
选择使用的 Locale。
到目前为止,我找不到除this 以外的任何内容。文章不完全符合我的需要(故事太长无法讲述)。
提前致谢!
最佳答案
根据spec你有两种可能性:
使用interpolate(String, Context, Locale)
或者
实现 custom message interpolator .
A custom message interpolator may be provided (e.g., to interpolate contextual data, or to adjust the default Locale used).
另见:
关于java - Bean 验证 : How to localize messages programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119545/