YII Framework框架實現國際化的方法

不言
發布: 2023-04-01 10:48:02
原創
1584 人瀏覽過

這篇文章主要介紹了YII Framework框架教程之國際化實現方法,較為詳細的分析了YII Framework框架國際化的原理與相關實現技巧,需要的朋友可以參考下

本文講述了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##text#└# gettext 。區域的設置,來判斷使用者所在的國際和使用的語言。

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 <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 &#39;coreMessages&#39; for messages belonging to
* the &#39;yii&#39; category and using &#39;messages&#39; 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(&#39;app&#39;, &#39;Path alias "{alias}" is redefined.&#39;,
  array(&#39;{alias}&#39;=>$alias))
登入後複製

#當然可以透過yiic提供的命令列指令message來翻譯,具體的參考yiic指令的使用說明



日期/時間、金錢和數字格式

日期/時間處理CDateFormatter類別

以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!

相關推薦:

YiiFramework的入門知識點總結


關於Yii Framework框架取得分類下面的所有子類別的方法


#

以上是YII Framework框架實現國際化的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!