/concrete/vendor/gettext/gettext/src/Translation.php

https://gitlab.com/koodersmiikka/operaatio-terveys · PHP · 479 lines · 222 code · 56 blank · 201 comment · 20 complexity · db0af9884c19eab7e4c3df000dbdf506 MD5 · raw file

  1. <?php
  2. namespace Gettext;
  3. /**
  4. * Class to manage a translation string.
  5. */
  6. class Translation
  7. {
  8. protected $context;
  9. protected $original;
  10. protected $translation = '';
  11. protected $plural;
  12. protected $pluralTranslation = array();
  13. protected $references = array();
  14. protected $comments = array();
  15. protected $extractedComments = array();
  16. protected $flags = array();
  17. protected $translationCount;
  18. /**
  19. * Generates the id of a translation (context + glue + original).
  20. *
  21. * @param string $context
  22. * @param string $original
  23. *
  24. * @return string
  25. */
  26. public static function generateId($context, $original)
  27. {
  28. return "{$context}\004{$original}";
  29. }
  30. /**
  31. * Construct.
  32. *
  33. * @param string $context The context of the translation
  34. * @param string $original The original string
  35. * @param string $plural The original plural string
  36. */
  37. public function __construct($context, $original, $plural = '')
  38. {
  39. $this->context = (string) $context;
  40. $this->original = (string) $original;
  41. $this->setPlural($plural);
  42. }
  43. /**
  44. * Clones this translation.
  45. *
  46. * @param null|string $context Optional new context
  47. * @param null|string $original Optional new original
  48. */
  49. public function getClone($context = null, $original = null)
  50. {
  51. $new = clone $this;
  52. if ($context !== null) {
  53. $new->context = (string) $context;
  54. }
  55. if ($original !== null) {
  56. $new->original = (string) $original;
  57. }
  58. return $new;
  59. }
  60. /**
  61. * Returns the id of this translation.
  62. *
  63. * @return string
  64. */
  65. public function getId()
  66. {
  67. return static::generateId($this->context, $this->original);
  68. }
  69. /**
  70. * Checks whether the translation matches with the arguments.
  71. *
  72. * @param string $context
  73. * @param string $original
  74. *
  75. * @return bool
  76. */
  77. public function is($context, $original = '')
  78. {
  79. return (($this->context === $context) && ($this->original === $original)) ? true : false;
  80. }
  81. /**
  82. * Gets the original string.
  83. *
  84. * @return string
  85. */
  86. public function getOriginal()
  87. {
  88. return $this->original;
  89. }
  90. /**
  91. * Checks if the original string is empty or not.
  92. *
  93. * @return bool
  94. */
  95. public function hasOriginal()
  96. {
  97. return ($this->original !== '') ? true : false;
  98. }
  99. /**
  100. * Sets the translation string.
  101. *
  102. * @param string $translation
  103. */
  104. public function setTranslation($translation)
  105. {
  106. $this->translation = (string) $translation;
  107. }
  108. /**
  109. * Gets the translation string.
  110. *
  111. * @return string
  112. */
  113. public function getTranslation()
  114. {
  115. return $this->translation;
  116. }
  117. /**
  118. * Checks if the translation string is empty or not.
  119. *
  120. * @return bool
  121. */
  122. public function hasTranslation()
  123. {
  124. return ($this->translation !== '') ? true : false;
  125. }
  126. /**
  127. * Sets the plural translation string.
  128. *
  129. * @param string $plural
  130. */
  131. public function setPlural($plural)
  132. {
  133. $this->plural = (string) $plural;
  134. $this->normalizeTranslationCount();
  135. }
  136. /**
  137. * Gets the plural translation string.
  138. *
  139. * @return string
  140. */
  141. public function getPlural()
  142. {
  143. return $this->plural;
  144. }
  145. /**
  146. * Checks if the plural translation string is empty or not.
  147. *
  148. * @return bool
  149. */
  150. public function hasPlural()
  151. {
  152. return ($this->plural !== '') ? true : false;
  153. }
  154. /**
  155. * Set a new plural translation.
  156. *
  157. * @param string $plural The plural string to add
  158. * @param int $key The key of the plural translation.
  159. */
  160. public function setPluralTranslation($plural, $key = 0)
  161. {
  162. $this->pluralTranslation[$key] = $plural;
  163. $this->normalizeTranslationCount();
  164. }
  165. /**
  166. * Gets one or all plural translations.
  167. *
  168. * @param int|null $key The key to return. If is null, return all translations
  169. *
  170. * @return string|array
  171. */
  172. public function getPluralTranslation($key = null)
  173. {
  174. if ($key === null) {
  175. return $this->pluralTranslation;
  176. }
  177. return isset($this->pluralTranslation[$key]) ? (string) $this->pluralTranslation[$key] : '';
  178. }
  179. /**
  180. * Checks if there are any plural translation.
  181. *
  182. * @return bool
  183. */
  184. public function hasPluralTranslation()
  185. {
  186. return implode('', $this->pluralTranslation) !== '';
  187. }
  188. /**
  189. * Removes all plural translations.
  190. */
  191. public function deletePluralTranslation()
  192. {
  193. $this->pluralTranslation = array();
  194. $this->normalizeTranslationCount();
  195. }
  196. /**
  197. * Set the number of singular + plural translations allowed.
  198. *
  199. * @param int $count
  200. */
  201. public function setTranslationCount($count)
  202. {
  203. $this->translationCount = is_null($count) ? null : intval($count);
  204. $this->normalizeTranslationCount();
  205. }
  206. /**
  207. * Returns the number of singular + plural translations
  208. * Returns null if this Translation is not a plural one.
  209. *
  210. * @return int|null
  211. */
  212. public function getTranslationCount()
  213. {
  214. return $this->hasPlural() ? $this->translationCount : null;
  215. }
  216. /**
  217. * Normalizes the translation count.
  218. */
  219. protected function normalizeTranslationCount()
  220. {
  221. if ($this->translationCount === null) {
  222. return;
  223. }
  224. if ($this->hasPlural()) {
  225. $allowed = $this->translationCount - 1;
  226. $current = count($this->pluralTranslation);
  227. if ($allowed > $current) {
  228. $this->pluralTranslation = $this->pluralTranslation + array_fill(0, $allowed, '');
  229. } elseif ($current > $allowed) {
  230. $this->pluralTranslation = array_slice($this->pluralTranslation, 0, $allowed);
  231. }
  232. } else {
  233. $this->pluralTranslation = array();
  234. }
  235. }
  236. /**
  237. * Gets the context of this translation.
  238. *
  239. * @return string
  240. */
  241. public function getContext()
  242. {
  243. return $this->context;
  244. }
  245. /**
  246. * Checks if the context is empty or not.
  247. *
  248. * @return bool
  249. */
  250. public function hasContext()
  251. {
  252. return (isset($this->context) && ($this->context !== '')) ? true : false;
  253. }
  254. /**
  255. * Adds a new reference for this translation.
  256. *
  257. * @param string $filename The file path where the translation has been found
  258. * @param null|int $line The line number where the translation has been found
  259. */
  260. public function addReference($filename, $line = null)
  261. {
  262. $key = "{$filename}:{$line}";
  263. $this->references[$key] = array($filename, $line);
  264. }
  265. /**
  266. * Checks if the translation has any reference.
  267. *
  268. * @return bool
  269. */
  270. public function hasReferences()
  271. {
  272. return !empty($this->references);
  273. }
  274. /**
  275. * Return all references for this translation.
  276. *
  277. * @return array
  278. */
  279. public function getReferences()
  280. {
  281. return array_values($this->references);
  282. }
  283. /**
  284. * Removes all references.
  285. */
  286. public function deleteReferences()
  287. {
  288. $this->references = array();
  289. }
  290. /**
  291. * Adds a new comment for this translation.
  292. *
  293. * @param string $comment
  294. */
  295. public function addComment($comment)
  296. {
  297. $this->comments[] = $comment;
  298. }
  299. /**
  300. * Checks if the translation has any comment.
  301. *
  302. * @return bool
  303. */
  304. public function hasComments()
  305. {
  306. return isset($this->comments[0]);
  307. }
  308. /**
  309. * Returns all comments for this translation.
  310. *
  311. * @return array
  312. */
  313. public function getComments()
  314. {
  315. return $this->comments;
  316. }
  317. /**
  318. * Removes all comments.
  319. */
  320. public function deleteComments()
  321. {
  322. $this->comments = array();
  323. }
  324. /**
  325. * Adds a new extracted comment for this translation.
  326. *
  327. * @param string $comment
  328. */
  329. public function addExtractedComment($comment)
  330. {
  331. $this->extractedComments[] = $comment;
  332. }
  333. /**
  334. * Checks if the translation has any extracted comment.
  335. *
  336. * @return bool
  337. */
  338. public function hasExtractedComments()
  339. {
  340. return isset($this->extractedComments[0]);
  341. }
  342. /**
  343. * Returns all extracted comments for this translation.
  344. *
  345. * @return array
  346. */
  347. public function getExtractedComments()
  348. {
  349. return $this->extractedComments;
  350. }
  351. /**
  352. * Removes all extracted comments.
  353. */
  354. public function deleteExtractedComments()
  355. {
  356. $this->extractedComments = array();
  357. }
  358. /**
  359. * Adds a new flat for this translation.
  360. *
  361. * @param string $flag
  362. */
  363. public function addFlag($flag)
  364. {
  365. $this->flags[] = $flag;
  366. }
  367. /**
  368. * Checks if the translation has any flag.
  369. *
  370. * @return bool
  371. */
  372. public function hasFlags()
  373. {
  374. return isset($this->flags[0]);
  375. }
  376. /**
  377. * Returns all extracted flags for this translation.
  378. *
  379. * @return array
  380. */
  381. public function getFlags()
  382. {
  383. return $this->flags;
  384. }
  385. /**
  386. * Removes all flags.
  387. */
  388. public function deleteFlags()
  389. {
  390. $this->flags = array();
  391. }
  392. /**
  393. * Merges this translation with other translation.
  394. *
  395. * @param Translation $translation The translation to merge with
  396. * @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations
  397. */
  398. public function mergeWith(Translation $translation, $method = null)
  399. {
  400. if ($method === null) {
  401. $method = Translations::$mergeDefault;
  402. }
  403. if (!$this->hasTranslation() || ($translation->hasTranslation() && ($method & Translations::MERGE_OVERRIDE))) {
  404. $this->setTranslation($translation->getTranslation());
  405. }
  406. if (($method & Translations::MERGE_PLURAL) && !$this->hasPlural()) {
  407. $this->setPlural($translation->getPlural());
  408. }
  409. if ($this->hasPlural() && !$this->hasPluralTranslation() && $translation->hasPluralTranslation()) {
  410. $this->pluralTranslation = $translation->getPluralTranslation();
  411. }
  412. if ($method & Translations::MERGE_REFERENCES) {
  413. foreach ($translation->getReferences() as $reference) {
  414. $this->addReference($reference[0], $reference[1]);
  415. }
  416. }
  417. if ($method & Translations::MERGE_COMMENTS) {
  418. $this->comments = array_values(array_unique(array_merge($translation->getComments(), $this->comments)));
  419. $this->extractedComments = array_values(array_unique(array_merge($translation->getExtractedComments(), $this->extractedComments)));
  420. $this->flags = array_values(array_unique(array_merge($translation->getFlags(), $this->flags)));
  421. }
  422. }
  423. }