I18nData.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. class I18nData {
  3. const REFERENCE_LANGUAGE = 'en';
  4. private $data = array();
  5. private $originalData = array();
  6. public function __construct($data) {
  7. $this->data = $data;
  8. $this->originalData = $data;
  9. }
  10. public function getData() {
  11. return $this->data;
  12. }
  13. /**
  14. * Return the available languages
  15. *
  16. * @return array
  17. */
  18. public function getAvailableLanguages() {
  19. $languages = array_keys($this->data);
  20. sort($languages);
  21. return $languages;
  22. }
  23. /**
  24. * Add a new language. It's a copy of the reference language.
  25. *
  26. * @param string $language
  27. * @throws Exception
  28. */
  29. public function addLanguage($language) {
  30. if (array_key_exists($language, $this->data)) {
  31. throw new Exception('The selected language already exist.');
  32. }
  33. $this->data[$language] = $this->data[static::REFERENCE_LANGUAGE];
  34. }
  35. /**
  36. * Add a key in the reference language
  37. *
  38. * @param string $key
  39. * @param string $value
  40. * @throws Exception
  41. */
  42. public function addKey($key, $value) {
  43. if (array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
  44. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  45. throw new Exception('The selected key already exist.');
  46. }
  47. $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key] = $value;
  48. }
  49. /**
  50. * Add a value for a key for the selected language.
  51. *
  52. * @param string $key
  53. * @param string $value
  54. * @param string $language
  55. * @throws Exception
  56. */
  57. public function addValue($key, $value, $language) {
  58. if (!in_array($language, $this->getAvailableLanguages())) {
  59. throw new Exception('The selected language does not exist.');
  60. }
  61. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  62. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  63. throw new Exception('The selected key does not exist for the selected language.');
  64. }
  65. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  66. }
  67. /**
  68. * Duplicate a key from the reference language to all other languages
  69. *
  70. * @param string $key
  71. * @throws Exception
  72. */
  73. public function duplicateKey($key) {
  74. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  75. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  76. throw new Exception('The selected key does not exist.');
  77. }
  78. $value = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  79. foreach ($this->getAvailableLanguages() as $language) {
  80. if (static::REFERENCE_LANGUAGE === $language) {
  81. continue;
  82. }
  83. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  84. continue;
  85. }
  86. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  87. }
  88. }
  89. /**
  90. * Remove a key in all languages
  91. *
  92. * @param string $key
  93. * @throws Exception
  94. */
  95. public function removeKey($key) {
  96. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  97. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  98. throw new Exception('The selected key does not exist.');
  99. }
  100. foreach ($this->getAvailableLanguages() as $language) {
  101. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  102. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  103. }
  104. }
  105. }
  106. /**
  107. * Check if the data has changed
  108. *
  109. * @return bool
  110. */
  111. public function hasChanged() {
  112. return $this->data !== $this->originalData;
  113. }
  114. public function getLanguage($language) {
  115. return $this->data[$language];
  116. }
  117. public function getReferenceLanguage() {
  118. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  119. }
  120. /**
  121. * @param string $key
  122. * @return string
  123. */
  124. private function getFilenamePrefix($key) {
  125. return preg_replace('/\..*/', '.php', $key);
  126. }
  127. }