Cakephp:多國語系網站建制

一般網站擴張到國際都要搞多語系, 以下是cakephp多語系建制的步驟。

假設你的兩個Domain分別是:
1.www.your-site.com (英文)
2.zh-tw.your-site.com (繁體中文)

step1.修改你的bootstrap.php,設定你的config language

if($_SERVER['SERVER_NAME'] == 'www.your-site.com'){
Configure::write('Config.language', 'eng');
}elseif($_SERVER['SERVER_NAME'] == 'zh-TW.your-site.com'){
Configure::write('Config.language', 'zh-TW');
}elseif($_SERVER['SERVER_NAME'] == 'xxx'){
#依此類推
}

step2.修改所有你要轉換的字串
例如有一串字 ‘購物車’,將它修改成以下:

<?= __('購物車'); ?>

step3.設定console cake

#先將cake這個command加到.bashrc
export PATH="$PATH:/Users/mark/cakephp/lib/Cake/Console"
#之後你可以進入你的cakephp的app目錄裡面打cake,會發現可以用了,你會得到類似以下訊息
Current Paths:

-app: app
-working: /path/to/cakephp/app
-root: /path/to/cakephp/
-core: /path/to/cakephp/core

Changing Paths:

your working path should be the same as your application path
to change your path use the '-app' param.
Example: -app relative/path/to/cakephp/app or -app /absolute/path/to/cakephp/app

Available Shells:

acl [CORE] i18n [CORE]
api [CORE] import [app]
bake [CORE] schema [CORE]
command_list [CORE] testsuite [CORE]
console [CORE] upgrade [CORE]

To run a command, type 'cake shell_name [args]'
To get help on a specific command, type 'cake shell_name help'

step3.產生pot檔

產生po檔案指令:
cake i18n extract --extract-core no
接下來它會問你What is the path you would like to extract?
輸入你要轉解析的路徑即可。然後按D[Done]
接下來它會問你What is the path your would like to output?
輸入你要放置pot檔的位置,習慣是先放到app/Locale下
接下來它會問你Would you like to merge all domains strings into the default.pot file?
按n,不要即可。

#這樣應該就會在app/Locale產生一個default.pot檔了

step4.將default.po放置到正確路徑&翻譯
如果你的Config.language是eng,它會去找app/Locale目錄下的eng這個目錄。
如果你的Config.language是zh-tw,它會去找app/Locale目錄下的zh-tw這個目錄。
將剛剛的default.pot放到app/Locale/eng/LC_MESSAGES目錄下,檔名改成default.po
接下來編輯default.po

msgid "購物車"
msgstr "Cart"

#以下依此類推

如果你還有更多語言,跟上面步驟一樣,放到Locale對應的目錄下即可。

另外還有其他方式可用,例如__d(‘domain’,’購物車’)
domain可以讓妳指定,如果你用__d,生出來的就會是domain.pot,給你要指定的domain做轉換生成pot檔案。

以上是多國語系建制簡略說明。

參考文件:
1.http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html
2.http://book.cakephp.org/2.0/en/console-and-shells.html
3.http://book.cakephp.org/2.0/en/console-and-shells/i18n-shell.html