Language.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Language\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use function App\Core\I18n\_m;
  24. use App\Entity\Actor;
  25. use App\Entity\Note;
  26. use App\Util\Common;
  27. use DateTimeInterface;
  28. use Functional as F;
  29. /**
  30. * Entity for languages
  31. *
  32. * @category DB
  33. * @package GNUsocial
  34. *
  35. * @author Hugo Sales <hugo@hsal.es>
  36. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class Language extends Entity
  40. {
  41. // {{{ Autocode
  42. // @codeCoverageIgnoreStart
  43. private int $id;
  44. private ?string $locale = null;
  45. private ?string $long_display = null;
  46. private ?string $short_display = null;
  47. private DateTimeInterface $created;
  48. public function setId(int $id): self
  49. {
  50. $this->id = $id;
  51. return $this;
  52. }
  53. public function getId(): int
  54. {
  55. return $this->id;
  56. }
  57. public function setLocale(?string $locale): self
  58. {
  59. $this->locale = \is_null($locale) ? null : mb_substr($locale, 0, 64);
  60. return $this;
  61. }
  62. public function getLocale(): ?string
  63. {
  64. return $this->locale;
  65. }
  66. public function setLongDisplay(?string $long_display): self
  67. {
  68. $this->long_display = \is_null($long_display) ? null : mb_substr($long_display, 0, 64);
  69. return $this;
  70. }
  71. public function getLongDisplay(): ?string
  72. {
  73. return $this->long_display;
  74. }
  75. public function setShortDisplay(?string $short_display): self
  76. {
  77. $this->short_display = \is_null($short_display) ? null : mb_substr($short_display, 0, 12);
  78. return $this;
  79. }
  80. public function getShortDisplay(): ?string
  81. {
  82. return $this->short_display;
  83. }
  84. public function setCreated(DateTimeInterface $created): self
  85. {
  86. $this->created = $created;
  87. return $this;
  88. }
  89. public function getCreated(): DateTimeInterface
  90. {
  91. return $this->created;
  92. }
  93. // @codeCoverageIgnoreEnd
  94. // }}} Autocode
  95. public static function getById(int $id): self
  96. {
  97. return Cache::getHashMapKey(
  98. map_key: 'languages-id',
  99. key: (string) $id,
  100. calculate_map: fn () => F\reindex(DB::dql('select l from language l'), fn (self $l) => (string) $l->getId()),
  101. );
  102. }
  103. public static function getByLocale(string $locale): self
  104. {
  105. return Cache::getHashMapKey(
  106. 'languages',
  107. $locale,
  108. calculate_map: fn () => F\reindex(DB::dql('select l from language l'), fn (self $l) => $l->getLocale()),
  109. );
  110. }
  111. public static function getByNote(Note $note): self
  112. {
  113. return self::getById($note->getLanguageId());
  114. }
  115. public static function getLanguageChoices(): array
  116. {
  117. $langs = Cache::getHashMap(
  118. 'languages',
  119. fn () => F\reindex(DB::dql('select l from language l'), fn (self $l) => $l->getLocale()),
  120. );
  121. return array_merge(...F\map(array_values($langs), fn ($l) => $l->toChoiceFormat()));
  122. }
  123. public function toChoiceFormat(): array
  124. {
  125. return [_m($this->getLongDisplay()) => $this->getLocale()];
  126. }
  127. /**
  128. * Get all the available languages as well as the languages $actor
  129. * prefers and are appropriate for posting in/to $context_actor
  130. */
  131. public static function getSortedLanguageChoices(?Actor $actor, ?Actor $context_actor, ?bool $use_short_display): array
  132. {
  133. $language_choices = self::getLanguageChoices();
  134. if (\is_null($actor)) {
  135. return [$language_choices, []];
  136. }
  137. $preferred_language_choices = $actor->getPreferredLanguageChoices($context_actor);
  138. ksort($language_choices);
  139. if ($use_short_display ?? Common::config('posting', 'use_short_language_display')) {
  140. $key = array_key_first($preferred_language_choices);
  141. $language = $preferred_language_choices[$key];
  142. unset($preferred_language_choices[$key], $language_choices[$key]);
  143. $short_display = $language->getShortDisplay();
  144. $preferred_language_choices[$short_display] = ($locale = $language->getLocale());
  145. $language_choices[$short_display] = $locale;
  146. }
  147. return [$language_choices, $preferred_language_choices];
  148. }
  149. public static function schemaDef(): array
  150. {
  151. return [
  152. 'name' => 'language',
  153. 'description' => 'all known languages',
  154. 'fields' => [
  155. 'id' => ['type' => 'serial', 'not null' => true, 'description' => 'unique identifier'],
  156. 'locale' => ['type' => 'varchar', 'length' => 64, 'description' => 'The locale identifier for the language of a note. 2-leter-iso-language-code_4-leter-script-code_2-leter-iso-country-code, but kept longer in case we get a different format'],
  157. 'long_display' => ['type' => 'varchar', 'length' => 64, 'description' => 'The long display string for the language, in english (translated later)'],
  158. 'short_display' => ['type' => 'varchar', 'length' => 12, 'description' => 'The short display string for the language (used for the first option)'],
  159. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  160. ],
  161. 'primary key' => ['id'],
  162. 'unique keys' => [
  163. 'language_locale_uniq' => ['locale'],
  164. ],
  165. 'indexes' => [
  166. 'locale_idx' => ['locale'],
  167. ],
  168. ];
  169. }
  170. }