Skip to content

Commit

Permalink
Merge pull request #37 from ARCANEDEV/patch-1
Browse files Browse the repository at this point in the history
Using the latest `caouecs/Laravel-lang` changes
  • Loading branch information
arcanedev-maroc committed Jul 10, 2020
2 parents 8b1711a + e23da2b commit 4e5419f
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 100 deletions.
6 changes: 3 additions & 3 deletions composer.json
@@ -1,6 +1,6 @@
{
"name": "arcanedev/laravel-lang",
"description": "Translations manager and checker for Laravel 5 based on 'caouecs/laravel4-lang' package.",
"description": "Translations manager and checker for Laravel based on 'caouecs/laravel4-lang' package.",
"homepage": "https://github.com/ARCANEDEV/LaravelLang",
"keywords": [
"arcanedev", "laravel", "translations", "trans", "languages", "lang", "localisation", "localization", "validations"
Expand All @@ -18,11 +18,11 @@
"require": {
"php": "^7.2.5",
"arcanedev/support": "^7.1.2",
"caouecs/laravel-lang": "^6.0"
"caouecs/laravel-lang": "^6.1"
},
"require-dev": {
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^8.5|^9.0",
"phpunit/phpunit": "^8.5",
"mockery/mockery": "^1.3.1"
},
"autoload": {
Expand Down
28 changes: 15 additions & 13 deletions src/Commands/PublishCommand.php
Expand Up @@ -27,7 +27,8 @@ class PublishCommand extends AbstractCommand
*/
protected $signature = 'trans:publish
{locale : The language to publish the translations.}
{--force : Force to override the translations}';
{--force : Force to override the translations}
{--inline : Publish the inline translations}';

/**
* The console command description.
Expand Down Expand Up @@ -75,10 +76,16 @@ public function handle(): void

$locale = (string) $this->argument('locale');

if ($this->publisher->isDefault($locale))
if ($this->publisher->isDefault($locale)) {
$this->info("The locale [{$locale}] is a default lang and it's shipped with laravel.");
else
$this->publish($locale, (bool) $this->option('force'));
$this->line('');
return;
}

$this->publish($locale, [
'force' => (bool) $this->option('force'),
'inline' => (bool) $this->option('inline'),
]);

$this->line('');
}
Expand All @@ -92,17 +99,12 @@ public function handle(): void
* Publish the translations.
*
* @param string $locale
* @param bool $force
* @param array $options
*/
private function publish(string $locale, bool $force): void
private function publish(string $locale, array $options): void
{
try {
$this->publisher->publish($locale, $force);
$this->publisher->publish($locale, $options);

$this->info("The locale [{$locale}] translations were published successfully.");
}
catch (LangPublishException $e) {
$this->error($e->getMessage());
}
$this->info("The locale [{$locale}] translations were published successfully.");
}
}
6 changes: 3 additions & 3 deletions src/Contracts/TransPublisher.php
Expand Up @@ -21,13 +21,13 @@ interface TransPublisher
* Publish a lang.
*
* @param string $localeKey
* @param bool $force
* @param array $options
*
* @return bool
* @return array
*
* @throws \Arcanedev\LaravelLang\Exceptions\LangPublishException
*/
public function publish(string $localeKey, $force = false): bool;
public function publish(string $localeKey, array $options = []): array;

/* -----------------------------------------------------------------
| Check Methods
Expand Down
21 changes: 1 addition & 20 deletions src/Exceptions/LangPublishException.php
Expand Up @@ -10,23 +10,4 @@
* @package Arcanedev\LaravelLang\Exceptions
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class LangPublishException extends LaravelLangException
{
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* @param string $locale
*
* @return static
*/
public static function unpublishable(string $locale): self
{
return new static(
"You can't publish the translations because the [$locale] folder is not empty. ".
"To override the translations, try to clean/delete the [$locale] folder or force the publication."
);
}
}
class LangPublishException extends LaravelLangException {}
10 changes: 8 additions & 2 deletions src/TransChecker.php
Expand Up @@ -6,8 +6,10 @@

use Arcanedev\LaravelLang\Contracts\{
TransChecker as TransCheckerInterface,
TransManager as TransManagerInterface};
TransManager as TransManagerInterface
};
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Translation\Translator;

/**
Expand Down Expand Up @@ -151,9 +153,13 @@ private function getTranslations(string $locale, array $ignored): array
$appLocale = $this->manager->getFrom('app', $locale);
$vendorLocale = $this->manager->getFrom('vendor', $locale);

return is_null($appLocale)
$translations = is_null($appLocale)
? $vendorLocale->mergeTranslations($appLocale, $ignored)
: $appLocale->mergeTranslations($vendorLocale, $ignored);

return array_filter($translations, function ($key) {
return ! Str::startsWith($key, ['validation-inline.']);
}, ARRAY_FILTER_USE_KEY);
}

/**
Expand Down
105 changes: 66 additions & 39 deletions src/TransPublisher.php
Expand Up @@ -8,6 +8,8 @@
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerContract;
use Arcanedev\LaravelLang\Exceptions\LangPublishException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use SplFileInfo;

/**
* Class TransPublisher
Expand Down Expand Up @@ -50,6 +52,13 @@ class TransPublisher implements TransPublisherContract
*/
private $langPath;

/**
* Publish's results.
*
* @var array
*/
private $results = [];

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
Expand Down Expand Up @@ -118,26 +127,34 @@ private function getLocale(string $key, $default = null)
* Publish a lang.
*
* @param string $locale
* @param bool $force
* @param array $options
*
* @return bool
* @return array
*/
public function publish(string $locale, $force = false): bool
public function publish(string $locale, array $options = []): array
{
$this->resetResults();

$locale = trim($locale);

if ($this->isDefault($locale)) {
return true;
$this->results['skipped'][] = $locale;

return $this->results;
}

$this->checkLocale($locale);

$source = $this->getLocale($locale)->getPath();
$destination = $this->getDestinationPath($locale);

$this->isPublishable($locale, $destination, $force);
$this->filesystem->ensureDirectoryExists($destination);

return $this->filesystem->copyDirectory($source, $destination);
foreach ($this->filesystem->files($source) as $file) {
$this->publishFile($file, $locale, $destination, $options);
}

return $this->results;
}

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -183,54 +200,64 @@ private function checkLocale(string $locale): void
}
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/

/**
* Check the folder exists.
* Publish the translation file.
*
* @param string $path
*
* @return bool
* @param SplFileInfo $file
* @param string $locale
* @param string $destination
* @param array $options
*/
private function isFolderExists(string $path): bool
private function publishFile(SplFileInfo $file, string $locale, string $destination, array $options): void
{
return $this->filesystem->exists($path)
&& $this->filesystem->isDirectory($path);
$isInlineFile = Str::endsWith($file->getFilename(), '-inline.php');
$destFile = $isInlineFile
? Str::replaceLast('-inline.php', '.php', $file->getFilename())
: $file->getFilename();

if ($this->isInResults($key = "{$locale}/{$destFile}"))
return;

// Ignore if inline option is not enabled
if ($isInlineFile && (($options['inline'] ?? false) === false))
return;

// Ignore if force option is not enabled
if ($this->filesystem->exists($destination.DIRECTORY_SEPARATOR.$destFile) && ($options['force'] ?? false) === false) {
$this->results['skipped'][] = $key;
return;
}

$this->filesystem->copy($file->getRealPath(), $destination.DIRECTORY_SEPARATOR.$destFile);
$this->results['published'][] = $key;
}

/**
* Check if the folder is empty.
*
* @param string $path
*
* @return bool
* Reset the publish results.
*/
private function isFolderEmpty(string $path): bool
private function resetResults(): void
{
$files = $this->filesystem->files($path);

return empty($files);
$this->results = [
'published' => [],
'skipped' => [],
];
}

/**
* Check if locale is publishable.
* Check if the given key exists in results.
*
* @param string $locale
* @param string $path
* @param bool $force
* @param string $key
*
* @throws \Arcanedev\LaravelLang\Exceptions\LangPublishException
* @return bool
*/
private function isPublishable(string $locale, string $path, bool $force): void
private function isInResults(string $key): bool
{
if ( ! $this->isFolderExists($path)) {
return;
}

if ($this->isFolderEmpty($path)) {
return;
}

if ( ! $force) {
throw LangPublishException::unpublishable($locale);
}
return in_array($key, $this->results['published'])
|| in_array($key, $this->results['skipped']);
}
}
13 changes: 13 additions & 0 deletions tests/Commands/PublishCommandTest.php
Expand Up @@ -65,6 +65,19 @@ public function it_can_publish_with_force(): void
$this->deleteLangDirectory($locale);
}

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

$this->artisan('trans:publish', ['locale' => $locale, '--inline' => true])
->expectsOutput("The locale [{$locale}] translations were published successfully.")
->assertExitCode(0);

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

/** @test */
public function it_can_skip_the_default_locale(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Expand Up @@ -28,8 +28,8 @@ abstract class TestCase extends BaseTestCase
'ar', 'az', 'be', 'bg', 'bn', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'de-CH', 'el', 'es', 'et', 'eu', 'fa', 'fi',
'fil', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'km', 'kn', 'ko', 'lt',
'lv', 'me', 'mk', 'mn', 'mr', 'ms', 'nb', 'ne', 'nl', 'nn', 'pl', 'ps', 'pt', 'pt-BR', 'ro', 'ru', 'sc', 'si',
'sk', 'sl', 'sq', 'sr', 'sv', 'sw', 'tg', 'th', 'tk', 'tl', 'tr', 'ug', 'uk', 'ur', 'uz-cyrillic', 'uz-latin',
'vi', 'zh-CN', 'zh-HK', 'zh-TW',
'sk', 'sl', 'sq', 'sr-cyrillic', 'sr-latin', 'sv', 'sw', 'tg', 'th', 'tk', 'tl', 'tr', 'ug', 'uk', 'ur',
'uz-cyrillic', 'uz-latin', 'vi', 'zh-CN', 'zh-HK', 'zh-TW',
];

/* -----------------------------------------------------------------
Expand Down

0 comments on commit 4e5419f

Please sign in to comment.