I18nData.php 10 KB

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