Translate.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  13. * translate resource management class
  14. * creates the translate object and adds the adapter
  15. * only accepts a single adapter
  16. */
  17. namespace Cube\Application\Resource;
  18. use Cube\Translate as TranslateObject;
  19. class Translate extends AbstractResource
  20. {
  21. /**
  22. *
  23. * @var \Cube\Translate;
  24. */
  25. protected $_translate;
  26. /**
  27. *
  28. * initialize translate object
  29. *
  30. * @throws \RuntimeException
  31. * @return \Cube\Translate
  32. */
  33. public function init()
  34. {
  35. if (!($this->_translate instanceof TranslateObject)) {
  36. if (!array_key_exists('translate', $this->_options)) {
  37. throw new \RuntimeException("The 'translate' key in the configuration array must be set.");
  38. }
  39. $this->_translate = new TranslateObject();
  40. if (!array_key_exists('adapter', $this->_options['translate']) ||
  41. !array_key_exists('translations', $this->_options['translate'])
  42. ) {
  43. throw new \RuntimeException("The 'adapter' and 'translations' keys need to be set when configuring the translate object.");
  44. }
  45. $adapterClass = $this->_options['translate']['adapter'];
  46. if (!class_exists($adapterClass)) {
  47. throw new \RuntimeException(
  48. sprintf("Class %s doesn't exist", $adapterClass));
  49. }
  50. /** @var \Cube\Translate\Adapter\AbstractAdapter $adapter */
  51. $adapter = new $adapterClass();
  52. foreach ($this->_options['translate']['translations'] as $translation) {
  53. $adapter->addTranslation($translation);
  54. }
  55. $this->_translate->setAdapter($adapter);
  56. }
  57. return $this->_translate;
  58. }
  59. }