强制 Grails 使用 message.properties 和正确的语言代码?
Force Grails to use message.properties with correct Language Code?
我用 message.properties
代替 i18n。我有以下 属性 个文件。
message.properties
message_de.properties
当我这样做时效果很好:
Locale locale = new Locale("de")
def test = g.message(code:'some.code', locale: locale)
问题是当我像这样使用 Locale
时:
Locale locale = new Locale("de_DE")
在前一种情况下,Grails 搜索不存在的 属性 文件 message_de_DE.properties
,因此它回退到 message.properties
.
如何强制 Grails 执行以下操作?
- 检查是否有
properties
文件与请求的区域设置的语言代码和国家/地区代码相匹配。
- 检查是否有
properties
文件匹配请求的语言代码
- 如果 1. 和 2. 失败回退到
message.properties
.
刚刚改变
Locale locale = new Locale("de_DE")
至
Locale locale = new Locale("de", "DE")
应该达到你想要的。
AFAIK Grails 默认执行您在要点中提到的内容,无需强制。
Locale
的单参数构造函数期望参数是语言名称,但 de_DE 不是有效的语言名称(这可能是grails 退回到 message.properties
的原因)。您必须使用构造函数的另一个变体,它将语言名称作为第一个参数,将国家/地区名称作为第二个参数。 doc。
我用 message.properties
代替 i18n。我有以下 属性 个文件。
message.properties
message_de.properties
当我这样做时效果很好:
Locale locale = new Locale("de")
def test = g.message(code:'some.code', locale: locale)
问题是当我像这样使用 Locale
时:
Locale locale = new Locale("de_DE")
在前一种情况下,Grails 搜索不存在的 属性 文件 message_de_DE.properties
,因此它回退到 message.properties
.
如何强制 Grails 执行以下操作?
- 检查是否有
properties
文件与请求的区域设置的语言代码和国家/地区代码相匹配。 - 检查是否有
properties
文件匹配请求的语言代码 - 如果 1. 和 2. 失败回退到
message.properties
.
刚刚改变
Locale locale = new Locale("de_DE")
至
Locale locale = new Locale("de", "DE")
应该达到你想要的。
AFAIK Grails 默认执行您在要点中提到的内容,无需强制。
Locale
的单参数构造函数期望参数是语言名称,但 de_DE 不是有效的语言名称(这可能是grails 退回到 message.properties
的原因)。您必须使用构造函数的另一个变体,它将语言名称作为第一个参数,将国家/地区名称作为第二个参数。 doc。