I18nData.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * @param string $reference
  107. * @throws Exception
  108. */
  109. public function addLanguage($language, $reference = null) {
  110. if (array_key_exists($language, $this->data)) {
  111. throw new Exception('The selected language already exist.');
  112. }
  113. if (!is_string($reference) && !array_key_exists($reference, $this->data)) {
  114. $reference = static::REFERENCE_LANGUAGE;
  115. }
  116. $this->data[$language] = $this->data[$reference];
  117. }
  118. /**
  119. * Check if the key is known.
  120. *
  121. * @param string $key
  122. * @return bool
  123. */
  124. public function isKnown($key) {
  125. return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
  126. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  127. }
  128. /**
  129. * Return the parent key for a specified key.
  130. * To get the parent key, you need to remove the last section of the key. Each
  131. * is separated into sections. The parent of a section is the concatenation of
  132. * all sections before the selected key. For instance, if the key is 'a.b.c.d.e',
  133. * the parent key is 'a.b.c.d'.
  134. *
  135. * @return string
  136. */
  137. private function getParentKey($key) {
  138. return substr($key, 0, strrpos($key, '.'));
  139. }
  140. /**
  141. * Return the siblings for a specified key.
  142. * To get the siblings, we need to find all matches with the parent.
  143. */
  144. private function getSiblings($key) {
  145. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
  146. return [];
  147. }
  148. $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  149. $parent = $this->getParentKey($key);
  150. return array_values(array_filter($keys, function ($element) use ($parent) {
  151. return false !== strpos($element, $parent);
  152. }));
  153. }
  154. /**
  155. * Check if the key is an only child.
  156. * To be an only child, there must be only one sibling and that sibling must
  157. * be the empty sibling. The empty sibling is the parent.
  158. *
  159. * @return bool
  160. */
  161. private function isOnlyChild($key) {
  162. $siblings = $this->getSiblings($key);
  163. if (1 !== count($siblings)) {
  164. return false;
  165. }
  166. return '_' === $siblings[0][-1];
  167. }
  168. /**
  169. * Return the parent key as an empty sibling.
  170. * When a key has children, it cannot have its value directly. The value
  171. * needs to be attached to an empty sibling represented by "_".
  172. */
  173. private function getEmptySibling($key) {
  174. return "{$key}._";
  175. }
  176. /**
  177. * Add a new key to all languages.
  178. *
  179. * @param string $key
  180. * @param string $value
  181. * @throws Exception
  182. */
  183. public function addKey($key, $value) {
  184. if ($this->isKnown($key)) {
  185. throw new Exception('The selected key already exist.');
  186. }
  187. $parentKey = $this->getParentKey($key);
  188. if ($this->isKnown($parentKey)) {
  189. // The parent key exists, that means that we need to convert it to an array.
  190. // To create an array, we need to change the key by appending an empty section.
  191. foreach ($this->getAvailableLanguages() as $language) {
  192. $parentValue = $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey];
  193. $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)] = $parentValue;
  194. }
  195. }
  196. foreach ($this->getAvailableLanguages() as $language) {
  197. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  198. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  199. }
  200. }
  201. if ($this->isKnown($parentKey)) {
  202. $this->removeKey($parentKey);
  203. }
  204. }
  205. /**
  206. * Add a value for a key for the selected language.
  207. *
  208. * @param string $key
  209. * @param string $value
  210. * @param string $language
  211. * @throws Exception
  212. */
  213. public function addValue($key, $value, $language) {
  214. if (!in_array($language, $this->getAvailableLanguages())) {
  215. throw new Exception('The selected language does not exist.');
  216. }
  217. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  218. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  219. throw new Exception('The selected key does not exist for the selected language.');
  220. }
  221. if (static::REFERENCE_LANGUAGE === $language) {
  222. $previousValue = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  223. foreach ($this->getAvailableLanguages() as $lang) {
  224. if ($this->data[$lang][$this->getFilenamePrefix($key)][$key] === $previousValue) {
  225. $this->data[$lang][$this->getFilenamePrefix($key)][$key] = $value;
  226. }
  227. }
  228. } else {
  229. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  230. }
  231. }
  232. /**
  233. * Remove a key in all languages
  234. *
  235. * @param string $key
  236. * @throws Exception
  237. */
  238. public function removeKey($key) {
  239. if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
  240. throw new Exception('The selected key does not exist.');
  241. }
  242. if (!$this->isKnown($key)) {
  243. // The key has children, it needs to be appended with an empty section.
  244. $key = $this->getEmptySibling($key);
  245. }
  246. foreach ($this->getAvailableLanguages() as $language) {
  247. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  248. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  249. }
  250. if (array_key_exists($language, $this->ignore) && $position = array_search($key, $this->ignore[$language])) {
  251. unset($this->ignore[$language][$position]);
  252. }
  253. }
  254. if ($this->isOnlyChild($key)) {
  255. $parentKey = $this->getParentKey($key);
  256. foreach ($this->getAvailableLanguages() as $language) {
  257. $parentValue = $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)];
  258. $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey] = $parentValue;
  259. }
  260. $this->removeKey($this->getEmptySibling($parentKey));
  261. }
  262. }
  263. /**
  264. * Ignore a key from a language, or reverse it.
  265. *
  266. * @param string $key
  267. * @param string $language
  268. * @param boolean $reverse
  269. */
  270. public function ignore($key, $language, $reverse = false) {
  271. if (!array_key_exists($language, $this->ignore)) {
  272. $this->ignore[$language] = array();
  273. }
  274. $index = array_search($key, $this->ignore[$language]);
  275. if (false !== $index && $reverse) {
  276. unset($this->ignore[$language][$index]);
  277. return;
  278. }
  279. if (false !== $index && !$reverse) {
  280. return;
  281. }
  282. $this->ignore[$language][] = $key;
  283. }
  284. /**
  285. *Ignore all unmodified keys from a language, or reverse it.
  286. *
  287. * @param string $language
  288. * @param boolean $reverse
  289. */
  290. public function ignore_unmodified($language, $reverse = false) {
  291. $my_language = $this->getLanguage($language);
  292. foreach ($this->getReferenceLanguage() as $file => $ref_language) {
  293. foreach ($ref_language as $key => $ref_value) {
  294. if (array_key_exists($key, $my_language[$file])) {
  295. if($ref_value == $my_language[$file][$key]) {
  296. $this->ignore($key, $language, $reverse);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. public function getLanguage($language) {
  303. return $this->data[$language];
  304. }
  305. public function getReferenceLanguage() {
  306. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  307. }
  308. /**
  309. * @param string $key
  310. * @return string
  311. */
  312. private function getFilenamePrefix($key) {
  313. return preg_replace('/\..*/', '.php', $key);
  314. }
  315. }