Skip to content

Commit

Permalink
Merge pull request #38 from ARCANEDEV/update-json-translations
Browse files Browse the repository at this point in the history
Adding the ability to publish json files
  • Loading branch information
arcanedev-maroc committed Jul 12, 2020
2 parents 2bfab1c + 5bc114d commit 83b029d
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 40 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arcanedev/laravel-lang",
"description": "Translations manager and checker for Laravel based on 'caouecs/laravel4-lang' package.",
"description": "Translations manager and checker for Laravel based on 'caouecs/laravel-lang' package.",
"homepage": "https://github.com/ARCANEDEV/LaravelLang",
"keywords": [
"arcanedev", "laravel", "translations", "trans", "languages", "lang", "localisation", "localization", "validations"
Expand All @@ -18,7 +18,7 @@
"require": {
"php": "^7.2.5",
"arcanedev/support": "^7.1.2",
"caouecs/laravel-lang": "^6.1"
"caouecs/laravel-lang": "^6.1.2"
},
"require-dev": {
"orchestra/testbench": "^5.0",
Expand Down
5 changes: 4 additions & 1 deletion config/laravel-lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
*/

/** @link https://github.com/caouecs/Laravel-lang */
'vendor' => base_path('vendor/caouecs/laravel-lang/src'),
'vendor' => [
'php' => base_path('vendor/caouecs/laravel-lang/src'),
'json' => base_path('vendor/caouecs/laravel-lang/json'),
],

/* -----------------------------------------------------------------
| Supported locales
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Arcanedev\LaravelLang\Commands;

use Arcanedev\LaravelLang\Contracts\TransPublisher;
use Arcanedev\LaravelLang\Exceptions\LangPublishException;

/**
* Class PublishCommand
Expand All @@ -28,7 +27,8 @@ class PublishCommand extends AbstractCommand
protected $signature = 'trans:publish
{locale : The language to publish the translations.}
{--force : Force to override the translations}
{--inline : Publish the inline translations}';
{--inline : Publish the inline translations}
{--json : Include json translations file}';

/**
* The console command description.
Expand Down Expand Up @@ -85,6 +85,7 @@ public function handle(): void
$this->publish($locale, [
'force' => (bool) $this->option('force'),
'inline' => (bool) $this->option('inline'),
'json' => (bool) $this->option('json'),
]);

$this->line('');
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Locale implements LocaleContract
* @param string $path
* @param array $files
*/
public function __construct(string $key, string $path, array $files)
public function __construct(string $key, string $path, array $files = [])
{
$this->key = $key;
$this->path = $path;
Expand Down
26 changes: 13 additions & 13 deletions src/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class FileLoader extends IlluminateFileLoader
/**
* Vendor directory path.
*
* @var string
* @var array
*/
protected $vendorPath;
protected $vendorPaths;

/**
* Supported locales.
Expand All @@ -44,14 +44,14 @@ class FileLoader extends IlluminateFileLoader
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $path
* @param string $vendorPath
* @param array $vendorPaths
* @param array $locales
*/
public function __construct(Filesystem $files, string $path, string $vendorPath, array $locales = [])
public function __construct(Filesystem $files, string $path, array $vendorPaths, array $locales = [])
{
parent::__construct($files, $path);

$this->setVendorPath($vendorPath);
$this->setVendorPaths($vendorPaths);
$this->setSupportedLocales($locales);
}

Expand All @@ -63,23 +63,23 @@ public function __construct(Filesystem $files, string $path, string $vendorPath,
/**
* Get the vendor path.
*
* @return string
* @return array
*/
private function getVendorPath(): string
private function getVendorPaths(): array
{
return $this->vendorPath;
return $this->vendorPaths;
}

/**
* Set the vendor path.
* Set the vendor paths.
*
* @param string $vendorPath
* @param array $vendorPaths
*
* @return $this
*/
private function setVendorPath(string $vendorPath): self
private function setVendorPaths(array $vendorPaths): self
{
$this->vendorPath = $vendorPath;
$this->vendorPaths = $vendorPaths;

return $this;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function load($locale, $group, $namespace = null): array
$defaults = [];

if (empty($this->locales) || $this->isSupported($locale))
$defaults = $this->loadPath($this->getVendorPath(), $locale, $group);
$defaults = $this->loadPath($this->getVendorPaths()['php'], $locale, $group);

return array_replace_recursive($defaults, parent::load($locale, $group, $namespace));
}
Expand Down
28 changes: 22 additions & 6 deletions src/Providers/DeferredServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Arcanedev\LaravelLang\Providers;

use Illuminate\Support\Collection;
use Arcanedev\LaravelLang\{TransChecker, TransManager, TransPublisher};
use Arcanedev\LaravelLang\Contracts\{
TransChecker as TransCheckerContract,
Expand Down Expand Up @@ -62,12 +63,7 @@ public function provides(): array
private function registerTransManager(): void
{
$this->singleton(TransManagerContract::class, function (Application $app) {
$paths = array_map('realpath', [
'app' => $app->langPath(),
'vendor' => $app['config']->get('laravel-lang.vendor', ''),
]);

return new TransManager($app['files'], $paths);
return new TransManager($app['files'], $this->getVendorPaths($app));
});
}

Expand Down Expand Up @@ -98,4 +94,24 @@ private function registerLangPublisher(): void
);
});
}

/**
* Get the vendor paths.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
private function getVendorPaths(Application $app): array
{
return Collection::make($app['config']->get('laravel-lang.vendor', []))
->mapWithKeys(function (string $path, string $group) {
return ["vendor-{$group}" => $path];
})
->put('app', $app->langPath())
->transform(function (string $path) {
return realpath($path);
})
->toArray();
}
}
2 changes: 1 addition & 1 deletion src/Providers/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function registerLoader(): void
return new FileLoader(
$app['files'],
$app->langPath(),
$app['config']->get('laravel-lang.vendor', ''),
$app['config']->get('laravel-lang.vendor', []),
$app['config']->get('laravel-lang.locales', [])
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/TransChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function check(): array
private function getTranslations(string $locale, array $ignored): array
{
$appLocale = $this->manager->getFrom('app', $locale);
$vendorLocale = $this->manager->getFrom('vendor', $locale);
$vendorLocale = $this->manager->getFrom('vendor-php', $locale);

$translations = is_null($appLocale)
? $vendorLocale->mergeTranslations($appLocale, $ignored)
Expand Down
29 changes: 26 additions & 3 deletions src/TransManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class TransManager implements TransManagerContract
*
* @var array
*/
private $excludedFolders = ['script', 'vendor'];
private $excludedFolders = [
'script',
'vendor',
];

/* -----------------------------------------------------------------
| Constructor
Expand Down Expand Up @@ -95,18 +98,38 @@ public function getPaths(): array
private function load(): void
{
foreach ($this->getPaths() as $group => $path) {
$this->locales[$group] = $this->loadDirectories($path);
$this->locales[$group] = $group === 'vendor-json'
? $this->loadJsonDirectory($path)
: $this->loadDirectories($path);
}
}

/**
* @param string $path
*
* @return \Arcanedev\LaravelLang\Entities\LocaleCollection
*/
private function loadJsonDirectory(string $path): LocaleCollection
{
$locales = new LocaleCollection;

foreach ($this->filesystem->files($path) as $file) {
$locales->addOne(
new Locale($file->getBasename('.json'), $file->getRealPath())
);
}

return $locales;
}

/**
* Load directories.
*
* @param string $dirPath
*
* @return \Arcanedev\LaravelLang\Entities\LocaleCollection
*/
private function loadDirectories($dirPath): LocaleCollection
private function loadDirectories(string $dirPath): LocaleCollection
{
$locales = new LocaleCollection;

Expand Down
39 changes: 38 additions & 1 deletion src/TransPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __construct(Filesystem $filesystem, TransManagerContract $manage
*/
private function init(): void
{
$this->locales = $this->manager->getCollection('vendor');
$this->locales = $this->manager->getCollection('vendor-php');
}

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -150,13 +150,50 @@ public function publish(string $locale, array $options = []): array

$this->filesystem->ensureDirectoryExists($destination);

// Publish the PHP Translations
foreach ($this->filesystem->files($source) as $file) {
$this->publishFile($file, $locale, $destination, $options);
}

// Publish the JSON Translation
if ($options['json'] ?? false) {
$this->publishJson($locale, $destination);
}

return $this->results;
}

/**
* Publish the json file.
*
* @param string $locale
* @param string $destination
* @param array $options
*
* @return bool
*/
private function publishJson(string $locale, string $destination, array $options = []): bool
{
$file = $this->manager->getCollection('vendor-json')->get($locale);

if (is_null($file)) {
$this->results['skipped'][] = "{$locale}.json";
return false;
}

if ($this->filesystem->exists($destFile = $destination.'.json') && ($options['force'] ?? false) === false) {
$this->results['skipped'][] = "{$locale}.json";
return false;
}

return tap($this->filesystem->copy($file->getPath(), $destFile), function (bool $published) use ($locale) {
if ($published)
$this->results['published'][] = "{$locale}.json";
else
$this->results['skipped'][] = "{$locale}.json";
});
}

/* -----------------------------------------------------------------
| Check Methods
| -----------------------------------------------------------------
Expand Down
30 changes: 27 additions & 3 deletions tests/Commands/PublishCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,35 @@ public function it_can_publish_with_inline(): void
/** @test */
public function it_can_skip_the_default_locale(): void
{
$locale = 'en';

$this->artisan('trans:publish', compact('locale'))
$this->artisan('trans:publish', ['locale' => 'en'])
->assertExitCode(0);

static::assertTrue(true);
}

/** @test */
public function it_can_publish_json(): void
{
$locale = 'es';

$this->artisan('trans:publish', ['locale' => $locale, '--json' => true])
->expectsOutput("Publishing the [{$locale}] translations...")
->assertExitCode(0);

// Delete the lang folder.
$this->deleteLangDirectory($locale);
$this->deleteJsonLangFile($locale);
}

/**
* Delete the json file from lang folder.
*
* @param string $locale
*
* @return bool
*/
private function deleteJsonLangFile(string $locale): bool
{
return $this->filesystem()->delete($this->app->langPath().DIRECTORY_SEPARATOR.$locale.'.json');
}
}
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ protected function getEnvironmentSetUp($app): void
$config = $app['config'];

$config->set('laravel-lang', [
'vendor' => realpath($basePath . '/vendor/caouecs/laravel-lang/src'),
'vendor' => [
'php' => realpath($basePath . '/vendor/caouecs/laravel-lang/src'),
'json' => realpath($basePath . '/vendor/caouecs/laravel-lang/json'),
],

'locales' => ['es', 'fr'],

Expand Down

0 comments on commit 83b029d

Please sign in to comment.