I18nData.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * Check if the key is known.
  116. *
  117. * @param string $key
  118. * @return bool
  119. */
  120. public function isKnown($key) {
  121. return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
  122. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  123. }
  124. /**
  125. * Add a new key to all languages.
  126. *
  127. * @param string $key
  128. * @param string $value
  129. * @throws Exception
  130. */
  131. public function addKey($key, $value) {
  132. if ($this->isKnown($key)) {
  133. throw new Exception('The selected key already exist.');
  134. }
  135. foreach ($this->getAvailableLanguages() as $language) {
  136. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  137. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  138. }
  139. }
  140. }
  141. /**
  142. * Add a value for a key for the selected language.
  143. *
  144. * @param string $key
  145. * @param string $value
  146. * @param string $language
  147. * @throws Exception
  148. */
  149. public function addValue($key, $value, $language) {
  150. if (!in_array($language, $this->getAvailableLanguages())) {
  151. throw new Exception('The selected language does not exist.');
  152. }
  153. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  154. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  155. throw new Exception('The selected key does not exist for the selected language.');
  156. }
  157. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  158. }
  159. /**
  160. * Remove a key in all languages
  161. *
  162. * @param string $key
  163. * @throws Exception
  164. */
  165. public function removeKey($key) {
  166. if (!$this->isKnown($key)) {
  167. throw new Exception('The selected key does not exist.');
  168. }
  169. foreach ($this->getAvailableLanguages() as $language) {
  170. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  171. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  172. }
  173. if (array_key_exists($language, $this->ignore) && $position = array_search($key, $this->ignore[$language])) {
  174. unset($this->ignore[$language][$position]);
  175. }
  176. }
  177. }
  178. /**
  179. * Ignore a key from a language, or reverse it.
  180. *
  181. * @param string $key
  182. * @param string $language
  183. * @param boolean $reverse
  184. */
  185. public function ignore($key, $language, $reverse = false) {
  186. if (!array_key_exists($language, $this->ignore)) {
  187. $this->ignore[$language] = array();
  188. }
  189. $index = array_search($key, $this->ignore[$language]);
  190. if (false !== $index && $reverse) {
  191. unset($this->ignore[$language][$index]);
  192. return;
  193. }
  194. if (false !== $index && !$reverse) {
  195. return;
  196. }
  197. $this->ignore[$language][] = $key;
  198. }
  199. public function getLanguage($language) {
  200. return $this->data[$language];
  201. }
  202. public function getReferenceLanguage() {
  203. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  204. }
  205. /**
  206. * @param string $key
  207. * @return string
  208. */
  209. private function getFilenamePrefix($key) {
  210. return preg_replace('/\..*/', '.php', $key);
  211. }
  212. }