I18nData.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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->addMissingPluralVariantsFromReference();
  10. $this->removeExtraKeysFromOtherLanguages();
  11. $this->processValueStates();
  12. }
  13. private static function isPluralVariantKey(string $key): bool {
  14. return self::parsePluralVariantKey($key) !== null;
  15. }
  16. /**
  17. * @return array{base:string,index:int}|null
  18. */
  19. private static function parsePluralVariantKey(string $key): ?array {
  20. if (preg_match('/^(?P<base>.+)\.(?P<index>\d+)$/', $key, $matches) !== 1) {
  21. return null;
  22. }
  23. return [
  24. 'base' => $matches['base'],
  25. 'index' => (int)$matches['index'],
  26. ];
  27. }
  28. /**
  29. * @return array<string,array<string,array<string,I18nValue>>>
  30. */
  31. public function getData(): array {
  32. return $this->data;
  33. }
  34. private function addMissingKeysFromReference(): void {
  35. $reference = $this->getReferenceLanguage();
  36. $languages = $this->getNonReferenceLanguages();
  37. foreach ($reference as $file => $refValues) {
  38. foreach ($refValues as $key => $refValue) {
  39. if (self::isPluralVariantKey($key)) {
  40. continue;
  41. }
  42. foreach ($languages as $language) {
  43. if (!array_key_exists($file, $this->data[$language]) || !array_key_exists($key, $this->data[$language][$file])) {
  44. $this->data[$language][$file][$key] = clone $refValue;
  45. }
  46. $value = $this->data[$language][$file][$key];
  47. if ($refValue->equal($value) && !$value->isIgnore()) {
  48. $value->markAsTodo();
  49. }
  50. }
  51. }
  52. }
  53. }
  54. private function addMissingPluralVariantsFromReference(): void {
  55. $reference = $this->getReferenceLanguage();
  56. foreach ($this->getNonReferenceLanguages() as $language) {
  57. $expectedIndexes = $this->pluralVariantIndexesForLanguage($language);
  58. foreach ($reference as $file => $refValues) {
  59. $pluralBases = [];
  60. foreach ($refValues as $key => $refValue) {
  61. $parsedKey = self::parsePluralVariantKey($key);
  62. if ($parsedKey === null) {
  63. continue;
  64. }
  65. $pluralBases[$parsedKey['base']] = true;
  66. }
  67. if (!array_key_exists($file, $this->data[$language])) {
  68. $this->data[$language][$file] = [];
  69. }
  70. foreach (array_keys($pluralBases) as $pluralBase) {
  71. foreach ($expectedIndexes as $index) {
  72. $pluralKey = $pluralBase . '.' . $index;
  73. if (array_key_exists($pluralKey, $this->data[$language][$file])) {
  74. continue;
  75. }
  76. $referenceValue = $this->referenceValueForKey($refValues, $pluralKey);
  77. if ($referenceValue === null) {
  78. continue;
  79. }
  80. $this->data[$language][$file][$pluralKey] = clone $referenceValue;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. private function removeExtraKeysFromOtherLanguages(): void {
  87. $reference = $this->getReferenceLanguage();
  88. foreach ($this->getNonReferenceLanguages() as $language) {
  89. foreach ($this->getLanguage($language) as $file => $values) {
  90. foreach ($values as $key => $value) {
  91. if (self::isPluralVariantKey($key)) {
  92. continue;
  93. }
  94. if (!array_key_exists($key, $reference[$file])) {
  95. unset($this->data[$language][$file][$key]);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. private function processValueStates(): void {
  102. $reference = $this->getReferenceLanguage();
  103. $languages = $this->getNonReferenceLanguages();
  104. foreach ($reference as $file => $refValues) {
  105. foreach ($refValues as $key => $refValue) {
  106. foreach ($languages as $language) {
  107. if (!$this->pluralVariantAppliesToLanguage($language, $key)) {
  108. continue;
  109. }
  110. $value = $this->data[$language][$file][$key];
  111. $this->syncValueState($refValue, $value);
  112. }
  113. }
  114. }
  115. foreach ($languages as $language) {
  116. foreach ($this->getLanguage($language) as $file => $values) {
  117. $referenceValues = $reference[$file] ?? [];
  118. foreach ($values as $key => $value) {
  119. if (!self::isPluralVariantKey($key) || array_key_exists($key, $referenceValues)) {
  120. continue;
  121. }
  122. $referenceValue = $this->referenceValueForKey($referenceValues, $key);
  123. if ($referenceValue === null) {
  124. continue;
  125. }
  126. $this->syncValueState($referenceValue, $value);
  127. }
  128. }
  129. }
  130. }
  131. private function syncValueState(I18nValue $referenceValue, I18nValue $value): void {
  132. if ($referenceValue->equal($value) && !$value->isIgnore()) {
  133. $value->markAsTodo();
  134. return;
  135. }
  136. if (!$referenceValue->equal($value) && $value->isTodo()) {
  137. $value->markAsDirty();
  138. }
  139. }
  140. private function pluralVariantAppliesToLanguage(string $language, string $key): bool {
  141. $parsedKey = self::parsePluralVariantKey($key);
  142. if ($parsedKey === null) {
  143. return true;
  144. }
  145. return in_array($parsedKey['index'], $this->pluralVariantIndexesForLanguage($language), true);
  146. }
  147. /**
  148. * @param array<string,I18nValue> $referenceValues
  149. */
  150. private function referenceValueForKey(array $referenceValues, string $key): ?I18nValue {
  151. if (array_key_exists($key, $referenceValues)) {
  152. return $referenceValues[$key];
  153. }
  154. $parsedKey = self::parsePluralVariantKey($key);
  155. if ($parsedKey === null) {
  156. return null;
  157. }
  158. $pluralKey = $parsedKey['base'] . '.1';
  159. if (array_key_exists($pluralKey, $referenceValues)) {
  160. return $referenceValues[$pluralKey];
  161. }
  162. $singularKey = $parsedKey['base'] . '.0';
  163. return $referenceValues[$singularKey] ?? null;
  164. }
  165. /**
  166. * @return list<int>
  167. */
  168. private function pluralVariantIndexesForLanguage(string $language): array {
  169. $pluralCount = $this->pluralCountForLanguage($language);
  170. if ($pluralCount !== null) {
  171. return range(0, $pluralCount - 1);
  172. }
  173. $indexes = [];
  174. foreach ($this->data[$language] as $values) {
  175. foreach (array_keys($values) as $key) {
  176. $parsedKey = self::parsePluralVariantKey($key);
  177. if ($parsedKey === null) {
  178. continue;
  179. }
  180. $indexes[$parsedKey['index']] = true;
  181. }
  182. }
  183. if ($indexes === []) {
  184. return [0];
  185. }
  186. ksort($indexes, SORT_NUMERIC);
  187. return array_map('intval', array_keys($indexes));
  188. }
  189. private function pluralCountForLanguage(string $language): ?int {
  190. if (!defined('I18N_PATH')) {
  191. return null;
  192. }
  193. $pluralFile = I18N_PATH . '/' . $language . '/plurals.php';
  194. if (!is_file($pluralFile)) {
  195. return null;
  196. }
  197. $pluralData = include $pluralFile;
  198. $pluralCount = is_array($pluralData) ? ($pluralData['nplurals'] ?? null) : null;
  199. return is_int($pluralCount) && $pluralCount > 0 ? $pluralCount : null;
  200. }
  201. /**
  202. * Return the available languages
  203. * @return list<string>
  204. */
  205. public function getAvailableLanguages(): array {
  206. $languages = array_keys($this->data);
  207. sort($languages);
  208. return $languages;
  209. }
  210. /**
  211. * Return all available languages without the reference language
  212. * @return list<string>
  213. */
  214. private function getNonReferenceLanguages(): array {
  215. return array_values(array_filter(array_keys($this->data),
  216. static fn(string $value) => static::REFERENCE_LANGUAGE !== $value));
  217. }
  218. /**
  219. * Add a new language. It’s a copy of the reference language.
  220. * @throws Exception
  221. */
  222. public function addLanguage(string $language, ?string $reference = null): void {
  223. if (array_key_exists($language, $this->data)) {
  224. throw new Exception('The selected language already exists.');
  225. }
  226. if (!is_string($reference) || !array_key_exists($reference, $this->data)) {
  227. $reference = static::REFERENCE_LANGUAGE;
  228. }
  229. $this->data[$language] = $this->data[$reference];
  230. }
  231. /**
  232. * Check if the key is known.
  233. */
  234. public function isKnown(string $key): bool {
  235. return $this->exists($key) &&
  236. array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  237. }
  238. /**
  239. * Check if the file exists
  240. */
  241. public function exists(string $file): bool {
  242. return array_key_exists($this->getFilenamePrefix($file), $this->data[static::REFERENCE_LANGUAGE]);
  243. }
  244. /**
  245. * Return the parent key for a specified key.
  246. * To get the parent key, you need to remove the last section of the key. Each
  247. * is separated into sections. The parent of a section is the concatenation of
  248. * all sections before the selected key. For instance, if the key is 'a.b.c.d.e',
  249. * the parent key is 'a.b.c.d'.
  250. */
  251. private function getParentKey(string $key): string {
  252. return substr($key, 0, strrpos($key, '.') ?: null);
  253. }
  254. /**
  255. * Return the siblings for a specified key.
  256. * To get the siblings, we need to find all matches with the parent.
  257. *
  258. * @return list<string>
  259. */
  260. private function getSiblings(string $key): array {
  261. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
  262. return [];
  263. }
  264. $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  265. $parent = $this->getParentKey($key);
  266. return array_values(array_filter($keys, static fn(string $element) => str_contains($element, $parent)));
  267. }
  268. /**
  269. * Check if the key is an only child.
  270. * To be an only child, there must be only one sibling and that sibling must
  271. * be the empty sibling. The empty sibling is the parent.
  272. */
  273. private function isOnlyChild(string $key): bool {
  274. $siblings = $this->getSiblings($key);
  275. if (1 !== count($siblings)) {
  276. return false;
  277. }
  278. return '_' === $siblings[0][-1];
  279. }
  280. /**
  281. * Return the parent key as an empty sibling.
  282. * When a key has children, it cannot have its value directly. The value
  283. * needs to be attached to an empty sibling represented by "_".
  284. */
  285. private function getEmptySibling(string $key): string {
  286. return "{$key}._";
  287. }
  288. /**
  289. * Check if a key is a parent key.
  290. * To be a parent key, there must be at least one key starting with the key
  291. * under test. Of course, it cannot be itself.
  292. */
  293. private function isParent(string $key): bool {
  294. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
  295. return false;
  296. }
  297. $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
  298. $children = array_values(array_filter($keys, static function (string $element) use ($key) {
  299. if ($element === $key) {
  300. return false;
  301. }
  302. return str_contains($element, $key);
  303. }));
  304. return count($children) !== 0;
  305. }
  306. /**
  307. * Add a new translation file to all languages
  308. * @throws Exception
  309. */
  310. public function addFile(string $file): void {
  311. $file = strtolower($file);
  312. if (!str_ends_with($file, '.php')) {
  313. throw new Exception('The selected file name is not supported.');
  314. }
  315. if ($this->exists($file)) {
  316. throw new Exception('The selected file exists already.');
  317. }
  318. foreach ($this->getAvailableLanguages() as $language) {
  319. $this->data[$language][$this->getFilenamePrefix($file)] = [];
  320. }
  321. }
  322. /**
  323. * Add a new key to all languages.
  324. * @throws Exception
  325. */
  326. public function addKey(string $key, string $value): void {
  327. if ($this->isParent($key)) {
  328. $key = $this->getEmptySibling($key);
  329. }
  330. if ($this->isKnown($key)) {
  331. throw new Exception('The selected key already exists.');
  332. }
  333. $parentKey = $this->getParentKey($key);
  334. if ($this->isKnown($parentKey)) {
  335. // The parent key exists, that means that we need to convert it to an array.
  336. // To create an array, we need to change the key by appending an empty section.
  337. foreach ($this->getAvailableLanguages() as $language) {
  338. $parentValue = $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey];
  339. $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)] =
  340. new I18nValue($parentValue);
  341. }
  342. }
  343. $value = new I18nValue($value);
  344. $value->markAsTodo();
  345. foreach ($this->getAvailableLanguages() as $language) {
  346. if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  347. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  348. }
  349. }
  350. if ($this->isKnown($parentKey)) {
  351. $this->removeKey($parentKey);
  352. }
  353. }
  354. /**
  355. * Move an existing key into a new location
  356. * @throws Exception
  357. */
  358. public function moveKey(string $key, string $newKey): void {
  359. if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
  360. throw new Exception('The selected key does not exist');
  361. }
  362. if ($this->isKnown($newKey)) {
  363. throw new Exception('Cannot move key to a location that already exists.');
  364. }
  365. $keyPrefix = $this->isParent($key) ? $key . '.' : $key;
  366. foreach ($this->getAvailableLanguages() as $language) {
  367. foreach ($this->data[$language][$this->getFilenamePrefix($key)] as $k => $v) {
  368. if (str_starts_with($k, $keyPrefix)) {
  369. $this->data[$language][$this->getFilenamePrefix($newKey)][str_replace($key, $newKey, $k)] = $v;
  370. unset($this->data[$language][$this->getFilenamePrefix($key)][$k]);
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * Add a value for a key for the selected language.
  377. *
  378. * @throws Exception
  379. */
  380. public function addValue(string $key, string $value, string $language): void {
  381. if (!in_array($language, $this->getAvailableLanguages(), true)) {
  382. throw new Exception('The selected language does not exist.');
  383. }
  384. if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) ||
  385. !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) {
  386. throw new Exception('The selected key does not exist for the selected language.');
  387. }
  388. $value = new I18nValue($value);
  389. if (static::REFERENCE_LANGUAGE === $language) {
  390. $previousValue = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key];
  391. foreach ($this->getAvailableLanguages() as $lang) {
  392. $currentValue = $this->data[$lang][$this->getFilenamePrefix($key)][$key];
  393. if ($currentValue->equal($previousValue)) {
  394. $this->data[$lang][$this->getFilenamePrefix($key)][$key] = $value;
  395. }
  396. }
  397. } else {
  398. $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value;
  399. }
  400. }
  401. /**
  402. * Remove a key in all languages
  403. */
  404. public function removeKey(string $key): void {
  405. if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
  406. throw new Exception('The selected key does not exist.');
  407. }
  408. if (!$this->isKnown($key)) {
  409. // The key has children, it needs to be appended with an empty section.
  410. $key = $this->getEmptySibling($key);
  411. }
  412. foreach ($this->getAvailableLanguages() as $language) {
  413. if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) {
  414. unset($this->data[$language][$this->getFilenamePrefix($key)][$key]);
  415. }
  416. }
  417. if ($this->isOnlyChild($key)) {
  418. $parentKey = $this->getParentKey($key);
  419. foreach ($this->getAvailableLanguages() as $language) {
  420. $parentValue = $this->data[$language][$this->getFilenamePrefix($this->getEmptySibling($parentKey))][$this->getEmptySibling($parentKey)];
  421. $this->data[$language][$this->getFilenamePrefix($parentKey)][$parentKey] = $parentValue;
  422. }
  423. $this->removeKey($this->getEmptySibling($parentKey));
  424. }
  425. }
  426. /**
  427. * Ignore a key from a language, or revert an existing ignore on a key.
  428. */
  429. public function ignore(string $key, string $language, bool $revert = false): void {
  430. $value = $this->data[$language][$this->getFilenamePrefix($key)][$key];
  431. if ($revert) {
  432. $value->unmarkAsIgnore();
  433. } else {
  434. $value->markAsIgnore();
  435. }
  436. }
  437. /**
  438. * Ignore all unmodified keys from a language, or revert all existing ignores on unmodified keys.
  439. */
  440. public function ignore_unmodified(string $language, bool $revert = false): void {
  441. $my_language = $this->getLanguage($language);
  442. foreach ($this->getReferenceLanguage() as $file => $ref_language) {
  443. foreach ($ref_language as $key => $ref_value) {
  444. if (array_key_exists($key, $my_language[$file])) {
  445. if ($ref_value->equal($my_language[$file][$key])) {
  446. $this->ignore($key, $language, $revert);
  447. }
  448. }
  449. }
  450. }
  451. }
  452. /**
  453. * @return array<string,array<string,I18nValue>>
  454. */
  455. public function getLanguage(string $language): array {
  456. return $this->data[$language];
  457. }
  458. /**
  459. * @return array<string,array<string,I18nValue>>
  460. */
  461. public function getReferenceLanguage(): array {
  462. return $this->getLanguage(static::REFERENCE_LANGUAGE);
  463. }
  464. private function getFilenamePrefix(string $key): string {
  465. return preg_replace('/\..*/', '.php', $key) ?? '';
  466. }
  467. }