多语言支持(Multi-lingual Support)

The component Phalcon\Translate 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.

AdapterDescription
Phalcon\Translate\Adapter\NativeArrayUses 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:

  1. app/messages/en.php
  2. app/messages/es.php
  3. app/messages/fr.php
  4. 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.

  1. <?php
  2. // app/messages/en.php
  3. $messages = [
  4. "hi" => "Hello",
  5. "bye" => "Good Bye",
  6. "hi-name" => "Hello %name%",
  7. "song" => "This song is %song%",
  8. ];
  1. <?php
  2. // app/messages/fr.php
  3. $messages = [
  4. "hi" => "Bonjour",
  5. "bye" => "Au revoir",
  6. "hi-name" => "Bonjour %name%",
  7. "song" => "La chanson est %song%",
  8. ];

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:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. use Phalcon\Translate\Adapter\NativeArray;
  4. class UserController extends Controller
  5. {
  6. protected function getTranslation()
  7. {
  8. // Ask browser what is the best language
  9. $language = $this->request->getBestLanguage();
  10. $translationFile = "app/messages/" . $language . ".php";
  11. // Check if we have a translation file for that lang
  12. if (file_exists($translationFile)) {
  13. require $translationFile;
  14. } else {
  15. // Fallback to some default
  16. require "app/messages/en.php";
  17. }
  18. // Return a translation object
  19. return new NativeArray(
  20. [
  21. "content" => $messages,
  22. ]
  23. );
  24. }
  25. public function indexAction()
  26. {
  27. $this->view->name = "Mike";
  28. $this->view->t = $this->getTranslation();
  29. }
  30. }

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:

  1. <!-- welcome -->
  2. <!-- String: hi => 'Hello' -->
  3. <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:

  1. <!-- welcome -->
  2. <!-- String: hi-name => 'Hello %name%' -->
  3. <p><?php echo $t->_("hi-name", ["name" => $name]); ?></p>

Some applications implement multilingual on the URL such as http://www.mozilla.org/es-ES/firefox/. Phalcon can implement this by using a Router.

自定义适配器(Implementing your own adapters)

The Phalcon\Translate\AdapterInterface interface must be implemented in order to create your own translate adapters or extend the existing ones:

  1. <?php
  2. use Phalcon\Translate\AdapterInterface;
  3. class MyTranslateAdapter implements AdapterInterface
  4. {
  5. /**
  6. * Adapter constructor
  7. *
  8. * @param array $data
  9. */
  10. public function __construct($options);
  11. /**
  12. * Returns the translation string of the given key
  13. *
  14. * @param string $translateKey
  15. * @param array $placeholders
  16. * @return string
  17. */
  18. public function _($translateKey, $placeholders = null);
  19. /**
  20. * Returns the translation related to the given key
  21. *
  22. * @param string $index
  23. * @param array $placeholders
  24. * @return string
  25. */
  26. public function query($index, $placeholders = null);
  27. /**
  28. * Check whether is defined a translation key in the internal array
  29. *
  30. * @param string $index
  31. * @return bool
  32. */
  33. public function exists($index);
  34. }

There are more adapters available for this components in the Phalcon Incubator