如何使用 Grails Localization 插件重新加载 messages.properties 中的消息更新?

How can I reload the message updates in the messages.properties using Grails Localization plugin?

在 Grails Localization 插件 documentation 中,它说:

If you distribute your finished application in the form of a war file, then unless the target application server unzips the war file on installation, the localizations plugin will be unable to automatically load (or subsequently 'import') the properties files from within the war file.

为了使用 war 重新加载所有消息更新,我在 BootStrap.groovy.

中使用了以下代码
import org.grails.plugins.localization.Localization

class BootStrap {
    def init = { servletContext ->
        Localization.reload()
    }
}

这段代码对我有帮助,只要应用程序部署到 Tomcat 服务器,消息更新就会应用。但是随着部署的版本越来越多,本地化 table 的 ID 号会自动增加。

所以我在我的代码中尝试 "truncate table" 来解决这个问题。在 BootStrap.groovy:

import org.grails.plugins.localization.Localization

class BootStrap {
    def sessionFactory
    def init = { servletContext ->
        sessionFactory.getCurrentSession().createSQLQuery('truncate table localization').executeUpdate()
        Localization.load()
    }
}

使用此代码导致 run-app 失败,因为有时 truncate table 发生在加载过程的中间,而不是之前!

您是否尝试将其作为线程执行并强制等待该进程结束?

本地化插件有一个 LocalizationsBootStrap.groovy。在 BootStrap.groovy 中编写代码会导致两个 BootStrap 文件之间的竞争。

我们可以在 BootStrap.groovy不做任何事情,但是 通过在 LocalizationsBootStrap.groovy 下创建 覆盖 LocalizationsBootStrap.groovy conf:

import org.grails.plugins.localization.*
class LocalizationsBootStrap {
    def sessionFactory
    def init = { servletContext ->
        sessionFactory.getCurrentSession().createSQLQuery('truncate table localization').executeUpdate()
        Localization.load()
    }
}