I18nData.php 10 KB

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