本文講述了YII Framework框架教程之國(guó)際化實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一個(gè)web應(yīng)用,發(fā)布到互聯(lián)網(wǎng),就是面向全球用戶(hù)。用戶(hù)在世界的各個(gè)角落都可以訪問(wèn)到你的web應(yīng)用,當(dāng)然要看你的網(wǎng)站和不和諧,不和諧的web應(yīng)用在和諧社會(huì)是不讓你訪問(wèn)的。
YII提供了國(guó)際化的支持,可以讓我們創(chuàng)建的應(yīng)用適合不同語(yǔ)言的人群。
國(guó)際化是一個(gè)很花哨的東西,沒(méi)有哪個(gè)大型的網(wǎng)站真正能做到國(guó)際化。大多都是針對(duì)不懂的語(yǔ)言,不同地區(qū)設(shè)計(jì)不同的網(wǎng)站。如果你的應(yīng)用相對(duì)較小,處理的東西不多,那么國(guó)際化起來(lái)的東西還是蠻可以的。
國(guó)際化從以下幾個(gè)方面入手:
區(qū)域設(shè)置
信息文本和文件資源的翻譯
日期/時(shí)間、貨幣符號(hào)和數(shù)字格式
YII中國(guó)際化涉及到的類(lèi)在/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
區(qū)域設(shè)置
通過(guò)對(duì)區(qū)域的設(shè)置,來(lái)判斷用戶(hù)所在的國(guó)際和使用的語(yǔ)言。
YII定義了常見(jiàn)的區(qū)域標(biāo)識(shí),可以認(rèn)為是表示區(qū)域的唯一ID。
YII中通過(guò)CLocale類(lèi)存放區(qū)域數(shù)據(jù)(包括貨幣,日期,數(shù)字格式等等)。
通過(guò)一個(gè)區(qū)域唯一ID,然后就可以通過(guò) CLocale::getInstance($localeID) 或者CApplication::getLocale($localeID) 獲取相應(yīng)的 CLocale 實(shí)例。通過(guò)CLocale實(shí)例,就能夠判斷用戶(hù)所在的國(guó)家,使用的語(yǔ)言。然后可以根據(jù)CLocale的數(shù)據(jù)進(jìn)行相應(yīng)的翻譯,讓web應(yīng)用更適于當(dāng)前用戶(hù)使用和閱讀。最根本的就是為了用戶(hù)進(jìn)行特定的翻譯。
信息文本和文件資源的翻譯
翻譯很簡(jiǎn)單就是把一種語(yǔ)言變成另一種語(yǔ)言。在計(jì)算機(jī)中用的是26字母,就是e文。所以可以把e文當(dāng)成是原始語(yǔ)言,萬(wàn)語(yǔ)之源,所有其他的語(yǔ)言都是通過(guò)e文翻譯而成的,暫且e文叫做源語(yǔ)言。翻譯成的語(yǔ)言叫做目標(biāo)語(yǔ)言。
具體的類(lèi)說(shuō)明
/*** 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 <code>strtr</code>.* 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源語(yǔ)言
$mesage目標(biāo)語(yǔ)言
$params是$mesage中要匹配翻譯的數(shù)組。
具體使用方法如:
Yii::t('app', 'Path alias "{alias}" is redefined.', array('{alias}'=>$alias)) 當(dāng)然可以通過(guò)yiic提供的命令行命令message進(jìn)行翻譯,具體的參考yiic命令的使用說(shuō)明
日期/時(shí)間、金錢(qián)和數(shù)字格式
日期/時(shí)間處理CDateFormatter類(lèi)
具體參考(/yii_dev/yii/framework/i18n/CDateFormatter.php)類(lèi)文件
數(shù)字處理
具體參考(/yii_dev/yii/framework/i18n/CNumberFormatter.php)類(lèi)文件

















