<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] # 多語言支持 `Phalcon\Translate` 組件有助于創建多語言應用程序。使用此組件的應用程序,根據應用程序支持的用戶所選語言顯示不同語言的內容。 ## 適配器 該組件使用適配器以統一的方式讀取來自不同源的翻譯消息。 | 適配器 | 描述 | | ------------------------------------------ | --------------------------------------------------------------------------------------- | | `Phalcon\Translate\Adapter\NativeArray` | 使用PHP數組來存儲消息。這是性能方面的最佳選擇。| ### 工廠 使用適配器選項加載Translate Adapter類 ```php <?php use Phalcon\Translate\Factory; $options = [ 'locale' => 'de_DE.UTF-8', 'defaultDomain' => 'translations', 'directory' => '/path/to/application/locales', 'category' => LC_MESSAGES, 'adapter' => 'gettext', ]; $translate = Factory::load($options); ``` ## 使用組件 翻譯字符串存儲在文件中。這些文件的結構可能因使用的適配器而異。Phalcon讓您可以自由地組織翻譯字符串。一個簡單的結構可能是: ```bash app/messages/en.php app/messages/es.php app/messages/fr.php app/messages/zh.php ``` 每個文件都以鍵/值的方式包含一系列翻譯。對于每個翻譯文件,密鑰都是唯一的。在不同的文件中使用相同的數組,其中鍵保持不變,值包含取決于每種語言的翻譯字符串。 ```php <?php // app/messages/en.php $messages = [ 'hi' => 'Hello', 'bye' => 'Good Bye', 'hi-name' => 'Hello %name%', 'song' => 'This song is %song%', ]; ``` ```php <?php // app/messages/fr.php $messages = [ 'hi' => 'Bonjour', 'bye' => 'Au revoir', 'hi-name' => 'Bonjour %name%', 'song' => 'La chanson est %song%', ]; ``` 在您的應用程序中實現轉換機制是微不足道的,但取決于您希望如何實現它。您可以使用用戶瀏覽器中的語言自動檢測,也可以提供用戶可以選擇語言的設置頁面。 檢測用戶語言的一種簡單方法是解析 `$_SERVER['HTTP_ACCEPT_LANGUAGE']` 內容,或者如果您愿意,可以通過從動作/控制器調用 `$this->request->getBestLanguage()` 來直接訪問它: ```php <?php use Phalcon\Mvc\Controller; use Phalcon\Translate\Adapter\NativeArray; class UserController extends Controller { protected function getTranslation() { // 詢問瀏覽器什么是最好的語言 $language = $this->request->getBestLanguage(); $messages = []; $translationFile = 'app/messages/' . $language . '.php'; // 檢查我們是否有該lang的翻譯文件 if (file_exists($translationFile)) { require $translationFile; } else { // 回退到某些默認值 require 'app/messages/en.php'; } // 返回翻譯對象$messages來自上面的require語句 return new NativeArray( [ 'content' => $messages, ] ); } public function indexAction() { $this->view->name = 'Mike'; $this->view->t = $this->getTranslation(); } } ``` `_getTranslation()` 方法適用于需要翻譯的所有操作。`$t` 變量傳遞給視圖,有了它,我們可以翻譯該層中的字符串: ```php <!-- welcome --> <!-- String: hi => 'Hello' --> <p><?php echo $t->_('hi'), ' ', $name; ?></p> ``` `_()` 方法根據傳遞的索引返回已翻譯的字符串。某些字符串需要包含計算數據的占位符,即`Hello %name%`。可以使用`_()`方法中的傳遞參數替換這些占位符。傳遞的參數采用 key/value 數組的形式,其中鍵與占位符名稱匹配,值是要替換的實際數據: ```php <!-- welcome --> <!-- String: hi-name => 'Hello %name%' --> <p><?php echo $t->_('hi-name', ['name' => $name]); ?></p> ``` 一些應用程序在URL上實現多語言,例如 `http://www.mozilla.org/**es-ES**/firefox/`。Phalcon可以使用路由器實現這一點。 上面的實現很有幫助,但它需要一個基本控制器來實現 `_getTranslation()` 并返回 `Phalcon\Translate\Adapter\NativeArray` 組件。另外,需要在視圖中設置組件,如上面 `$t` 變量中所示。 您始終可以將此功能包裝在自己的類中,并在DI容器中注冊該類: ```php <?php use Phalcon\Mvc\User\Component; use Phalcon\Translate\Adapter\NativeArray; class Locale extends Component { public function getTranslator() { // 詢問瀏覽器什么是最好的語言 $language = $this->request->getBestLanguage(); /** * 我們使用基于JSON的文件來存儲翻譯。 * 您需要檢查文件是否存在! */ $translations = json_decode( file_get_contents('app/messages/' . $language . '.json'), true ); // 返回翻譯對象$messages來自上面的require語句 return new NativeArray( [ 'content' => $translations, ] ); } } ``` 這樣您就可以在控制器中使用該組件: ```php <?php use Phalcon\Mvc\Controller; class MyController extends Controller { public function indexAction() { $name = 'Mike'; $text = $this->locale->_('hi-name', ['name' => $name]); $this->view->text = $text; } } ``` 或直接查看 ```php <?php echo $locale->_('hi-name', ['name' => 'Mike']); ``` <a name='custom'></a> ## 實現自己的適配器 必須實現 `Phalcon\Translate\AdapterInterface` 接口才能創建自己的轉換適配器或擴展現有轉換適配器: ```php <?php use Phalcon\Translate\AdapterInterface; class MyTranslateAdapter implements AdapterInterface { /** * Adapter constructor * * @param array $options */ public function __construct(array $options); /** * @param string $translateKey * @param array|null $placeholders * @return string */ public function t($translateKey, $placeholders = null); /** * 返回給定的key翻譯字符串 * * @param string $translateKey * @param array $placeholders * @return string */ public function _(string $translateKey, $placeholders = null): string; /** * 返回與給定的key相關的轉換 * * @param string $index * @param array $placeholders * @return string */ public function query(string $index, $placeholders = null): string; /** * 檢查是否在內部數組中定義了轉換key * * @param string $index * @return bool */ public function exists(string $index): bool; } ``` [Phalcon Incubator](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Translate/Adapter)中有更多適用于此組件的適配器
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看