I18nData.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. class I18nData {
  3. const REFERENCE_LANGUAGE = 'en';
  4. private $data = [];
  5. public function __construct(array $data) {
  6. $this->data = $data;
  7. $this->addMissingKeysFromReference();
  8. $this->removeExtraKeysFromOtherLanguages();
  9. $this->processValueStates();
  10. }
  11. public function getData() {
  12. return $this->data;
  13. }
  14. private function addMissingKeysFromReference() {
  15. $reference = $this->getReferenceLanguage();
  16. $languages = $this->getNonReferenceLanguages();
  17. foreach ($reference as $file => $refValues) {
  18. foreach ($refValues as $key => $refValue) {
  19. foreach ($languages as $language) {
  20. if (!array_key_exists($file, $this->data[$language]) || !array_key_exists($key, $this->data[$language][$file])) {
  21. $this->data[$language][$file][$key] = clone $refValue;
  22. }
  23. $value = $this->data[$language][$file][$key];
  24. if ($refValue->equal($value) && !$value->isIgnore()) {
  25. $value->markAsTodo();
  26. }
  27. }
  28. }
  29. }
  30. }
  31. private function removeExtraKeysFromOtherLanguages() {
  32. $reference = $this->getReferenceLanguage();
  33. foreach ($this->getNonReferenceLanguages() as $language) {
  34. foreach ($this->getLanguage($language) as $file => $values) {
  35. foreach ($values as $key => $value) {
  36. if (!array_key_exists($key, $reference[$file])) {
  37. unset($this->data[$language][$file][$key]);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. private function processValueStates() {
  44. $reference = $this->getReferenceLanguage();
  45. $languages = $this->getNonReferenceLanguages();
  46. foreach ($reference as $file => $refValues) {
  47. foreach ($refValues as $key => $refValue) {
  48. foreach ($languages as $language) {
  49. $value = $this->data[$language][$file][$key];
  50. if ($refValue->equal($value) && !$value->isIgnore()) {
  51. $value->markAsTodo();
  52. continue;
  53. }
  54. if (!$refValue->equal($value) && $value->isTodo()) {
  55. $value->markAsDirty();
  56. continue;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * Return the available languages
  64. *
  65. * @return array
  66. */
  67. public function getAvailableLanguages() {
  68. $languages = array_keys($this->data);
  69. sort($languages);
  70. return $languages;
  71. }
  72. /**
  73. * Return all available languages without the reference language
  74. *
  75. * @return array
  76. */
  77. private function getNonReferenceLanguages() {
  78. return array_filter(array_keys($this->data), function ($value) {
  79. return static::REFERENCE_LANGUAGE !== $value;
  80. });
  81. }
  82. /**
  83. * Add a new language. It’s a copy of the reference language.
  84. *
  85. * @param string $language
  86. * @param string $reference
  87. * @throws Exception
  88. */
  89. public function addLanguage($language, $reference = null) {
  90. if (array_key_exists($language, $this->data)) {
  91. throw new Exception('The selected language already exist.');
  92. }
  93. if (!is_string($reference) || !array_key_exists($reference, $this->data)) {
  94. $reference = static::REFERENCE_LANGUAGE;
  95. }
  96. $this->data[$language] = $this->data[$reference];
  97. }
  98. /**
  99. * Check if the key is known.
  100. *
  101. * @param string $key
  102. * @return bool
  103. */
  104. public function isKnown($key) {
  105. return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
  106. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  107. }
  108. /**
  109. * Return the parent key for a specified key.
  110. * To get the parent key, you need to remove the last section of the key. Each
  111. * is separated into sections. The parent of a section is the concatenation of
  112. * all sections before the selected key. For instance, if the key is 'a.b.c.d.e',
  113. * the parent key is 'a.b.c.d'.
  114. *
  115. * @return string
  116. */
  117. private function getParentKey($key) {
  118. return substr($key, 0, strrpos($key, '.'));
  119. }
  120. /**
  121. * Return the siblings for a specified key.
  122. * To get the siblings, we need to find all matches with the parent.
  123. */
  124. private function getSiblings($key) {
  125. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
  126. return [];
  127. }
  128. $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  129. $parent = $this->getParentKey($key);
  130. return array_values(array_filter($keys, function ($element) use ($parent) {
  131. return false !== strpos($element, $parent);
  132. }));
  133. }
  134. /**
  135. * Check if the key is an only child.
  136. * To be an only child, there must be only one sibling and that sibling must
  137. * be the empty sibling. The empty sibling is the parent.
  138. *
  139. * @return bool
  140. */
  141. private function isOnlyChild($key) {
  142. $siblings = $this->getSiblings($key);
  143. if (1 !== count($siblings)) {
  144. return false;
  145. }
  146. return '_' === $siblings[0][-1];
  147. }
  148. /**
  149. * Return the parent key as an empty sibling.
  150. * When a key has children, it cannot have its value directly. The value
  151. * needs to be attached to an empty sibling represented by "_".
  152. */
  153. private function getEmptySibling($key) {
  154. return "{$key}._";
  155. }
  156. /**
  157. * Check if a key is a parent key.
  158. * To be a parent key, there must be at least one key starting with the key
  159. * under test. Of course, it cannot be itself.
  160. */
  161. private function isParent($key) {
  162. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
  163. return false;
  164. }
  165. $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  166. $children = array_values(array_filter($keys, function ($element) use ($key) {
  167. if ($element === $key) {
  168. return false;
  169. }
  170. return false !== strpos($element, $key);
  171. }));
  172. return count($children) !== 0;
  173. }
  174. /**
  175. * Add a new key to all languages.
  176. *
  177. * @param string $key
  178. * @param string $value
  179. * @throws Exception
  180. */
  181. public function addKey($key, $value) {
  182. if ($this->isParent($key)) {
  183. $key = $this->getEmptySibling($key);
  184. }
  185. if ($this->isKnown($key)) {
  186. throw new Exception('The selected key already exist.');
  187. }
  188. $parentKey = $this->getParentKey($key);
  189. if ($this->isKnown($parentKey)) {
  190. // The parent key exists, that means that we need to convert it to an array.
  191. // To create an array, we need to change the key by appending an empty section.
  192. foreach ($this->getAvailableLanguages() as $language) {
  193. $parentValue = $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey];
  194. $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)] =
  195. new I18nValue($parentValue);
  196. }
  197. }
  198. $value = new I18nValue($value);
  199. $value->markAsTodo();
  200. foreach ($this->getAvailableLanguages() as $language) {
  201. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  202. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  203. }
  204. }
  205. if ($this->isKnown($parentKey)) {
  206. $this->removeKey($parentKey);
  207. }
  208. }
  209. /**
  210. * Add a value for a key for the selected language.
  211. *
  212. * @param string $key
  213. * @param string $value
  214. * @param string $language
  215. * @throws Exception
  216. */
  217. public function addValue($key, $value, $language) {
  218. if (!in_array($language, $this->getAvailableLanguages())) {
  219. throw new Exception('The selected language does not exist.');
  220. }
  221. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  222. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  223. throw new Exception('The selected key does not exist for the selected language.');
  224. }
  225. $value = new I18nValue($value);
  226. if (static::REFERENCE_LANGUAGE === $language) {
  227. $previousValue = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  228. foreach ($this->getAvailableLanguages() as $lang) {
  229. $currentValue = $this->data[$lang][$this->getFilenamePrefix($key)][$key];
  230. if ($currentValue->equal($previousValue)) {
  231. $this->data[$lang][$this->getFilenamePrefix($key)][$key] = $value;
  232. }
  233. }
  234. } else {
  235. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  236. }
  237. }
  238. /**
  239. * Remove a key in all languages
  240. *
  241. * @param string $key
  242. * @throws Exception
  243. */
  244. public function removeKey($key) {
  245. if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
  246. throw new Exception('The selected key does not exist.');
  247. }
  248. if (!$this->isKnown($key)) {
  249. // The key has children, it needs to be appended with an empty section.
  250. $key = $this->getEmptySibling($key);
  251. }
  252. foreach ($this->getAvailableLanguages() as $language) {
  253. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  254. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  255. }
  256. }
  257. if ($this->isOnlyChild($key)) {
  258. $parentKey = $this->getParentKey($key);
  259. foreach ($this->getAvailableLanguages() as $language) {
  260. $parentValue = $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)];
  261. $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey] = $parentValue;
  262. }
  263. $this->removeKey($this->getEmptySibling($parentKey));
  264. }
  265. }
  266. /**
  267. * Ignore a key from a language, or reverse it.
  268. *
  269. * @param string $key
  270. * @param string $language
  271. * @param boolean $reverse
  272. */
  273. public function ignore($key, $language, $reverse = false) {
  274. $value = $this->data[$language][$this->getFilenamePrefix($key)][$key];
  275. if ($reverse) {
  276. $value->markAsIgnore();
  277. } else {
  278. $value->unmarkAsIgnore();
  279. }
  280. }
  281. /**
  282. * Ignore all unmodified keys from a language, or reverse it.
  283. *
  284. * @param string $language
  285. * @param boolean $reverse
  286. */
  287. public function ignore_unmodified($language, $reverse = false) {
  288. $my_language = $this->getLanguage($language);
  289. foreach ($this->getReferenceLanguage() as $file => $ref_language) {
  290. foreach ($ref_language as $key => $ref_value) {
  291. if (array_key_exists($key, $my_language[$file])) {
  292. if($ref_value->equal($my_language[$file][$key])) {
  293. $this->ignore($key, $language, $reverse);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. public function getLanguage($language) {
  300. return $this->data[$language];
  301. }
  302. public function getReferenceLanguage() {
  303. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  304. }
  305. /**
  306. * @param string $key
  307. * @return string
  308. */
  309. private function getFilenamePrefix($key) {
  310. return preg_replace('/\..*/', '.php', $key);
  311. }
  312. }