2010-04-18 68 views
2

如何本地化cakePhp中的字符串?我沒有在線文檔取得任何成功。謝謝你的幫助。在cakephp中使用本地化

+1

'「沒有任何成功的在線文檔」'?正如在「我無法在我的粗略掃描中找到正確的部分」或「我嘗試了幾件我不會告訴你的事情,但它們不起作用」?無論哪種方式,不好的問題。在Cake手冊中記錄了L10n。 – deceze 2010-04-19 05:58:21

回答

3

有幾個步驟:

  1. 首先,設置使用
  2. 該語言創建一個或多個.po文件的語言環境
  3. 包裝所有與您的本地化,能夠串__()__d()輔助方法

下面是我的一個項目的摘錄:

# app/app_controller.php 
uses ('L10n'); 

class AppController extends Controller { 
    public function beforeFilter() { 
    /** 
    * Derive the desired locale by reading the subdomain from 
    * the HTTP_HOST server variable. Locale subdomains can use 
    * either the 2 or 3 character ISO code. Information on locale 
    * ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php. 
    */ 
    $this->L10n = new L10n(); 

    /** Auto-detect the request language settings */ 
    $this->L10n->get(); 

    /** 
    * Set the default "domain" for translations. The domain is the 
    * same as the po file name in a given locale directory. e.g. 
    * __d('homepage', 'message_id') would look for the 
    * message_id key in homepage.po. Using the __() convenience 
    * function will always look in default.po. 
    */ 
    $this->set('domain', 'default'); 
    } 

    # The rest of your AppController code 
} 

該代碼段將設置語言。您只需在/app/locale/eng/LC_MESSAGES/目錄中提供相應的.po文件即可。我認爲CakePHP書提供了sufficient information on this

如果您選擇僅使用一個.po文件,則會將您的字符串與__()幫助程序包裝在一起。爲了避免一個大文件,我選擇了多個.po文件,因此我使用__d()幫助程序,以便我可以指定哪個域(域== == .po文件的名稱,而不包含.po擴展名)。

UPDATE

我要補充一點,你需要使用Translate行爲,以幫助您需要翻譯數據庫的內容。

+0

你根本不需要實例化'L10n'類。這在第一次調用'__()'函數時自動發生。 – deceze 2010-04-19 06:00:17

+0

有趣。我不知道,但是因爲我在這裏使用它來獲取(')'語言,在對任何'__()'進行評估之前我不需要一個實例嗎? – 2010-04-19 11:24:37

+0

不,這一切都是在你調用'__()'時完成的。您可以嘗試烘焙新的Cake應用程序,並在某處添加一個'__('Test')',爲該字符串提供翻譯.po文件並切換瀏覽器的首選語言設置。它會切換語言,而無需再做任何事情。 – deceze 2010-04-20 00:07:44