AbstractAdapter.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ LKzi3HZ/M17qHTD/gVJs23m3hNyTCrKYHLjTtFPRsdI=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. namespace Cube\Translate\Adapter;
  13. use Cube\Locale;
  14. /**
  15. * abstract translate adapter
  16. *
  17. * Class AbstractAdapter
  18. *
  19. * @package Cube\Translate\Adapter
  20. */
  21. abstract class AbstractAdapter
  22. {
  23. /**
  24. *
  25. * translation sentences
  26. *
  27. * @var array
  28. */
  29. protected $_translate = array();
  30. /**
  31. *
  32. * current locale
  33. *
  34. * @var string
  35. */
  36. protected $_locale;
  37. /**
  38. *
  39. * class constructor
  40. *
  41. * @param array $options
  42. */
  43. public function __construct($options = array())
  44. {
  45. if ($options) {
  46. $this->addTranslation($options);
  47. }
  48. }
  49. /**
  50. *
  51. * get translation sentences array
  52. *
  53. * @return array
  54. */
  55. public function getTranslate()
  56. {
  57. return $this->_translate;
  58. }
  59. /**
  60. *
  61. * get locale
  62. *
  63. * @return string
  64. */
  65. public function getLocale()
  66. {
  67. if (!isset($this->_locale)) {
  68. $this->setLocale(Locale::DEFAULT_LOCALE);
  69. }
  70. return $this->_locale;
  71. }
  72. /**
  73. *
  74. * set active locale
  75. *
  76. * @param string|\Cube\Locale $locale
  77. *
  78. * @return $this
  79. * @throws \InvalidArgumentException
  80. */
  81. public function setLocale($locale)
  82. {
  83. if (is_string($locale)) {
  84. if (Locale::isLocale($locale) === false) {
  85. throw new \InvalidArgumentException(
  86. sprintf("Invalid locale '%s' set in the translation adapter.", $locale));
  87. }
  88. $this->_locale = $locale;
  89. }
  90. else if ($locale instanceof Locale) {
  91. $this->_locale = $locale->getLocale();
  92. }
  93. return $this;
  94. }
  95. /**
  96. *
  97. * translate a sentence in the object active locale or the set locale
  98. *
  99. * @param string $message
  100. * @param string $locale
  101. *
  102. * @throws \InvalidArgumentException
  103. * @return string
  104. */
  105. public function translate($message, $locale = null)
  106. {
  107. if (empty($message)) {
  108. return '';
  109. }
  110. if ($locale === null) {
  111. $locale = $this->getLocale();
  112. }
  113. else if (is_string($locale)) {
  114. if (Locale::isLocale($locale) === false) {
  115. throw new \InvalidArgumentException(
  116. sprintf("Invalid locale '%s' set in the translation adapter.", $locale));
  117. }
  118. }
  119. else if ($locale instanceof Locale) {
  120. $locale = $locale->getLocale();
  121. }
  122. else {
  123. throw new \InvalidArgumentException("The locale variable must be null,
  124. a string or an object of type \Cube\Locale");
  125. }
  126. if (isset($this->_translate[$locale])) {
  127. $message = trim($this->_filterString($message));
  128. if (array_key_exists($message, $this->_translate[$locale])) {
  129. return str_replace(array("'"), array('&#039;'), $this->_translate[$locale][$message]);
  130. }
  131. }
  132. return $message;
  133. }
  134. /**
  135. *
  136. * proxy to translate method
  137. *
  138. * @param string $message
  139. * @param string|\Cube\Locale $locale
  140. *
  141. * @return string
  142. */
  143. public function _($message, $locale = null)
  144. {
  145. return $this->translate($message, $locale);
  146. }
  147. /**
  148. *
  149. * filter a string of ascii tags
  150. *
  151. * @param string $string
  152. *
  153. * @return string
  154. */
  155. protected function _filterString($string)
  156. {
  157. return str_ireplace(
  158. array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&nbsp;'),
  159. array('&', "'", '"', '<', '>', ' '), $string);
  160. }
  161. /**
  162. *
  163. * add new translation in the adapter
  164. * needs a file that includes an array
  165. *
  166. * @param array $options
  167. *
  168. * @return $this
  169. * @throws \InvalidArgumentException
  170. */
  171. abstract public function addTranslation($options = array());
  172. }