1 回答
TA贡献1839条经验 获得超15个赞
请参阅https://github.com/jeffbrown/injecteermessagesource上的项目。
https://github.com/jeffbrown/injecteermessagesource/blob/beba0773d41cb475d8d837acc19d0dd41f5b4431/src/main/groovy/injecteermessagesource/CRUDController.groovy
package injecteermessagesource
import io.micronaut.context.MessageSource
import javax.inject.Inject
class CRUDController<T> {
protected final Class<T> clazz
@Inject
MessageSource messageSource
protected CRUDController( Class<T> clazz ) {
this.clazz = clazz
}
}
https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/groovy/injecteermessagesource/PersonController.groovy
package injecteermessagesource
import io.micronaut.context.MessageSource
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller('/people')
class PersonController extends CRUDController<Person> {
protected PersonController() {
super(Person)
}
@Get('/')
String index() {
def context = MessageSource.MessageContext.DEFAULT
def message = messageSource.getMessage('test', context)
message.get()
}
}
https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/groovy/injecteermessagesource/AppConfig.groovy
package injecteermessagesource
import io.micronaut.context.MessageSource
import io.micronaut.context.annotation.Bean
import io.micronaut.context.annotation.Factory
import io.micronaut.context.i18n.ResourceBundleMessageSource
@Factory
class AppConfig {
@Bean
MessageSource messageSource() {
new ResourceBundleMessageSource('my.somepackage.i18n.Messages')
}
}
https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/resources/my/somepackage/i18n/Messages.properties
test=FooBar
这一切似乎都有效:
$ curl localhost:8080/people
FooBar
添加回答
举报