I18nData.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. class I18nData {
  3. const REFERENCE_LANGUAGE = 'en';
  4. private $data = array();
  5. private $ignore = array();
  6. public function __construct($data, $ignore) {
  7. $this->data = $data;
  8. $this->ignore = $ignore;
  9. $this->synchonizeKeys();
  10. }
  11. public function getData() {
  12. $output = array();
  13. $reference = $this->getReferenceLanguage();
  14. $languages = $this->getNonReferenceLanguages();
  15. foreach ($reference as $file => $values) {
  16. foreach ($values as $key => $value) {
  17. $output[static::REFERENCE_LANGUAGE][$file][$key] = $value;
  18. foreach ($languages as $language) {
  19. if ($this->data[$language][$file][$key] !== $value) {
  20. // This value is translated, there is no need to flag it.
  21. $output[$language][$file][$key] = $this->data[$language][$file][$key];
  22. } elseif (array_key_exists($language, $this->ignore) && in_array($key, $this->ignore[$language])) {
  23. // This value is ignored, there is no need to flag it.
  24. $output[$language][$file][$key] = $this->data[$language][$file][$key];
  25. } else {
  26. // This value is not translated nor ignored, it must be flagged.
  27. $output[$language][$file][$key] = "{$value} -> todo";
  28. }
  29. }
  30. }
  31. }
  32. return $output;
  33. }
  34. public function getIgnore() {
  35. $ignore = array();
  36. foreach ($this->ignore as $language => $keys) {
  37. sort($keys);
  38. $ignore[$language] = $keys;
  39. }
  40. return $ignore;
  41. }
  42. private function synchonizeKeys() {
  43. $this->addMissingKeysFromReference();
  44. $this->removeExtraKeysFromOtherLanguages();
  45. $this->removeUnknownIgnoreKeys();
  46. }
  47. private function addMissingKeysFromReference() {
  48. $reference = $this->getReferenceLanguage();
  49. $languages = $this->getNonReferenceLanguages();
  50. foreach ($reference as $file => $values) {
  51. foreach ($values as $key => $value) {
  52. foreach ($languages as $language) {
  53. if (!array_key_exists($key, $this->data[$language][$file])) {
  54. $this->data[$language][$file][$key] = $value;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. private function removeExtraKeysFromOtherLanguages() {
  61. $reference = $this->getReferenceLanguage();
  62. foreach ($this->getNonReferenceLanguages() as $language) {
  63. foreach ($this->getLanguage($language) as $file => $values) {
  64. foreach ($values as $key => $value) {
  65. if (!array_key_exists($key, $reference[$file])) {
  66. unset($this->data[$language][$file][$key]);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. private function removeUnknownIgnoreKeys() {
  73. $reference = $this->getReferenceLanguage();
  74. foreach ($this->ignore as $language => $keys) {
  75. foreach ($keys as $index => $key) {
  76. if (!array_key_exists($this->getFilenamePrefix($key), $reference) || !array_key_exists($key, $reference[$this->getFilenamePrefix($key)])) {
  77. unset($this->ignore[$language][$index]);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * Return the available languages
  84. *
  85. * @return array
  86. */
  87. public function getAvailableLanguages() {
  88. $languages = array_keys($this->data);
  89. sort($languages);
  90. return $languages;
  91. }
  92. /**
  93. * Return all available languages without the reference language
  94. *
  95. * @return array
  96. */
  97. public function getNonReferenceLanguages() {
  98. return array_filter(array_keys($this->data), function ($value) {
  99. return static::REFERENCE_LANGUAGE !== $value;
  100. });
  101. }
  102. /**
  103. * Add a new language. It's a copy of the reference language.
  104. *
  105. * @param string $language
  106. * @throws Exception
  107. */
  108. public function addLanguage($language) {
  109. if (array_key_exists($language, $this->data)) {
  110. throw new Exception('The selected language already exist.');
  111. }
  112. $this->data[$language] = $this->data[static::REFERENCE_LANGUAGE];
  113. }
  114. /**
  115. * Add a new key to all languages.
  116. *
  117. * @param string $key
  118. * @param string $value
  119. * @throws Exception
  120. */
  121. public function addKey($key, $value) {
  122. if (array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
  123. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  124. throw new Exception('The selected key already exist.');
  125. }
  126. foreach ($this->getAvailableLanguages() as $language) {
  127. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  128. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  129. }
  130. }
  131. }
  132. /**
  133. * Add a value for a key for the selected language.
  134. *
  135. * @param string $key
  136. * @param string $value
  137. * @param string $language
  138. * @throws Exception
  139. */
  140. public function addValue($key, $value, $language) {
  141. if (!in_array($language, $this->getAvailableLanguages())) {
  142. throw new Exception('The selected language does not exist.');
  143. }
  144. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  145. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  146. throw new Exception('The selected key does not exist for the selected language.');
  147. }
  148. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  149. }
  150. /**
  151. * Remove a key in all languages
  152. *
  153. * @param string $key
  154. * @throws Exception
  155. */
  156. public function removeKey($key) {
  157. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  158. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  159. throw new Exception('The selected key does not exist.');
  160. }
  161. foreach ($this->getAvailableLanguages() as $language) {
  162. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  163. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  164. }
  165. if (array_key_exists($language, $this->ignore) && $position = array_search($key, $this->ignore[$language])) {
  166. unset($this->ignore[$language][$position]);
  167. }
  168. }
  169. }
  170. /**
  171. * Ignore a key from a language, or reverse it.
  172. *
  173. * @param string $key
  174. * @param string $language
  175. * @param boolean $reverse
  176. */
  177. public function ignore($key, $language, $reverse = false) {
  178. if (!array_key_exists($language, $this->ignore)) {
  179. $this->ignore[$language] = array();
  180. }
  181. $index = array_search($key, $this->ignore[$language]);
  182. if (false !== $index && $reverse) {
  183. unset($this->ignore[$language][$index]);
  184. return;
  185. }
  186. if (false !== $index && !$reverse) {
  187. return;
  188. }
  189. $this->ignore[$language][] = $key;
  190. }
  191. public function getLanguage($language) {
  192. return $this->data[$language];
  193. }
  194. public function getReferenceLanguage() {
  195. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  196. }
  197. /**
  198. * @param string $key
  199. * @return string
  200. */
  201. private function getFilenamePrefix($key) {
  202. return preg_replace('/\..*/', '.php', $key);
  203. }
  204. }