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