溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

YII中Framework框架如何實(shí)現(xiàn)國際化

發(fā)布時(shí)間:2021-08-27 10:40:27 來源:億速云 閱讀:121 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹YII中Framework框架如何實(shí)現(xiàn)國際化,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體如下:

一個(gè)web應(yīng)用,發(fā)布到互聯(lián)網(wǎng),就是面向全球用戶。用戶在世界的各個(gè)角落都可以訪問到你的web應(yīng)用,當(dāng)然要看你的網(wǎng)站和不和諧,不和諧的web應(yīng)用在和諧社會是不讓你訪問的。

YII提供了國際化的支持,可以讓我們創(chuàng)建的應(yīng)用適合不同語言的人群。

國際化是一個(gè)很花哨的東西,沒有哪個(gè)大型的網(wǎng)站真正能做到國際化。大多都是針對不懂的語言,不同地區(qū)設(shè)計(jì)不同的網(wǎng)站。如果你的應(yīng)用相對較小,處理的東西不多,那么國際化起來的東西還是蠻可以的。

國際化從以下幾個(gè)方面入手:

區(qū)域設(shè)置

信息文本和文件資源的翻譯

日期/時(shí)間、貨幣符號和數(shù)字格式

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

區(qū)域設(shè)置

通過對區(qū)域的設(shè)置,來判斷用戶所在的國際和使用的語言。

YII定義了常見的區(qū)域標(biāo)識,可以認(rèn)為是表示區(qū)域的唯一ID。

YII中通過CLocale類存放區(qū)域數(shù)據(jù)(包括貨幣,日期,數(shù)字格式等等)。

通過一個(gè)區(qū)域唯一ID,然后就可以通過 CLocale::getInstance($localeID) 或者CApplication::getLocale($localeID) 獲取相應(yīng)的 CLocale 實(shí)例。通過CLocale實(shí)例,就能夠判斷用戶所在的國家,使用的語言。然后可以根據(jù)CLocale的數(shù)據(jù)進(jìn)行相應(yīng)的翻譯,讓web應(yīng)用更適于當(dāng)前用戶使用和閱讀。最根本的就是為了用戶進(jìn)行特定的翻譯。

信息文本和文件資源的翻譯

翻譯很簡單就是把一種語言變成另一種語言。在計(jì)算機(jī)中用的是26字母,就是e文。所以可以把e文當(dāng)成是原始語言,萬語之源,所有其他的語言都是通過e文翻譯而成的,暫且e文叫做源語言。翻譯成的語言叫做目標(biāo)語言。

具體的類說明

/**
* 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源語言
$mesage目標(biāo)語言
$params是$mesage中要匹配翻譯的數(shù)組。

具體使用方法如:

Yii::t('app', 'Path alias "{alias}" is redefined.',
  array('{alias}'=>$alias))

當(dāng)然可以通過yiic提供的命令行命令message進(jìn)行翻譯,具體的參考yiic命令的使用說明

日期/時(shí)間、金錢和數(shù)字格式

日期/時(shí)間處理CDateFormatter類
具體參考(/yii_dev/yii/framework/i18n/CDateFormatter.php)類文件

數(shù)字處理
具體參考(/yii_dev/yii/framework/i18n/CNumberFormatter.php)類文件

以上是“YII中Framework框架如何實(shí)現(xiàn)國際化”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI