{"id":2984,"date":"2019-04-11T13:27:30","date_gmt":"2019-04-11T12:27:30","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=2984"},"modified":"2020-04-10T13:35:39","modified_gmt":"2020-04-10T12:35:39","slug":"internalisation-of-your-grails-based-application-with-i18n-asset-pipeline-plugin","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2019\/04\/11\/internalisation-of-your-grails-based-application-with-i18n-asset-pipeline-plugin\/","title":{"rendered":"Internalisation of your Grails based application with i18n-asset-pipeline plugin"},"content":{"rendered":"<div class=\"post-text\">\n<p>What I will present below about <code>i18n-asset-pipeline<\/code> grails plugin which is doing the same job as <code>jquery.i18n.properties.js<\/code> as such. You probably find a few implementations of <code>jquery.i18n.properties.js<\/code> on <a href=\"https:\/\/grails.org\/plugins\" rel=\"nofollow noreferrer\">grails plugin portal<\/a>. I&#8217;m not entirely sure you will be happy with my suggestion. However, I would say that using this plugin will help us minimise the effort to maintain and call tag lib <code>g:message<\/code> in <strong>both grails views and external javascript files<\/strong>.<\/p>\n<p>If you feel confident to play with <code>i18n-asset-pipeline<\/code>, please take a closer look at <a href=\"https:\/\/plugins.grails.org\/plugin\/amc-world\/i18n-asset-pipeline\" rel=\"nofollow noreferrer\">its documentation<\/a>. Below are the summarised baby steps I have been doing with <code>grails 2.5.6<\/code>.<\/p>\n<p><strong>Step 1: Declare the plugin in<\/strong> <code>BuildConfig.groovy<\/code><\/p>\n<pre><code>plugins {\r\n   ...\r\n   runtime 'org.grails.plugins:i18n-asset-pipeline:1.0.6'\r\n   ...\r\n}\r\n<\/code><\/pre>\n<p><strong>Step 2: Define your messages properties in the appropriate files<\/strong><\/p>\n<p>In Resource Bundle messages under <code>grails-app\/i18n<\/code> directory, you add your languages of interest files, such as <code>messages_fr.properties<\/code>, <code>messages_vi.properties<\/code>, etc. To demonstrate how this works, I have <code>welcome.message<\/code> just display a welcome message on the index page in these files <code>messages_{fr,es,vi}.properties<\/code>.<\/p>\n<p><strong>Step 3: Define<\/strong> <code>i18n<\/code> files in <code>assets\/javascripts\/i18n<\/code> <strong>directory<\/strong><\/p>\n<p>After starting the application first, you will see this directory. Creating the same set of files, i.e.,<code>messages.i18n<\/code><code>messages_{es,fr,vi}.i18n<\/code>. Inside these files, we just put the properties that were added in <code>messages_{es,fr,vi}.properties<\/code> files or the properties you want to display in only specific languages.<\/p>\n<p><strong>Step 4: Set the default language (optional)<\/strong><\/p>\n<p>Leaving the following snippet in <code>grails-app\/conf\/spring\/resources.groovy<\/code> file<\/p>\n<pre><code>beans = {\r\n   localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {\r\n      defaultLocale = new Locale(\"fr\", \"FR\")\r\n      java.util.Locale.setDefault(defaultLocale)\r\n   }\r\n}\r\n<\/code><\/pre>\n<p><strong>Step 5: How to use i18n plugin<\/strong><\/p>\n<p>In views, we can load i18n language default setting and display message properties something along those lines.<\/p>\n<pre><code>&lt;%@ page contentType=\"text\/html;charset=UTF-8\" %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Demo | BioModels&lt;\/title&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n    &lt;meta name=\"layout\" content=\"main\"\/&gt;\r\n    &lt;script&gt;\r\n\r\n    &lt;\/script&gt;\r\n    &lt;g:set var=\"locale\" value=\"${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'}\"\/&gt;\r\n    &lt;asset:i18n locale=\"${locale}\"\/&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n&lt;h2&gt;&lt;g:message code=\"welcome.message\"\/&gt; &lt;\/h2&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p><strong>Step 6: Testing<\/strong><\/p>\n<p>Open a tab in your web browser, retrieving your page, <code>http:\/\/localhost:8900\/i18ndemo\/demo\/<\/code>, for instance, it will render the content of <code>welcome.message<\/code> property in the default language which was set up in <code>resources.groovy<\/code>. If you want to switch to other languages straightaway, type <code>?lang=es<\/code> after the previous URL. That looks like <code>http:\/\/localhost:8900\/i18ndemo\/demo\/?lang=es<\/code><\/p>\n<p>After getting through those steps, I believe you will obtain your need.<\/p>\n<p>The sample project has been also pushed to bitbucket found <a href=\"https:\/\/bitbucket.org\/itersdesktop\/i18ndemo\/src\" rel=\"nofollow noreferrer\">here<\/a>.<\/p>\n<p>I hope this answer would help you solve your problem.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What I will present below about i18n-asset-pipeline grails plugin which is doing the same job as jquery.i18n.properties.js as such. You probably find a few implementations of jquery.i18n.properties.js on grails plugin&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[593,4,222],"tags":[659,657,658],"class_list":["post-2984","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-grails","category-web-engineering","category-web-programming","tag-i18n","tag-internalisation","tag-multiple-languages"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=2984"}],"version-history":[{"count":1,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2984\/revisions"}],"predecessor-version":[{"id":2985,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2984\/revisions\/2985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/3008"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=2984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=2984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=2984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}