PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SafeForKids/vendor/laravel/framework/src/Illuminate/Translation/Translator.php

https://gitlab.com/rocs/Streaming-Safe-for-Kids
PHP | 416 lines | 164 code | 50 blank | 202 comment | 9 complexity | 188e2190f36715b21ccfb6ab80a89a2f MD5 | raw file
  1. <?php
  2. namespace Illuminate\Translation;
  3. use Countable;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Traits\Macroable;
  8. use Illuminate\Support\NamespacedItemResolver;
  9. use Symfony\Component\Translation\MessageSelector;
  10. use Symfony\Component\Translation\TranslatorInterface;
  11. class Translator extends NamespacedItemResolver implements TranslatorInterface
  12. {
  13. use Macroable;
  14. /**
  15. * The loader implementation.
  16. *
  17. * @var \Illuminate\Translation\LoaderInterface
  18. */
  19. protected $loader;
  20. /**
  21. * The default locale being used by the translator.
  22. *
  23. * @var string
  24. */
  25. protected $locale;
  26. /**
  27. * The fallback locale used by the translator.
  28. *
  29. * @var string
  30. */
  31. protected $fallback;
  32. /**
  33. * The array of loaded translation groups.
  34. *
  35. * @var array
  36. */
  37. protected $loaded = [];
  38. /**
  39. * The message selector.
  40. *
  41. * @var \Symfony\Component\Translation\MessageSelector
  42. */
  43. protected $selector;
  44. /**
  45. * Create a new translator instance.
  46. *
  47. * @param \Illuminate\Translation\LoaderInterface $loader
  48. * @param string $locale
  49. * @return void
  50. */
  51. public function __construct(LoaderInterface $loader, $locale)
  52. {
  53. $this->loader = $loader;
  54. $this->locale = $locale;
  55. }
  56. /**
  57. * Determine if a translation exists for a given locale.
  58. *
  59. * @param string $key
  60. * @param string|null $locale
  61. * @return bool
  62. */
  63. public function hasForLocale($key, $locale = null)
  64. {
  65. return $this->has($key, $locale, false);
  66. }
  67. /**
  68. * Determine if a translation exists.
  69. *
  70. * @param string $key
  71. * @param string|null $locale
  72. * @param bool $fallback
  73. * @return bool
  74. */
  75. public function has($key, $locale = null, $fallback = true)
  76. {
  77. return $this->get($key, [], $locale, $fallback) !== $key;
  78. }
  79. /**
  80. * Get the translation for the given key.
  81. *
  82. * @param string $key
  83. * @param array $replace
  84. * @param string|null $locale
  85. * @param bool $fallback
  86. * @return string|array|null
  87. */
  88. public function get($key, array $replace = [], $locale = null, $fallback = true)
  89. {
  90. list($namespace, $group, $item) = $this->parseKey($key);
  91. // Here we will get the locale that should be used for the language line. If one
  92. // was not passed, we will use the default locales which was given to us when
  93. // the translator was instantiated. Then, we can load the lines and return.
  94. $locales = $fallback ? $this->parseLocale($locale) : [$locale ?: $this->locale];
  95. foreach ($locales as $locale) {
  96. $this->load($namespace, $group, $locale);
  97. $line = $this->getLine(
  98. $namespace, $group, $locale, $item, $replace
  99. );
  100. if (! is_null($line)) {
  101. break;
  102. }
  103. }
  104. // If the line doesn't exist, we will return back the key which was requested as
  105. // that will be quick to spot in the UI if language keys are wrong or missing
  106. // from the application's language files. Otherwise we can return the line.
  107. if (! isset($line)) {
  108. return $key;
  109. }
  110. return $line;
  111. }
  112. /**
  113. * Add translation lines to the given locale.
  114. *
  115. * @param array $lines
  116. * @param string $locale
  117. * @param string $namespace
  118. * @return void
  119. */
  120. public function addLines(array $lines, $locale, $namespace = '*')
  121. {
  122. foreach ($lines as $key => $value) {
  123. list($group, $item) = explode('.', $key, 2);
  124. Arr::set($this->loaded, "$namespace.$group.$locale.$item", $value);
  125. }
  126. }
  127. /**
  128. * Retrieve a language line out the loaded array.
  129. *
  130. * @param string $namespace
  131. * @param string $group
  132. * @param string $locale
  133. * @param string $item
  134. * @param array $replace
  135. * @return string|array|null
  136. */
  137. protected function getLine($namespace, $group, $locale, $item, array $replace)
  138. {
  139. $line = Arr::get($this->loaded[$namespace][$group][$locale], $item);
  140. if (is_string($line)) {
  141. return $this->makeReplacements($line, $replace);
  142. } elseif (is_array($line) && count($line) > 0) {
  143. return $line;
  144. }
  145. }
  146. /**
  147. * Make the place-holder replacements on a line.
  148. *
  149. * @param string $line
  150. * @param array $replace
  151. * @return string
  152. */
  153. protected function makeReplacements($line, array $replace)
  154. {
  155. $replace = $this->sortReplacements($replace);
  156. foreach ($replace as $key => $value) {
  157. $line = str_replace(
  158. [':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
  159. [$value, Str::upper($value), Str::ucfirst($value)],
  160. $line
  161. );
  162. }
  163. return $line;
  164. }
  165. /**
  166. * Sort the replacements array.
  167. *
  168. * @param array $replace
  169. * @return array
  170. */
  171. protected function sortReplacements(array $replace)
  172. {
  173. return (new Collection($replace))->sortBy(function ($value, $key) {
  174. return mb_strlen($key) * -1;
  175. })->all();
  176. }
  177. /**
  178. * Get a translation according to an integer value.
  179. *
  180. * @param string $key
  181. * @param int|array|\Countable $number
  182. * @param array $replace
  183. * @param string $locale
  184. * @return string
  185. */
  186. public function choice($key, $number, array $replace = [], $locale = null)
  187. {
  188. $line = $this->get($key, $replace, $locale = $locale ?: $this->locale ?: $this->fallback);
  189. if (is_array($number) || $number instanceof Countable) {
  190. $number = count($number);
  191. }
  192. $replace['count'] = $number;
  193. return $this->makeReplacements($this->getSelector()->choose($line, $number, $locale), $replace);
  194. }
  195. /**
  196. * Get the translation for a given key.
  197. *
  198. * @param string $id
  199. * @param array $parameters
  200. * @param string $domain
  201. * @param string $locale
  202. * @return string|array|null
  203. */
  204. public function trans($id, array $parameters = [], $domain = 'messages', $locale = null)
  205. {
  206. return $this->get($id, $parameters, $locale);
  207. }
  208. /**
  209. * Get a translation according to an integer value.
  210. *
  211. * @param string $id
  212. * @param int|array|\Countable $number
  213. * @param array $parameters
  214. * @param string $domain
  215. * @param string $locale
  216. * @return string
  217. */
  218. public function transChoice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
  219. {
  220. return $this->choice($id, $number, $parameters, $locale);
  221. }
  222. /**
  223. * Load the specified language group.
  224. *
  225. * @param string $namespace
  226. * @param string $group
  227. * @param string $locale
  228. * @return void
  229. */
  230. public function load($namespace, $group, $locale)
  231. {
  232. if ($this->isLoaded($namespace, $group, $locale)) {
  233. return;
  234. }
  235. // The loader is responsible for returning the array of language lines for the
  236. // given namespace, group, and locale. We'll set the lines in this array of
  237. // lines that have already been loaded so that we can easily access them.
  238. $lines = $this->loader->load($locale, $group, $namespace);
  239. $this->loaded[$namespace][$group][$locale] = $lines;
  240. }
  241. /**
  242. * Determine if the given group has been loaded.
  243. *
  244. * @param string $namespace
  245. * @param string $group
  246. * @param string $locale
  247. * @return bool
  248. */
  249. protected function isLoaded($namespace, $group, $locale)
  250. {
  251. return isset($this->loaded[$namespace][$group][$locale]);
  252. }
  253. /**
  254. * Add a new namespace to the loader.
  255. *
  256. * @param string $namespace
  257. * @param string $hint
  258. * @return void
  259. */
  260. public function addNamespace($namespace, $hint)
  261. {
  262. $this->loader->addNamespace($namespace, $hint);
  263. }
  264. /**
  265. * Parse a key into namespace, group, and item.
  266. *
  267. * @param string $key
  268. * @return array
  269. */
  270. public function parseKey($key)
  271. {
  272. $segments = parent::parseKey($key);
  273. if (is_null($segments[0])) {
  274. $segments[0] = '*';
  275. }
  276. return $segments;
  277. }
  278. /**
  279. * Get the array of locales to be checked.
  280. *
  281. * @param string|null $locale
  282. * @return array
  283. */
  284. protected function parseLocale($locale)
  285. {
  286. return array_filter([$locale ?: $this->locale, $this->fallback]);
  287. }
  288. /**
  289. * Get the message selector instance.
  290. *
  291. * @return \Symfony\Component\Translation\MessageSelector
  292. */
  293. public function getSelector()
  294. {
  295. if (! isset($this->selector)) {
  296. $this->selector = new MessageSelector;
  297. }
  298. return $this->selector;
  299. }
  300. /**
  301. * Set the message selector instance.
  302. *
  303. * @param \Symfony\Component\Translation\MessageSelector $selector
  304. * @return void
  305. */
  306. public function setSelector(MessageSelector $selector)
  307. {
  308. $this->selector = $selector;
  309. }
  310. /**
  311. * Get the language line loader implementation.
  312. *
  313. * @return \Illuminate\Translation\LoaderInterface
  314. */
  315. public function getLoader()
  316. {
  317. return $this->loader;
  318. }
  319. /**
  320. * Get the default locale being used.
  321. *
  322. * @return string
  323. */
  324. public function locale()
  325. {
  326. return $this->getLocale();
  327. }
  328. /**
  329. * Get the default locale being used.
  330. *
  331. * @return string
  332. */
  333. public function getLocale()
  334. {
  335. return $this->locale;
  336. }
  337. /**
  338. * Set the default locale.
  339. *
  340. * @param string $locale
  341. * @return void
  342. */
  343. public function setLocale($locale)
  344. {
  345. $this->locale = $locale;
  346. }
  347. /**
  348. * Get the fallback locale being used.
  349. *
  350. * @return string
  351. */
  352. public function getFallback()
  353. {
  354. return $this->fallback;
  355. }
  356. /**
  357. * Set the fallback locale being used.
  358. *
  359. * @param string $fallback
  360. * @return void
  361. */
  362. public function setFallback($fallback)
  363. {
  364. $this->fallback = $fallback;
  365. }
  366. }