grails - 强制 Grails 使用带有正确语言代码的 message.properties?

标签 grails internationalization

我使用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 执行以下操作?

  1. 检查是否存在与所请求的区域设置的语言代码和国家/地区代码相匹配的 properties 文件。
  2. 检查是否存在与请求的语言代码匹配的 properties 文件
  3. 如果 1. 和 2. 失败,则返回到 message.properties

最佳答案

只是改变

    Locale locale = new Locale("de_DE")

    Locale locale = new Locale("de", "DE")

应该达到你想要的。

据我所知,Grails 默认情况下会执行您在观点中提到的操作,无需强制。

Locale 的单参数构造函数期望参数是一个语言名称,但是 de_DE 不是一个有效的语言名称(这可能是 grails 回落到message.properties)。您必须使用构造函数的其他变体,它将语言名称作为第一个参数,国家/地区名称作为第二个参数。 doc

关于grails - 强制 Grails 使用带有正确语言代码的 message.properties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833724/

相关文章:

ruby-on-rails - 自定义 Rails 验证消息(不显示属性名称)Rails 4

events - 服务级别的Grails域类事件监听器

Grails Spring Security 默认配置 : How to define default user and password

grails - Kerberos的域身份验证失败

java - 如何在 spring mvc 中动态切换语言?

Heroku 上的 Django : customizing django. po

ruby-on-rails - 在 I18n 消息中使用 a/an

tomcat - Grails 网络应用程序图像目录在 war 中无法正常工作

mysql - grails - 下载 PDF 时得到空文件

xcode - iOS - 使用基本本地化 Pane 始终为空