{"id":36825,"date":"2016-06-28T09:21:44","date_gmt":"2016-06-28T03:51:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36825"},"modified":"2016-06-28T10:46:53","modified_gmt":"2016-06-28T05:16:53","slug":"javascript-based-internationalization-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/javascript-based-internationalization-in-grails\/","title":{"rendered":"JavaScript based internationalization in Grails"},"content":{"rendered":"<p>Generally Internationalization is used for displaying HTML text \u00a0based on user\u2019s locale or\u00a0region. This is often done on server side of the application.<\/p>\n<p>Of late a lot of applications do the HTML generation on the client side, rather than on the server side. And to support the internationalization on client side is a common problem in this scenario.<\/p>\n<p>Currently there are 2 ways to achieve internationalisation on client side:<\/p>\n<p>1.Pass the <a href=\"http:\/\/www.tothenew.com\/blog\/i18n-implementation-using-sightly\/\">i18n messages from controller<\/a> to client side in response<\/p>\n<p>2.Internationalize the required messages on the client side instead of server side<\/p>\n<p>Both of them are not easy. But, out of the two, I would personally recommend the second option as passing the i18n messages every time in the response is not a good practice.<\/p>\n<p>However, if we are <a title=\"grails development services\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">working on <strong> <span style=\"font-weight: 400;\">Grails<\/span><\/strong>\u00a0framework<\/a>, the second option is fairly easy to achieve due to its i18n-asset-pipeline plugin.<\/p>\n<p>i18n-asset-pipeline plugin is an asset pipeline plugin, that generates a JavaScript file with internationalized texts, which can be used for client side i18n.<\/p>\n<p>To use this plugin, add the following code to your BuildConfig-<\/p>\n<p>[java]<\/p>\n<p>plugins {<\/p>\n<p>compile &quot;:i18n-asset-pipeline:1.0.6&quot;<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>For defining internationalised messages we have to create a special file (per locale) with &#8216;i18n&#8217; extension, eg. messages_fr.i18n, where &#8216;_fr&#8217; represents french locale and &#8216;messages&#8217; is the base name of the file that is common amongst all files. These files contains list of local messages keys you would like to use on the client-side.<\/p>\n<p>Now, to load the messages file for specific locale, all you have to do is use the following tag in your code:<\/p>\n<p>[java]<\/p>\n<p>&lt;asset:i18n locale=&quot;en_UK&quot; \/&gt;<br \/>\nOR<br \/>\n&lt;asset:i18n locale=&quot;${locale}&quot; name=\u201dtext\u201d\/&gt;<br \/>\n[\/java]<\/p>\n<p>where locale is a string or locale object and name is the base name of the file you want to load. In first case the \u201cmessages_uk.i18n\u201d file will be loaded and in second case, if the locale object is of France then \u201ctext_fr.i18n\u201d file will be loaded.<\/p>\n<p>After the file is loaded you can show the locale specific message by using following code:<\/p>\n<p>[java]$L(&quot;user.name.label&quot;)[\/java]<\/p>\n<p>assuming, user.name.label is one of the keys defined in your locale file.<\/p>\n<p><span style=\"font-weight: 400;\">The beauty of this plugin is that it creates a JS file similar to messages.properties and places all the keys and their values, that it had pick from correspoding message.properties file, in it. And since the output file is .js type, therefore now you can use the locale specific messages in your javascript code without worrying about where to get them from.<\/span><\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><span style=\"font-weight: 400;\">For further details you can use <a href=\"https:\/\/github.com\/aarifIg\/i18n-demo\">my sample project<\/a><\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><strong><span style=\"font-weight: 400;\">Hoping this information was helpful. Happy Internationalisation!<\/span><\/strong><\/p>\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generally Internationalization is used for displaying HTML text \u00a0based on user\u2019s locale or\u00a0region. This is often done on server side of the application. Of late a lot of applications do the HTML generation on the client side, rather than on the server side. And to support the internationalization on client side is a common problem [&hellip;]<\/p>\n","protected":false},"author":256,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[4840,3640,3636,3638,3639,3637,3631,3630],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36825"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/256"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36825"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36825\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}