Grails: reading a resource package

I tried to get the message / translation list from the resource bundle, but it fails (throws an exception). The application runs on Tomcat from IDEA:

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

What is wrong here. i18n / dictionary is on the way to the class. Cannot be "i18n / dictionary".

I can get the message source, but I can’t get the keys to this object (SPRING):

 def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource"); 
+4
source share
1 answer

The path to the resource was incomplete. If you need a translation table on an interface, maybe the following controller might 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('"', '&quot;')); sb.append('"\n'); sep = ','; } sb.append('};\n</script>\n') render(text: sb.toString()); } 

}

+1
source

Source: https://habr.com/ru/post/1393871/


All Articles