gpt4 book ai didi

grails - inList中的国际化

转载 作者:行者123 更新时间:2023-12-02 15:56:39 25 4
gpt4 key购买 nike

我找到了this,它对我想做的事情非常有用,但是有什么方法可以使 inList 中的内容遵循。Grails国际化是

谢谢

最佳答案

我找到了workaround,我认为这可能会有所帮助..

1. toString()

As you know, the scaffolded screens just output values in selects and dropdowns through the toString() method of the shown object. We can construct each enum with a hardcoded translation and have toString() return that value.


 class Product {

enum Status {

AVAILABLE("Beschikbaar"), SOLD_OUT("Uitverkocht")

final String value
Status(String value) {
this.value = value
}

String toString() {
value
}
}

String name
Status status

static constraints = { name blank: false, unique: true }
}

The actual names of the enum now only appear in the generated HTML:


 <select name="status" required="" id="status" >
<option value="AVAILABLE" >Beschikbaar</option>
<option value="SOLD_OUT" >Uitverkocht</option>
</select>

2. MessageSourceResolvable

By now you’ll probably understand above hardcoded solution works for just one language, one hardcoded in the enum itself – which will cause problems when in a month from now your application actually needs to support a 2nd language :-) So how do we leverage the fact that we have already have message_XX.properties where we’ve put our other message keys? Use the underlying Spring framework. Have our enum implement org.springframework.context.MessageSourceResolvable e.g. like this:


enum Status implements org.springframework.context.MessageSourceResolvable {

AVAILABLE, SOLD_OUT

public Object[] getArguments() { [] as Object[] }

public String[] getCodes() { [ name() ] }

public String getDefaultMessage() { "?-" + name() }
}

Now we can provide a value in our messages_nl.properties for each enum we have:


product.label=Product
product.name.label=Naam
product.status.label=Status
AVAILABLE=Beschikbaar
SOLD_OUT=Uitverkocht

关于grails - inList中的国际化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com