maxifelix / langSelector

grails-lang-selector plugin
Other
0 stars 4 forks source link

Grails Language Selector Plugin

Build Status

This plugin provides a simple to use tag, to display flags for countries of the languages your application supports, that when clicked, changes the language of your application using the i18n support provided by Grails.

Flag icons 16x16 pixels are from FamFamFam

Usage

Add the next tag to your gsp.

<langs:selector langs="es, en, en_US, pt_BR, pt, pt_pt"/>
<langs:selector langs="es, en, en_US, pt_BR, pt, pt_pt"
       url="${createLink(action: 'list', controller: 'libro', params: [paramun: 123])}"/>
<langs:selector langs="es, en, en_US, pt_BR, pt, pt_pt" default="es" />

The required attribute langs tells the plugin which flags to show, if pay attention the values are the ISO 3166-1 alpha-2 code for languages and a countries, also are the same of the suffixes of messages.properties files.

Optionally if you want to redirect always to the same url when changing the language (this is helpful to avoid doing a GET with post data) use the url parameter, and this provided url will be used instead of the actual one.

From version 0.3 you optionally can set the default flag to be highlighted when the user enters for first time in the app or has a new fresh session.

This tag includes the css stylesheet that helps you identify which language is selected:

<langs:resources/>

Difference with <g:localeSelect/> tag

You can also use standard Grails . It shows a combo-box with all known locales and can't show a country flag. But it my might be useful if you don't want to use this plugin. Here is example:

<form method="get">
    <g:localeSelect name="lang" value="${Locale.ENGLISH}"/>
    <input type="submit" value="Change locale"/>
</form>

Configuration.

Optionally you can add this property to the Config.groovy to tell the plugin which flag display for the language. By default has the value shown below. Its a map that in the keys are the languages and the values are the countries.

grails.plugin.langSelector.langFlags = [
    'es': 'es',
    'en': 'gb',
    'fr': 'fr',
    'da': 'dk',
    'de': 'de',
    'it': 'it',
    'ja': 'jp',
    'nl': 'nl',
    'ru': 'ru',
    'th': 'th',
    'zh': 'cn',
    'pt': 'pt'
]

Tips & tricks

1 . The i18n support its executed in a filter attached to all controllers in your app, so in order to get it working in '/', you have to point it to a controller in the grails-app/conf/UrlMappings.groovy file.

Here is an example:

'/'(controller: 'oneController', action: 'oneAction')

2 . Some versions of Grails when create an app generate the english message properties without the corresponding suffix, this prevents the i18n support to work properly. You should change the name of the file to get it working, this way: messages.properties -> messages_en.properties

Plugin history

v0.4

<langs:resource/> tag is deprecated and will be removed in v1.0. You should use standard <r:require module="langSelector"/> instead:

<head>
    ...
    <r:require module="localeSelector"/>
</head>

-[#9] com.mfelix.grails.plugins.langSelector.lang.flags with grails.plugin.langSelector.langFlags