[TOC]
# 國際化
Phalcon是用C語言編寫的PHP的擴展。有一個[PECL](http://pecl.php.net/package/intl)擴展,稱為`intl`的PHP應用程序提供國際化功能。從PHP 5.4 / 5.5開始,此擴展與PHP捆綁在一起。它的文檔可以在官方PHP手冊的頁面中找到。
Phalcon不提供此功能,因為創建此類組件將復制現有代碼。
在下面的示例中,我們將向您展示如何在Phalcon支持的應用程序中實現intl擴展的功能。
>[warning] 本指南不是 [intl](http://pecl.php.net/package/intl) 擴展的完整文檔。 請訪問該擴展程序的[文檔](http://www.php.net/manual/en/book.intl.php) 以獲取參考。
## 找出最佳的Locale
有幾種方法可以使用intl找出最佳的可用語言環境。 其中之一是檢查HTTP `Accept-Language`標頭:
```php
<?php
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
// Locale可能類似于'en_GB'或'en'
echo $locale;
```
以下方法返回標識的區域設置。它用于從Locale API獲取語言,文化或區域特定的行為。
標識符的示例包括:
* en-US(英語,美國)
* ru-RU(俄羅斯,俄羅斯)
* zh-Hant-TW(中文,繁體字,臺灣)
* fr-CA,fr-FR(法語分別為加拿大和法國)
## 根據區域設置格式化消息
創建本地化應用程序的一部分是生成連接的,與語言無關的消息。`MessageFormatter`允許生成這些消息。
打印基于某些區域設置格式化的數字:
```php
<?php
// Prints € 4 560
$formatter = new MessageFormatter('fr_FR', '€ {0, number, integer}');
echo $formatter->format([4560]);
// Prints USD$ 4,560.5
$formatter = new MessageFormatter('en_US', 'USD$ {0, number}');
echo $formatter->format([4560.50]);
// Prints ARS$ 1.250,25
$formatter = new MessageFormatter('es_AR', 'ARS$ {0, number}');
echo $formatter->format([1250.25]);
```
使用時間和日期模式格式化消息:
```php
<?php
// 設置參數
$time = time();
$values = [7, $time, $time];
// 打印 'At 3:50:31 PM on Apr 19, 2015, there was a disturbance on planet 7.'
$pattern = 'At {1, time} on {1, date}, there was a disturbance on planet {0, number}.';
$formatter = new MessageFormatter('en_US', $pattern);
echo $formatter->format($values);
// 打印 'à 15:53:01 le 19 avr. 2015, il y avait une perturbation sur la planète 7.'
$pattern = 'à {1, time} le {1, date}, il y avait une perturbation sur la planète {0, number}.';
$formatter = new MessageFormatter('fr_FR', $pattern);
echo $formatter->format($values);
```
## 區域敏感比較
Collator類提供字符串比較功能,支持適當的區域設置敏感排序順序。請查看以下有關此類用法的示例:
```php
<?php
// 使用西班牙語語言環境創建collator
$collator = new Collator('es');
// 返回字符串相等,盡管強調'o'
$collator->setStrength(Collator::PRIMARY);
var_dump($collator->compare('una canción', 'una cancion'));
// 返回字符串不相等
$collator->setStrength(Collator::DEFAULT_VALUE);
var_dump($collator->compare('una canción', 'una cancion'));
```
## 音譯
Transliterator提供字符串的音譯:
```php
<?php
$id = 'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();';
$transliterator = Transliterator::create($id);
$string = "gar?on-étudiant-où-L'école";
echo $transliterator->transliterate($string); // garconetudiantoulecole
```
- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持