I18nData.php 10 KB

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