Grails:读取资源包

发布于 2025-01-01 01:24:49 字数 414 浏览 1 评论 0原文

我尝试从资源包获取消息/翻译列表,但失败(引发异常)。该应用程序正在 IDEA 的 Tomcat 上运行:

Locale locale = new Locale("en");
ResourceBundle bundle = ResourceBundle.getBundle('i18n/dictionary', locale);

这里出了什么问题。 i18n/dictionary 在类路径上。可能是“i18n/dictionary”是错误的。

我能够获取消息源,但无法从此(SPRING)对象获取密钥:

def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource");

I tried to get list of message/translations from resource bundle but it fails (throws exception). The app is running on Tomcat from IDEA:

Locale locale = new Locale("en");
ResourceBundle bundle = ResourceBundle.getBundle('i18n/dictionary', locale);

What is wrong here. i18n/dictionary is on class path. Could be 'i18n/dictionary' is wrong.

I'm able to get message source, but I can't get keys from this (SPRING) object:

def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么敢 2025-01-08 01:24:49

资源路径不完整。如果您需要前端的转换表,也许以下控制器可能有用:

class ClientMessagesController {

def index = {
    Locale locale = session.getAttribute('locale') ?: new Locale("en");
    ResourceBundle bundle = ResourceBundle.getBundle('\\grails-app\\i18n\\clientMessages', locale);

    def sep = '';
    def sb = new StringBuilder();
    sb.append('<script type="text/javascript">\n');
    sb.append('_i18n = {\n');
    bundle.getKeys().each {key ->
        sb.append(sep);
        sb.append(key.replace('.', '_'));
        sb.append(': "');
        sb.append(bundle.getString(key).replace('"', '"'));
        sb.append('"\n');
        sep = ',';
    }
    sb.append('};\n</script>\n')
    render(text: sb.toString());
}

}

The resource path was incomplete. If you need the translation table on the front-end, maybe the following controller could be useful:

class ClientMessagesController {

def index = {
    Locale locale = session.getAttribute('locale') ?: new Locale("en");
    ResourceBundle bundle = ResourceBundle.getBundle('\\grails-app\\i18n\\clientMessages', locale);

    def sep = '';
    def sb = new StringBuilder();
    sb.append('<script type="text/javascript">\n');
    sb.append('_i18n = {\n');
    bundle.getKeys().each {key ->
        sb.append(sep);
        sb.append(key.replace('.', '_'));
        sb.append(': "');
        sb.append(bundle.getString(key).replace('"', '"'));
        sb.append('"\n');
        sep = ',';
    }
    sb.append('};\n</script>\n')
    render(text: sb.toString());
}

}

猫卆 2025-01-08 01:24:49

这对我有用。

ResourceBundle resourceBundle = ResourceBundle.getBundle("grails-app.i18n.messages", locale)

this works for me.

ResourceBundle resourceBundle = ResourceBundle.getBundle("grails-app.i18n.messages", locale)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文