Skip to content

Commit

Permalink
Merge pull request #4 from druc/new
Browse files Browse the repository at this point in the history
Prepare new release
  • Loading branch information
Constantin Druc authored Nov 21, 2021
2 parents ea51eef + aa6b6ce commit 0d480ea
Show file tree
Hide file tree
Showing 21 changed files with 455 additions and 191 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Check & fix styling

on: [ push ]

jobs:
php-cs-fixer:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
37 changes: 37 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests

on: [ push, pull_request ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest, windows-latest ]
php: [ 7.4, 8.0 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ coverage
docs
phpunit.xml
psalm.xml
testbench.yaml
vendor
.php-cs-fixer.cache
39 changes: 39 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => false,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# laravel-langscanner

[![Latest Version on Packagist](https://img.shields.io/packagist/v/druc/laravel-langscanner.svg?style=flat-square)](https://packagist.org/packages/druc/laravel-langscanner)
[![Tests](https://github.com/druc/laravel-langscanner/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/druc/laravel-langscanner/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/druc/laravel-langscanner.svg?style=flat-square)](https://packagist.org/packages/druc/laravel-langscanner)

This package scans your project for missing translation keys and then writes them into individual json files for you to fill in.
Expand Down
107 changes: 53 additions & 54 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
{
"name": "druc/laravel-langscanner",
"description": "Scan missing language translations.",
"keywords": [
"druc",
"laravel-langscanner"
],
"homepage": "https://github.com/druc/laravel-langscanner",
"license": "MIT",
"authors": [
{
"name": "Constantin Druc",
"email": "druc@pinsmile.com",
"role": "Developer"
}
],
"require": {
"php": "^7.4|^8.0",
"spatie/laravel-package-tools": "^1.4.3",
"illuminate/contracts": "^8.0",
"illuminate/support": "^8.0",
"illuminate/filesystem": "^8.0",
"ext-json": "*"
},
"require-dev": {
"nunomaduro/collision": "^5.3",
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
"Druc\\Langscanner\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Druc\\Langscanner\\Tests\\": "tests"
}
},
"scripts": {
"test": "./vendor/bin/testbench package:test",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Druc\\Langscanner\\LangscannerServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
"name": "druc/laravel-langscanner",
"description": "Scan missing language translations.",
"keywords": [
"druc",
"laravel-langscanner"
],
"homepage": "https://github.com/druc/laravel-langscanner",
"license": "MIT",
"authors": [
{
"name": "Constantin Druc",
"email": "druc@pinsmile.com",
"role": "Developer"
}
],
"require": {
"php": "^7.4|^8.0",
"spatie/laravel-package-tools": "^1.4.3",
"illuminate/contracts": "^8.0",
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.3",
"nunomaduro/collision": "^5.3",
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
"Druc\\Langscanner\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Druc\\Langscanner\\Tests\\": "tests"
}
},
"scripts": {
"test": "./vendor/bin/testbench package:test",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Druc\\Langscanner\\LangscannerServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 0 additions & 10 deletions config/langscanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,4 @@
|
*/
'excluded_paths' => [],

/*
|--------------------------------------------------------------------------
| Excluded languages
|--------------------------------------------------------------------------
|
| Which languages to exclude when scanning for missing translations.
|
*/
'excluded_languages' => ['en'],
];
35 changes: 35 additions & 0 deletions src/CachedFileTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Druc\Langscanner;

use Druc\Langscanner\Contracts\FileTranslations;

class CachedFileTranslations implements FileTranslations
{
private FileTranslations $fileTranslations;

public function __construct(FileTranslations $fileTranslations)
{
$this->fileTranslations = $fileTranslations;
}

public function update(array $translations): void
{
$this->fileTranslations->update($translations);
}

public function all(): array
{
$this->fileTranslations->all();
}

public function contains(string $key): bool
{
return $this->fileTranslations->contains($key);
}

public function language(): string
{
return $this->fileTranslations->language();
}
}
60 changes: 28 additions & 32 deletions src/Commands/LangscannerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,56 @@

namespace Druc\Langscanner\Commands;

use Druc\Langscanner\CachedFileTranslations;
use Druc\Langscanner\FileTranslations;
use Druc\Langscanner\Languages;
use Druc\Langscanner\MissingTranslations;
use Druc\Langscanner\RequiredTranslations;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class LangscannerCommand extends Command
{
protected $signature = 'langscanner {language?}';
protected $description = "Finds keys without a corresponding translation and writes them into the translation (json) files.";
protected $description = "Updates translation files with missing translation keys.";

public function handle(Filesystem $filesystem)
public function handle(Filesystem $filesystem): void
{
if ($this->argument('language')) {
$languages = [$this->argument('language')];
$languages = new Languages([$this->argument('language')]);
} else {
$languages = (new Languages($filesystem, resource_path('lang'), config('langscanner.excluded_languages')))->toArray();
$languages = Languages::fromPath(resource_path('lang'), $filesystem);
}

$requiredTranslations = (new RequiredTranslations($filesystem, config('langscanner')))->toArray();
foreach ($languages->all() as $language) {
$fileTranslations = new CachedFileTranslations(
new FileTranslations(['language' => $language])
);

$rows = [];

foreach ($languages as $language) {
if ($filesystem->exists(resource_path("lang/$language.json"))) {
$existingTranslations = json_decode($filesystem->get(resource_path("lang/$language.json")), true);
} else {
$existingTranslations = [];
}

$missingTranslations = $this->missingTranslations($requiredTranslations, $existingTranslations);

foreach ($missingTranslations as $key => $path) {
$rows[] = [$language, $key, $path];
}
$missingTranslations = new MissingTranslations(
new RequiredTranslations(config('langscanner')),
$fileTranslations
);

$fileTranslations->update(
// sets translation values to empty string
$missingTranslations = array_fill_keys(array_keys($missingTranslations), '');
(new FileTranslations(resource_path("lang/$language.json")))->update($missingTranslations);
}
array_fill_keys(
array_keys($missingTranslations->all()),
''
)
);

$this->table(["Language", "Key", "Path"], $rows);
}
// Render table
$this->comment(PHP_EOL);
$this->comment(strtoupper($language) . " missing translations:");

private function missingTranslations(array $required, array $existing): array
{
$missing = [];
$rows = [];

foreach ($required as $key => $value) {
if (empty($existing[$key])) {
$missing[$key] = $value;
foreach ($missingTranslations->all() as $key => $path) {
$rows[] = [$key, $path];
}
}

return $missing;
$this->table(["Key", "Path"], $rows);
}
}
}
14 changes: 14 additions & 0 deletions src/Contracts/FileTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Druc\Langscanner\Contracts;

interface FileTranslations
{
public function update(array $translations): void;

public function all(): array;

public function language(): string;

public function contains(string $key): bool;
}
Loading

0 comments on commit 0d480ea

Please sign in to comment.