YII Framework架構教程之國際化實現方法,yiiframework
本文講述了YII Framework架構教程之國際化實現方法。分享給大家供大家參考,具體如下:
一個web應用,發布到互連網,就是面向全球使用者。使用者在世界的各個角落都可以訪問到你的web應用,當然要看你的網站和不和諧,不和諧的web應用在和諧社會是不讓你訪問的。
YII提供了國際化的支援,可以讓我們建立的應用適合不同語言的人群。
國際化是一個很花哨的東西,沒有哪個大型的網站真正能做到國際化。大多都是針對不懂的語言,不同地區設計不同的網站。如果你的應用相對較小,處理的東西不多,那麼國際化起來的東西還是蠻可以的。
國際化從以下幾個方面入手:
地區設定
資訊文本和檔案資源的翻譯
日期/時間、貨幣符號和數字格式
YII中國際化涉及到的類在/yii_dev/yii/framework/i18n目錄下面:
/yii_dev/yii/framework/i18n# tree
.
├── CChoiceFormat.php
├── CDateFormatter.php
├── CDbMessageSource.php
├── CGettextMessageSource.php
├── CLocale.php
├── CMessageSource.php
├── CNumberFormatter.php
├── CPhpMessageSource.php
├── data
│ ├── en_us.php
│ ├── ....................
│ ├── zh_hk.php
│ ├── zh_mo.php
│ ├── zh.php
│ ├── zh_sg.php
│ ├── zh_tw.php
│ ├── zu.php
│ └── zu_za.php
└── gettext
├── CGettextFile.php
├── CGettextMoFile.php
└── CGettextPoFile.php
2 directories, 616 files
地區設定
通過對地區的設定,來判斷使用者所在的國際和使用的語言。
YII定義了常見的地區標識,可以認為是表示地區的唯一ID。
YII中通過CLocale類存放地區資料(包括貨幣,日期,數字格式等等)。
通過一個地區唯一ID,然後就可以通過 CLocale::getInstance($localeID) 或者CApplication::getLocale($localeID) 擷取相應的 CLocale 執行個體。通過CLocale執行個體,就能夠判斷使用者所在的國家,使用的語言。然後可以根據CLocale的資料進行相應的翻譯,讓web應用更適於目前使用者使用和閱讀。最根本的就是為了使用者進行特定的翻譯。
資訊文本和檔案資源的翻譯
翻譯很簡單就是把一種語言變成另一種語言。在電腦中用的是26字母,就是e文。所以可以把e文當成是原始語言,萬語之源,所有其他的語言都是通過e文翻譯而成的,暫且e文叫做源語言。翻譯成的語言叫做目標語言。
具體的類說明
/*** Translates a message to the specified language.* Starting from version 1.0.2, this method supports choice format (see {@link CChoiceFormat}),* i.e., the message returned will be chosen from a few candidates according to the given* number value. This feature is mainly used to solve plural format issue in case* a message has different plural forms in some languages.* @param string $category message category. Please use only word letters. Note, category 'yii' is* reserved for Yii framework core code use. See {@link CPhpMessageSource} for* more interpretation about message category.* @param string $message the original message* @param array $params parameters to be applied to the message using strtr.* Starting from version 1.0.2, the first parameter can be a number without key.* And in this case, the method will call {@link CChoiceFormat::format} to choose* an appropriate message translation.* Starting from version 1.1.6 you can pass parameter for {@link CChoiceFormat::format}* or plural forms format without wrapping it with array.* @param string $source which message source application component to use.* Defaults to null, meaning using 'coreMessages' for messages belonging to* the 'yii' category and using 'messages' for the rest messages.* @param string $language the target language. If null (default), the {@link CApplication::getLanguage application language} will be used.* This parameter has been available since version 1.0.3.* @return string the translated message* @see CMessageSource*/public static function t($category,$message,$params=array(),$source=null,$language=null){
$category源語言
$mesage目標語言
$params是$mesage中要匹配翻譯的數組。
具體使用方法如:
Yii::t('app', 'Path alias "{alias}" is redefined.', array('{alias}'=>$alias))
當然可以通過yiic提供的命令列命令message進行翻譯,具體的參考yiic命令的使用說明
日期/時間、金錢和數字格式
日期/時間處理CDateFormatter類
具體參考(/yii_dev/yii/framework/i18n/CDateFormatter.php)類檔案
數文書處理
具體參考(/yii_dev/yii/framework/i18n/CNumberFormatter.php)類檔案
更多關於Yii相關內容感興趣的讀者可查看本站專題:《Yii架構入門及常用技巧總結》、《php優秀開發架構總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家基於Yii架構的PHP程式設計有所協助。
您可能感興趣的文章:
- PHP YII架構開發小技巧之模型(models)中rules自訂驗證規則
- Nginx配置PHP的Yii與CakePHP架構的rewrite規則樣本
- 使用Composer安裝Yii架構的方法
- Yii使用migrate命令執行sql語句的方法
- YII Framework架構使用YIIC快速建立YII應用之migrate用法執行個體詳解
- YII Framework架構教程之使用YIIC快速建立YII應用詳解
- YII Framework架構教程之緩衝用法詳解
- YII Framework架構教程之安全方案詳解
- YII Framework架構教程之日誌用法詳解
- YII Framework教程之異常處理詳解
- Yii rules常用規則樣本
http://www.bkjia.com/PHPjc/1110084.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1110084.htmlTechArticleYII Framework架構教程之國際化實現方法,yiiframework 本文講述了YII Framework架構教程之國際化實現方法。分享給大家供大家參考,具體如下:...