Thursday, May 13, 2010

Grails Acegi change locale after user login

In Acegi and Grails application, I would like to set the locale to the users preferred language after they log in. Here comes some steps I made:

In config file: conf/security.groovy. Add the following line:

security {
active = true
loginUserDomainClass = "User"
authorityDomainClass = "Role"
requestMapClass = "Requestmap"
security.defaultRole="ROLE_USER"

useSecurityEventListener = true
onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
// handle AuthenticationSuccessEvent
def autservice = appCtx.authenticateService
def domain = autservice.userDomain()
def request = org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder.getRequest()
def session = request.getSession(false)

// ok set session information for lang file - tested and ok
if (domain) {
def person = User.get(domain.id)
def userSetting = UserSetting.findByAuthor(person)
def lang = 'en'
if (session && userSetting && userSetting.language != null) {
session.lang = userSetting.language
}
}
}
}

And I have a table called userSetting which hold the user setting information, you need change it as your table information.

Ok, now we need call this function in any controller you want to change locale, maybe you'd better create a new general controller which will be extended by any controller:

// this is for locale change ...
def localeChangeCheck = {
def locale;
if (session.lang != null) {
locale = new Locale(session.lang)
RCU.getLocaleResolver(request).setLocale(request,response,locale)
}
if (locale == null) {
locale = RCU.getLocale(request)
}
}

Now you are on fly. To see running example, you can try @

www.feyasoft.com

Ok, do not forget to change properties file under your i18n folder.

Thursday, April 22, 2010

Quick add multiple language to extjs + grails application

Recently we quick added multiple language to our application. To see demo, you can login @ www.feyasoft.com/main

1: In database, we create a table which hold on the setting for language.

2: In grail MainController file, add the following code:

/**
* Copyright (c) 2005 - 2010 FeyaSoft Inc. All Rights Reserved.
*/
import grails.converters.*

class MainController extends GeneralController {

def index = {
// get login user id
User loginUser = getLoginUser()
if (loginUser == null) {
def json = [failure : "true", errorInfo : "Please login before post"]
render json as JSON
}

def item = UserSetting.findByAuthor(loginUser);
def lang = 'en'
if (item) lang = item.language;

render(view: 'main', model: [lang: lang])
}
}

3: In main.gsp file, the following code are added, really simple:

<~g:javascript library="extjs/src/locale/ext-lang-${lang}" /~>
<~g:javascript library="feyaSoft/lang/${lang}" /~>
And you need create the related lang file now. For example, we have this lang file under my folder: web-app\js\feyaSoft\lang\en.js

/**
* FeyaSoft Online MyActivity
* Copyright(c) 2006-2010, FeyaSoft Inc. All right reserved.
* info@feyasoft.com
*/
Ext.ns("feyaSoft");

feyaSoft.lang = {

'common':{
'accessories': 'Accessories',
'align': 'Align',
'average': 'Average',
'brush': 'Brush',
'calculate': 'Calculate',
....}
...
}

4: Now you can use something like this in your JS file:

feyaSoft.lang.common['average']
And you are on fly now.

Sunday, April 18, 2010

UTF-8 setting in Grails, mysql

Client UTF8----(A)---->
Server UTF8 ----(B)---->
Database UTF8 --- (C)---->
Table UTF8 ---(D)----> Row UTF8

1: Client UTF8

<~meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

2: Server UTF8

In DataSource.groovy

url = "jdbc:mysql://localhost:3306/home?tcpKeepAlive=true&useUnicode=true&characterEncoding=UTF8"
This forces the driver to recognize that all text being sent to the driver is in UTF8. This should conform directly with Java's default UTF8 encoding without any translations occurring.

3: Database UTF8

mysql> show variables like '%character%';

+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> set global character_set_server=utf8;
be sure to include the option in your my.cnf to ensure this option is persisted for a mysqld restart.

/etc/mysql/my.conf - default-character-set = utf8