首頁 php教程 PHP开发 YII Framework框架教程之國際化實作方法

YII Framework框架教程之國際化實作方法

Dec 27, 2016 pm 01:57 PM

文講述了YII Framework框架教程之國際化實作方法。分享給大家供大家參考,具體如下:

一個web應用,發佈到互聯網,就是面向全球用戶。使用者在世界的各個角落都可以存取到你的web應用,當然要看你的網站和不和諧,不和諧的web應用在和諧社會是不讓你訪問的。

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

國際化是一個很花俏的東西,沒有哪個大型的網站真正能做到國際化。大多都是針對不懂的語言,不同地區設計不同的網站。如果你的應用相對較小,處理的東西不多,那麼國際化起來的東西還蠻可以的。

國際化從以下幾個方面入手:

區域設定

資訊文字和文件資源的翻譯

日期/時間、貨幣符號和數字格式

YII中國際化涉及到的類在/yii_dev/yii /framework/i18n目錄下面:

/yii_dev/yii/framework/i18n# tree
.
├── CChoiceFormat.php
├── CDateFormatter.php
├Source CDbMessage ├── CLocale.php
├── CMessageSource.php
├── CNumberFormatter.php
├── CPhpMessageSource.php
├── data
│   ├ en
├── data
│   ├ en_en_.php—— ...............
│   ├── zh_hk.php
│   ├── zh_mo.php
│   ├── zh.php
── zh_tw.php
│   ├── zu.php
│   └── zu_za.php
└── gettext
    ├化php ettextPoFile.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 <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命令的使用說明

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

日期/時間處理CDateFormatteratter計劃

具體參考(/yii_dev/yii/framework/i18n/CDateFormatter.php)類別檔案

數字處理

具體參考(/yii_dev/yii/framework/i18n/CNumberFormatter.php)類別檔案

希望本文所述對大家基於Yii框架的PHP程式設計有所幫助。


更多YII Framework框架教程之國際化實現方法相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)