FileLoader.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Illuminate\Translation;
  3. use RuntimeException;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Contracts\Translation\Loader;
  6. class FileLoader implements Loader
  7. {
  8. /**
  9. * The filesystem instance.
  10. *
  11. * @var \Illuminate\Filesystem\Filesystem
  12. */
  13. protected $files;
  14. /**
  15. * The default path for the loader.
  16. *
  17. * @var string
  18. */
  19. protected $path;
  20. /**
  21. * All of the registered paths to JSON translation files.
  22. *
  23. * @var string
  24. */
  25. protected $jsonPaths = [];
  26. /**
  27. * All of the namespace hints.
  28. *
  29. * @var array
  30. */
  31. protected $hints = [];
  32. /**
  33. * Create a new file loader instance.
  34. *
  35. * @param \Illuminate\Filesystem\Filesystem $files
  36. * @param string $path
  37. * @return void
  38. */
  39. public function __construct(Filesystem $files, $path)
  40. {
  41. $this->path = $path;
  42. $this->files = $files;
  43. }
  44. /**
  45. * Load the messages for the given locale.
  46. *
  47. * @param string $locale
  48. * @param string $group
  49. * @param string $namespace
  50. * @return array
  51. */
  52. public function load($locale, $group, $namespace = null)
  53. {
  54. if ($group == '*' && $namespace == '*') {
  55. return $this->loadJsonPaths($locale);
  56. }
  57. if (is_null($namespace) || $namespace == '*') {
  58. return $this->loadPath($this->path, $locale, $group);
  59. }
  60. return $this->loadNamespaced($locale, $group, $namespace);
  61. }
  62. /**
  63. * Load a namespaced translation group.
  64. *
  65. * @param string $locale
  66. * @param string $group
  67. * @param string $namespace
  68. * @return array
  69. */
  70. protected function loadNamespaced($locale, $group, $namespace)
  71. {
  72. if (isset($this->hints[$namespace])) {
  73. $lines = $this->loadPath($this->hints[$namespace], $locale, $group);
  74. return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
  75. }
  76. return [];
  77. }
  78. /**
  79. * Load a local namespaced translation group for overrides.
  80. *
  81. * @param array $lines
  82. * @param string $locale
  83. * @param string $group
  84. * @param string $namespace
  85. * @return array
  86. */
  87. protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
  88. {
  89. $file = "{$this->path}/vendor/{$namespace}/{$locale}/{$group}.php";
  90. if ($this->files->exists($file)) {
  91. return array_replace_recursive($lines, $this->files->getRequire($file));
  92. }
  93. return $lines;
  94. }
  95. /**
  96. * Load a locale from a given path.
  97. *
  98. * @param string $path
  99. * @param string $locale
  100. * @param string $group
  101. * @return array
  102. */
  103. protected function loadPath($path, $locale, $group)
  104. {
  105. if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
  106. return $this->files->getRequire($full);
  107. }
  108. return [];
  109. }
  110. /**
  111. * Load a locale from the given JSON file path.
  112. *
  113. * @param string $locale
  114. * @return array
  115. *
  116. * @throws \RuntimeException
  117. */
  118. protected function loadJsonPaths($locale)
  119. {
  120. return collect(array_merge($this->jsonPaths, [$this->path]))
  121. ->reduce(function ($output, $path) use ($locale) {
  122. if ($this->files->exists($full = "{$path}/{$locale}.json")) {
  123. $decoded = json_decode($this->files->get($full), true);
  124. if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
  125. throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
  126. }
  127. $output = array_merge($output, $decoded);
  128. }
  129. return $output;
  130. }, []);
  131. }
  132. /**
  133. * Add a new namespace to the loader.
  134. *
  135. * @param string $namespace
  136. * @param string $hint
  137. * @return void
  138. */
  139. public function addNamespace($namespace, $hint)
  140. {
  141. $this->hints[$namespace] = $hint;
  142. }
  143. /**
  144. * Add a new JSON path to the loader.
  145. *
  146. * @param string $path
  147. * @return void
  148. */
  149. public function addJsonPath($path)
  150. {
  151. $this->jsonPaths[] = $path;
  152. }
  153. /**
  154. * Get an array of all the registered namespaces.
  155. *
  156. * @return array
  157. */
  158. public function namespaces()
  159. {
  160. return $this->hints;
  161. }
  162. }