Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #78 from suyar/fix-trans-fail
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue authored Aug 20, 2021
2 parents 8623d49 + 8aaca30 commit cb82a2f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/Commands/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function handle()
$locale = \str_replace('-', '_', $this->argument('locales'));
$force = $this->option('force') ? 'f' : 'n';

$sourcePath = base_path('vendor/laravel-lang/lang/src');
$sourceJsonPath = base_path('vendor/laravel-lang/lang/json');
$sourcePath = base_path('vendor/laravel-lang/lang/locales');
$sourceJsonPath = base_path('vendor/laravel-lang/lang/locales');
$targetPath = base_path('resources/lang/');

if (!is_dir($targetPath) && !mkdir($targetPath)) {
Expand All @@ -53,7 +53,7 @@ public function handle()
if ('all' == $locale) {
$files = [
\addslashes($sourcePath).'/*',
escapeshellarg($sourceJsonPath),
\addslashes($sourceJsonPath).'/*/*.json',
];
$message = 'all';
$copyEnFiles = true;
Expand All @@ -64,8 +64,10 @@ public function handle()

continue;
}
$file = $sourcePath.'/'.trim($filename);
$jsonFile = $sourceJsonPath.'/'.trim($filename).'.json';

$trimFilename = trim($filename);
$file = $sourcePath.'/'.$trimFilename;
$jsonFile = $sourceJsonPath."/{$trimFilename}/{$trimFilename}".'.json';

if (!file_exists($file)) {
$this->error("'$filename' not found.");
Expand Down
48 changes: 48 additions & 0 deletions src/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader as LaravelTranslationFileLoader;
use RuntimeException;

class FileLoader extends LaravelTranslationFileLoader
{
Expand All @@ -13,6 +14,11 @@ class FileLoader extends LaravelTranslationFileLoader
*/
protected $paths;

/**
* @var string[]
*/
protected $customJsonPaths = [];

/**
* Create a new file loader instance.
*
Expand Down Expand Up @@ -68,4 +74,46 @@ protected function loadPath($path, $locale, $group)

return $result;
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->customJsonPaths[] = $path;
parent::addJsonPath($path);
}

/**
* 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->path]))
->reduce(function ($output, $path) use ($locale) {
if (in_array($path, $this->customJsonPaths)) {
$locale = "{$locale}/{$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;
}, []);
}
}
4 changes: 2 additions & 2 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function registerLoader()
{
$this->app->singleton('translation.loader', function ($app) {
$paths = [
base_path('vendor/laravel-lang/lang/src/'),
base_path('vendor/laravel-lang/lang/locales/'),
];

if ($this->inLumen) {
Expand All @@ -57,7 +57,7 @@ protected function registerLoader()
$loader = new FileLoader($app['files'], $app['path.lang'], $paths);

if (\is_callable([$loader, 'addJsonPath'])) {
$loader->addJsonPath(base_path('vendor/laravel-lang/lang/json/'));
$loader->addJsonPath(base_path('vendor/laravel-lang/lang/locales/'));
}

return $loader;
Expand Down

0 comments on commit cb82a2f

Please sign in to comment.