多语言支持(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.

Adapter Description
NativeArray 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 freedomto 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 indifferent files, where keys remain the same and values contain the translated strings depending on each language.

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

Implementing the translation mechanism in your application is trivial but depends on how you wish to implement it. You can use anautomatic 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 itdirectly by calling $this->request->getBestLanguage() from an action/controller:

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

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 forcalculated data i.e. Hello %name%. These placeholders can be replaced with passed parameters in the () method. The passed parametersare 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", array("name" => $name)); ?></p>

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

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

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

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

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

原文: http://www.myleftstudio.com/reference/translate.html