Skip to content

Commit

Permalink
Change username
Browse files Browse the repository at this point in the history
Update

Update
  • Loading branch information
cdruc committed May 20, 2021
1 parent 56bd8be commit ca5f92f
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 456 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: cdruc
github: druc
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/cdruc/laravel-langscanner/discussions/new?category=q-a
url: https://github.com/druc/laravel-langscanner/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/cdruc/laravel-langscanner/discussions/new?category=ideas
url: https://github.com/druc/laravel-langscanner/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a bug
url: https://github.com/cdruc/laravel-langscanner/issues/new
url: https://github.com/druc/laravel-langscanner/issues/new
about: Report a reproducable bug
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) cdruc <druc@pinsmile.com>
Copyright (c) druc <druc@pinsmile.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# laravel-langscanner

[![Latest Version on Packagist](https://img.shields.io/packagist/v/cdruc/laravel-langscanner.svg?style=flat-square)](https://packagist.org/packages/cdruc/laravel-langscanner)
[![Total Downloads](https://img.shields.io/packagist/dt/cdruc/laravel-langscanner.svg?style=flat-square)](https://packagist.org/packages/cdruc/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)
[![Total Downloads](https://img.shields.io/packagist/dt/druc/laravel-langscanner.svg?style=flat-square)](https://packagist.org/packages/druc/laravel-langscanner)

## Usage

Expand All @@ -16,7 +16,7 @@ php artisan langscanner
You can install the package via composer:

```bash
composer require cdruc/laravel-langscanner
composer require druc/laravel-langscanner
```

## Credits
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "cdruc/laravel-langscanner",
"name": "druc/laravel-langscanner",
"description": "Scan missing language translations.",
"keywords": [
"cdruc",
"druc",
"laravel-langscanner"
],
"homepage": "https://github.com/cdruc/laravel-langscanner",
"homepage": "https://github.com/druc/laravel-langscanner",
"license": "MIT",
"authors": [
{
Expand All @@ -29,12 +29,12 @@
},
"autoload": {
"psr-4": {
"Cdruc\\Langscanner\\": "src"
"Druc\\Langscanner\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Cdruc\\Langscanner\\Tests\\": "tests"
"Druc\\Langscanner\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -47,7 +47,7 @@
"extra": {
"laravel": {
"providers": [
"Cdruc\\Langscanner\\LangscannerServiceProvider"
"Druc\\Langscanner\\LangscannerServiceProvider"
]
}
},
Expand Down
14 changes: 12 additions & 2 deletions config/langscanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
| Which directories to scan for missing translations.
|
*/
'scan_paths' => [app_path(), resource_path()],
'paths' => [app_path(), resource_path()],

/*
|--------------------------------------------------------------------------
Expand All @@ -39,7 +39,7 @@
| Which directories to exclude when scanning for missing translations.
|
*/
'scan_excluded_paths' => [],
'excluded_paths' => [],

/*
|--------------------------------------------------------------------------
Expand All @@ -50,4 +50,14 @@
|
*/
'excluded_languages' => ['en'],

/*
|--------------------------------------------------------------------------
| Languages path
|--------------------------------------------------------------------------
|
| Where are the language .json files located
|
*/
'languages_path' => resource_path('lang'),
];
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
verbose="true"
>
<testsuites>
<testsuite name="Cdruc Test Suite">
<testsuite name="Druc Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
86 changes: 37 additions & 49 deletions src/Commands/LangscannerCommand.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,56 @@
<?php

namespace Cdruc\Langscanner\Commands;
namespace Druc\Langscanner\Commands;

use Cdruc\Langscanner\Langscanner;
use Druc\Langscanner\ExistingTranslations;
use Druc\Langscanner\FileTranslations;
use Druc\Langscanner\MissingTranslations;
use Druc\Langscanner\RequiredLanguages;
use Druc\Langscanner\RequiredTranslations;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class LangscannerCommand extends Command
{
protected Langscanner $langscanner;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'langscanner';
protected $description = "Finds keys without a corresponding translations and writes them into the translation (json) files.";

/**
* The console command description.
*
* @var string
*/
protected $description = 'List all translation keys which don\'t have a corresponding translation';

public function __construct(Langscanner $langscanner)
{
parent::__construct();
$this->langscanner = $langscanner;
}

/**
* Execute the console command.
*
* @return mixed
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle()
{
$missingTranslations = $this->langscanner->missingTranslations();

if (empty($missingTranslations)) {
return $this->info("No missing translations were detected.");
}

$headers = ["Language", "Key", "Path"];
$rows = [];

// set some headers for the table of results
$headers = ["Language", "Type", "Group", "Key"];

// iterate over each of the missing languages
foreach ($missingTranslations as $language => $types) {
// iterate over each of the file types (json or array)
foreach ($types as $type => $keys) {
// iterate over each of the keys
foreach ($keys as $key => $value) {
// populate the array with the relevant data to fill the table
foreach ($value as $k => $v) {
$rows[] = [$language, $type, $key, $k];
}
}
$languages = new RequiredLanguages(
new Filesystem,
config('langscanner.languages_path'),
config('langscanner.excluded_languages')
);

$requiredTranslations = new RequiredTranslations(
new Filesystem,
config('langscanner.paths'),
config('langscanner.excluded_paths'),
config('langscanner.translation_methods')
);

foreach ($languages->toArray() as $language) {
$missingTranslations = new MissingTranslations(
$requiredTranslations,
new ExistingTranslations(
new Filesystem,
config('langscanner.languages_path'),
$language
)
);

foreach ($missingTranslations->toArray() as $key => $path) {
$rows[] = [$language, $key, $path];
}

(new FileTranslations(config('langscanner.languages_path')."/$language.json"))
->update($missingTranslations);
}

// render the table of results
$this->table($headers, $rows);
}
}
31 changes: 31 additions & 0 deletions src/ExistingTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Druc\Langscanner;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

class ExistingTranslations
{
private Filesystem $disk;
private string $language;
private string $languagesPath;

public function __construct(Filesystem $disk, string $languagesPath, string $language)
{
$this->disk = $disk;
$this->language = $language;
$this->languagesPath = $languagesPath;
}

public function toArray(): array
{
$translationFiles = new Collection($this->disk->allFiles($this->languagesPath));

return $translationFiles->filter(function ($file) {
return strpos($file, "{$this->language}.json");
})->flatMap(function ($file) {
return json_decode($this->disk->get($file), true);
})->toArray();
}
}
27 changes: 27 additions & 0 deletions src/FileTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Druc\Langscanner;

class FileTranslations
{
private string $path;

public function __construct(string $path)
{
$this->path = $path;
}

public function update(MissingTranslations $missingTranslations)
{
$existingTranslations = file_get_contents($this->path);
$existingTranslations = json_decode($existingTranslations, true);

// Sets keys to an empty string: ['needs translation' => '', etc]
$unfilledTranslations = array_fill_keys(array_keys($missingTranslations->toArray()), '');

$mergedTranslations = array_merge($existingTranslations, $unfilledTranslations);
$mergedTranslations = json_encode($mergedTranslations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

file_put_contents($this->path, $mergedTranslations);
}
}
Loading

0 comments on commit ca5f92f

Please sign in to comment.