Friday, February 5, 2016

Different language message error from Model Cakephp


That is just a small trick how how we can make our website to be multiple language. For the concept is not different from other
framework, by the way you have understand about folder structure of Cakephp.

In this case you will see the Local folder which its contain the two folder inside.

I have created two folder of language "en and jp".

1- Create language folder. ex. ('en and jp')



2- Create file with extension ".po"
3- There are two key word in the file which you have to understand :
    msgid : it is the key or id which represent the message as a global
    msgstr: it is the message of the key a head
4- Use in model. ex.
public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notBlank'),
                'message' => 'This field is required!'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notBlank'),
                'message' => 'This field is required!',
            ),
            'min_length' => array(
                'rule' => array('minLength', '5'),
                'message' => 'Password must over 5',
            )
        ),
);

5- check in the file .po syntax
    a- Example in en folder :

msgid "This field is required!"
msgstr "This field is required!"

msgid "Please enter the same value again!"
msgstr "Please enter the same value again!"

msgid "Password must over 5!"
msgstr "Password must over 5!"

msgid "Email already exist!"
msgstr "Email already exist!"

msgid "Please enter valid phone number!"
msgstr "Please enter valid phone number!"

msgid "This field must be number!"
msgstr "This field must be number!"

msgid "Invalid email format!"
msgstr "Invalid email format!"

   b- Example in jp folder :
msgid "This field is required!"
msgstr "必須項目です。"

msgid "Please enter the same value again!"
msgstr "必須項目です。"

msgid "Password must over 5!"
msgstr "パスワードは5文字以上を入力ください。"

msgid "Email already exist!"
msgstr "入力されたEメールは既に存在しています。"

msgid "Please enter valid phone number!"
msgstr "電話の形式ではありません。"

msgid "This field must be number!"
msgstr "数値ではありません。"

msgid "Invalid email format!"
msgstr "Eメールの形式ではありません。"

6- Please configure the language in AppController :

Configure::write('Config.language', 'jp or en');

Ok, that is. Let's enjoy.