I18nData.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * WARNING! This is valid only for ignore files. It's not the best way to
  108. * handle that but as it's meant to be used only for the cli tool, there
  109. * is no point of spending time on making it better than that.
  110. *
  111. * Ignore a key from a language, or reverse it.
  112. *
  113. * @param string $key
  114. * @param string $language
  115. * @param boolean $reverse
  116. */
  117. public function ignore($key, $language, $reverse = false) {
  118. $index = array_search($key, $this->data[$language]);
  119. if ($index && $reverse) {
  120. unset($this->data[$language][$index]);
  121. return;
  122. }
  123. if ($index && !$reverse) {
  124. return;
  125. }
  126. $this->data[$language][] = $key;
  127. }
  128. /**
  129. * Check if the data has changed
  130. *
  131. * @return bool
  132. */
  133. public function hasChanged() {
  134. return $this->data !== $this->originalData;
  135. }
  136. public function getLanguage($language) {
  137. return $this->data[$language];
  138. }
  139. public function getReferenceLanguage() {
  140. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  141. }
  142. /**
  143. * @param string $key
  144. * @return string
  145. */
  146. private function getFilenamePrefix($key) {
  147. return preg_replace('/\..*/', '.php', $key);
  148. }
  149. }