FileLoader.php
TLDR
This file, FileLoader.php
, is a class in the Illuminate\Translation
namespace. It implements the Loader
interface and is responsible for loading translation files. It provides methods to load translation messages for a given locale, group, and namespace, as well as to add namespaces and JSON paths to the loader.
Methods
load
This method loads the messages for the given locale, group, and namespace. If both the group and namespace are "", it calls the loadJsonPaths
method. If the namespace is null or "", it calls the loadPaths
method with the default paths and returns the loaded messages as an array. Otherwise, it calls the loadNamespaced
method.
loadNamespaced
This protected method loads a namespaced translation group. If the namespace is registered in the hints
array, it calls the loadPaths
method with the path for the namespace and returns the loaded lines. Otherwise, it returns an empty array.
loadNamespaceOverrides
This protected method loads a local namespaced translation group for overrides. It iterates over the registered paths and checks if a translation file exists for the given locale, group, and namespace. If the file exists, it merges its content with the passed lines array and returns the merged lines.
loadPaths
This protected method loads a locale from a given path. It iterates over the passed paths and checks if a translation file exists for the given locale and group. If the file exists, it merges its content with the passed output array and returns the merged output.
loadJsonPaths
This protected method loads a locale from the given JSON file path. It iterates over the registered JSON paths and checks if a JSON file exists for the given locale. If the file exists, it reads its content, decodes the JSON, merges the decoded content with the passed output array, and returns the merged output. If the JSON structure is invalid, it throws a RuntimeException
.
addNamespace
This method adds a new namespace to the loader. It takes a namespace and a hint and registers them in the hints
array.
namespaces
This method returns an array of all the registered namespaces.
addJsonPath
This method adds a new JSON path to the loader. It takes a path and appends it to the jsonPaths
array.
jsonPaths
This method returns an array of all the registered paths to JSON translation files.
Classes
There are no other classes in this file.
<?php
namespace Illuminate\Translation;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
class FileLoader implements Loader
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The default paths for the loader.
*
* @var array
*/
protected $paths;
/**
* All of the registered paths to JSON translation files.
*
* @var array
*/
protected $jsonPaths = [];
/**
* All of the namespace hints.
*
* @var array
*/
protected $hints = [];
/**
* Create a new file loader instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param array|string $path
* @return void
*/
public function __construct(Filesystem $files, array|string $path)
{
$this->files = $files;
$this->paths = is_string($path) ? [$path] : $path;
}
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string|null $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if ($group === '*' && $namespace === '*') {
return $this->loadJsonPaths($locale);
}
if (is_null($namespace) || $namespace === '*') {
return $this->loadPaths($this->paths, $locale, $group);
}
return $this->loadNamespaced($locale, $group, $namespace);
}
/**
* Load a namespaced translation group.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaced($locale, $group, $namespace)
{
if (isset($this->hints[$namespace])) {
$lines = $this->loadPaths([$this->hints[$namespace]], $locale, $group);
return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
}
return [];
}
/**
* Load a local namespaced translation group for overrides.
*
* @param array $lines
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
{
return collect($this->paths)
->reduce(function ($output, $path) use ($lines, $locale, $group, $namespace) {
$file = "{$path}/vendor/{$namespace}/{$locale}/{$group}.php";
if ($this->files->exists($file)) {
$lines = array_replace_recursive($lines, $this->files->getRequire($file));
}
return $lines;
}, []);
}
/**
* Load a locale from a given path.
*
* @param array $paths
* @param string $locale
* @param string $group
* @return array
*/
protected function loadPaths(array $paths, $locale, $group)
{
return collect($paths)
->reduce(function ($output, $path) use ($locale, $group) {
if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
$output = array_replace_recursive($output, $this->files->getRequire($full));
}
return $output;
}, []);
}
/**
* Load a locale from the given JSON file path.
*
* @param string $locale
* @return array
*
* @throws \RuntimeException
*/
protected function loadJsonPaths($locale)
{
return collect(array_merge($this->jsonPaths, $this->paths))
->reduce(function ($output, $path) use ($locale) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}
$output = array_merge($output, $decoded);
}
return $output;
}, []);
}
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint)
{
$this->hints[$namespace] = $hint;
}
/**
* Get an array of all the registered namespaces.
*
* @return array
*/
public function namespaces()
{
return $this->hints;
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->jsonPaths[] = $path;
}
/**
* Get an array of all the registered paths to JSON translation files.
*
* @return array
*/
public function jsonPaths()
{
return $this->jsonPaths;
}
}