Translate.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ JQ/llBxac2Qi2BDSApPlNuF8k1nb1C9IfVbXg/a0gjM=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\View\Helper;
  13. use Cube\Translate as TranslateObject,
  14. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  15. Cube\Controller\Front;
  16. /**
  17. * translate view helper
  18. *
  19. * Class Translate
  20. *
  21. * @package Cube\View\Helper
  22. */
  23. class Translate extends AbstractHelper
  24. {
  25. /**
  26. *
  27. * translate adapter used by the view helper
  28. *
  29. * @var \Cube\Translate\Adapter\AbstractAdapter
  30. */
  31. protected $_adapter;
  32. /**
  33. *
  34. * get translation adapter
  35. *
  36. * @return TranslateAdapter|null
  37. */
  38. public function getAdapter()
  39. {
  40. if ($this->_adapter === null) {
  41. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  42. $this->setAdapter($translate);
  43. }
  44. return $this->_adapter;
  45. }
  46. /**
  47. *
  48. * set translation adapter
  49. *
  50. * @param mixed $adapter
  51. *
  52. * @return $this
  53. * @throws \InvalidArgumentException
  54. */
  55. public function setAdapter($adapter)
  56. {
  57. if ($adapter instanceof TranslateAdapter) {
  58. $this->_adapter = $adapter;
  59. }
  60. else if ($adapter instanceof TranslateObject) {
  61. $this->_adapter = $adapter->getAdapter();
  62. }
  63. else {
  64. throw new \InvalidArgumentException("The translation adapter set in the view helper must be of type \Cube\Translate or \Cube\Translate\Adapter.");
  65. }
  66. return $this;
  67. }
  68. /**
  69. *
  70. * get locale
  71. *
  72. * @return string
  73. */
  74. public function getLocale()
  75. {
  76. return $this->getAdapter()->getLocale();
  77. }
  78. /**
  79. *
  80. * set locale
  81. *
  82. * @param string|\Cube\Locale $locale
  83. *
  84. * @return $this
  85. */
  86. public function setLocale($locale)
  87. {
  88. $this->getAdapter()->setLocale($locale);
  89. return $this;
  90. }
  91. /**
  92. *
  93. * returns translated string
  94. *
  95. * @param string $message
  96. *
  97. * @return string
  98. */
  99. public function translate($message = null)
  100. {
  101. if (!$message) {
  102. return '';
  103. }
  104. return $this->getAdapter()->translate($message);
  105. }
  106. }