# 多語言支持(Multi-lingual Support)
The component[Phalcon\\Translate](http://docs.iphalcon.cn/api/Phalcon_Translate.html)aids in creating multilingual applications. Applications using this component, display content in different languages, based on the user’s chosen language supported by the application.
## 適配器(Adapters)
This component makes use of adapters to read translation messages from different sources in a unified way.
| Adapter | Description |
| --- | --- |
| [Phalcon\\Translate\\Adapter\\NativeArray](http://docs.iphalcon.cn/api/Phalcon_Translate_Adapter_NativeArray.html) | Uses PHP arrays to store the messages. This is the best option in terms of performance. |
## 組件的使用(Component Usage)
Translation strings are stored in files. The structure of these files could vary depending of the adapter used. Phalcon gives you the freedom to organize your translation strings. A simple structure could be:
~~~
app/messages/en.php
app/messages/es.php
app/messages/fr.php
app/messages/zh.php
~~~
Each file contains an array of the translations in a key/value manner. For each translation file, keys are unique. The same array is used in different files, where keys remain the same and values contain the translated strings depending on each language.
~~~
<?php
// app/messages/en.php
$messages = [
"hi" => "Hello",
"bye" => "Good Bye",
"hi-name" => "Hello %name%",
"song" => "This song is %song%",
];
~~~
~~~
<?php
// app/messages/fr.php
$messages = [
"hi" => "Bonjour",
"bye" => "Au revoir",
"hi-name" => "Bonjour %name%",
"song" => "La chanson est %song%",
];
~~~
Implementing the translation mechanism in your application is trivial but depends on how you wish to implement it. You can use an automatic detection of the language from the user’s browser or you can provide a settings page where the user can select their language.
A simple way of detecting the user’s language is to parse the`$_SERVER['HTTP_ACCEPT_LANGUAGE']`contents, or if you wish, access it directly by calling`$this->request->getBestLanguage()`from an action/controller:
~~~
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Translate\Adapter\NativeArray;
class UserController extends Controller
{
protected function getTranslation()
{
// Ask browser what is the best language
$language = $this->request->getBestLanguage();
$translationFile = "app/messages/" . $language . ".php";
// Check if we have a translation file for that lang
if (file_exists($translationFile)) {
require $translationFile;
} else {
// Fallback to some default
require "app/messages/en.php";
}
// Return a translation object
return new NativeArray(
[
"content" => $messages,
]
);
}
public function indexAction()
{
$this->view->name = "Mike";
$this->view->t = $this->getTranslation();
}
}
~~~
The`_getTranslation()`method is available for all actions that require translations. The`$t`variable is passed to the views, and with it, we can translate strings in that layer:
~~~
<!-- welcome -->
<!-- String: hi => 'Hello' -->
<p><?php echo $t->_("hi"), " ", $name; ?></p>
~~~
The`_()`method is returning the translated string based on the index passed. Some strings need to incorporate placeholders for calculated data i.e. Hello %name%. These placeholders can be replaced with passed parameters in the`_()`method. The passed parameters are in the form of a key/value array, where the key matches the placeholder name and the value is the actual data to be replaced:
~~~
<!-- welcome -->
<!-- String: hi-name => 'Hello %name%' -->
<p><?php echo $t->_("hi-name", ["name" => $name]); ?></p>
~~~
Some applications implement multilingual on the URL such as[http://www.mozilla.org/](http://www.mozilla.org/)**es-ES**/firefox/. Phalcon can implement this by using a[Router](http://docs.iphalcon.cn/reference/routing.html).
## 自定義適配器(Implementing your own adapters)
The[Phalcon\\Translate\\AdapterInterface](http://docs.iphalcon.cn/api/Phalcon_Translate_AdapterInterface.html)interface must be implemented in order to create your own translate adapters or extend the existing ones:
~~~
<?php
use Phalcon\Translate\AdapterInterface;
class MyTranslateAdapter implements AdapterInterface
{
/**
* Adapter constructor
*
* @param array $data
*/
public function __construct($options);
/**
* Returns the translation string of the given key
*
* @param string $translateKey
* @param array $placeholders
* @return string
*/
public function _($translateKey, $placeholders = null);
/**
* Returns the translation related to the given key
*
* @param string $index
* @param array $placeholders
* @return string
*/
public function query($index, $placeholders = null);
/**
* Check whether is defined a translation key in the internal array
*
* @param string $index
* @return bool
*/
public function exists($index);
}
~~~
There are more adapters available for this components in the[Phalcon Incubator](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Translate/Adapter)
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl