I18nData.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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($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. public 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. * Add a new key to all languages.
  158. *
  159. * @param string $key
  160. * @param string $value
  161. * @throws Exception
  162. */
  163. public function addKey($key, $value) {
  164. if ($this->isKnown($key)) {
  165. throw new Exception('The selected key already exist.');
  166. }
  167. $parentKey = $this->getParentKey($key);
  168. if ($this->isKnown($parentKey)) {
  169. // The parent key exists, that means that we need to convert it to an array.
  170. // To create an array, we need to change the key by appending an empty section.
  171. foreach ($this->getAvailableLanguages() as $language) {
  172. $parentValue = $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey];
  173. $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)] =
  174. new I18nValue($parentValue);
  175. }
  176. }
  177. $value = new I18nValue($value);
  178. $value->markAsTodo();
  179. foreach ($this->getAvailableLanguages() as $language) {
  180. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  181. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  182. }
  183. }
  184. if ($this->isKnown($parentKey)) {
  185. $this->removeKey($parentKey);
  186. }
  187. }
  188. /**
  189. * Add a value for a key for the selected language.
  190. *
  191. * @param string $key
  192. * @param string $value
  193. * @param string $language
  194. * @throws Exception
  195. */
  196. public function addValue($key, $value, $language) {
  197. if (!in_array($language, $this->getAvailableLanguages())) {
  198. throw new Exception('The selected language does not exist.');
  199. }
  200. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  201. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  202. throw new Exception('The selected key does not exist for the selected language.');
  203. }
  204. $value = new I18nValue($value);
  205. if (static::REFERENCE_LANGUAGE === $language) {
  206. $previousValue = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  207. foreach ($this->getAvailableLanguages() as $lang) {
  208. if ($this->data[$lang][$this->getFilenamePrefix($key)][$key] === $previousValue) {
  209. $this->data[$lang][$this->getFilenamePrefix($key)][$key] = $value;
  210. }
  211. }
  212. } else {
  213. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  214. }
  215. }
  216. /**
  217. * Remove a key in all languages
  218. *
  219. * @param string $key
  220. * @throws Exception
  221. */
  222. public function removeKey($key) {
  223. if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
  224. throw new Exception('The selected key does not exist.');
  225. }
  226. if (!$this->isKnown($key)) {
  227. // The key has children, it needs to be appended with an empty section.
  228. $key = $this->getEmptySibling($key);
  229. }
  230. foreach ($this->getAvailableLanguages() as $language) {
  231. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  232. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  233. }
  234. }
  235. if ($this->isOnlyChild($key)) {
  236. $parentKey = $this->getParentKey($key);
  237. foreach ($this->getAvailableLanguages() as $language) {
  238. $parentValue = $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)];
  239. $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey] = $parentValue;
  240. }
  241. $this->removeKey($this->getEmptySibling($parentKey));
  242. }
  243. }
  244. /**
  245. * Ignore a key from a language, or reverse it.
  246. *
  247. * @param string $key
  248. * @param string $language
  249. * @param boolean $reverse
  250. */
  251. public function ignore($key, $language, $reverse = false) {
  252. $value = $this->data[$language][$this->getFilenamePrefix($key)][$key];
  253. if ($reverse) {
  254. $value->markAsIgnore();
  255. } else {
  256. $value->unmarkAsIgnore();
  257. }
  258. }
  259. /**
  260. * Ignore all unmodified keys from a language, or reverse it.
  261. *
  262. * @param string $language
  263. * @param boolean $reverse
  264. */
  265. public function ignore_unmodified($language, $reverse = false) {
  266. $my_language = $this->getLanguage($language);
  267. foreach ($this->getReferenceLanguage() as $file => $ref_language) {
  268. foreach ($ref_language as $key => $ref_value) {
  269. if (array_key_exists($key, $my_language[$file])) {
  270. if($ref_value->equal($my_language[$file][$key])) {
  271. $this->ignore($key, $language, $reverse);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. public function getLanguage($language) {
  278. return $this->data[$language];
  279. }
  280. public function getReferenceLanguage() {
  281. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  282. }
  283. /**
  284. * @param string $key
  285. * @return string
  286. */
  287. private function getFilenamePrefix($key) {
  288. return preg_replace('/\..*/', '.php', $key);
  289. }
  290. }