Skip to content

Commit

Permalink
Create language file by specifying it in the command
Browse files Browse the repository at this point in the history
  • Loading branch information
cdruc committed Jul 4, 2021
1 parent 490352c commit 7ccd96b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@

This package scans your project for missing translation keys and then writes them into individual json files for you to fill in.

## Usage

Scan your project for missing translations:

```
php artisan langscanner
// outputs the translations
// writes them to translation files: en.json. nl.json, de.json, etc.
```

## Installation

You can install the package via composer:
Expand All @@ -23,6 +13,18 @@ You can install the package via composer:
composer require druc/laravel-langscanner
```

## Usage

Scan your project for missing translations:

```
// outputs and writes translations for the specified language (dutch)
php artisan langscanner nl
// outputs and writes translations in the existing {language}.json files
php artisan langscanner
```

## Credits

This package is based on [joedixon/laravel-translation](https://github.com/joedixon/laravel-translation) and [themsaid/laravel-langman-gui](https://github.com/themsaid/laravel-langman-gui)
Expand Down
20 changes: 12 additions & 8 deletions src/Commands/LangscannerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@

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

public function handle()
{
$headers = ["Language", "Key", "Path"];
$rows = [];

$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) {
if ($this->argument('language')) {
$languages = [$this->argument('language')];
} else {
$languages = (new RequiredLanguages(
new Filesystem,
config('langscanner.languages_path'),
config('langscanner.excluded_languages')
))->toArray();
}

foreach ($languages as $language) {
$missingTranslations = new MissingTranslations(
$requiredTranslations,
new ExistingTranslations(
Expand Down
2 changes: 1 addition & 1 deletion src/ExistingTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ExistingTranslations
private string $language;
private string $languagesPath;

public function __construct(Filesystem $disk, string $languagesPath, string $language)
public function __construct(Filesystem $disk, string $languagesPath, string $language)
{
$this->disk = $disk;
$this->language = $language;
Expand Down
7 changes: 5 additions & 2 deletions src/FileTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public function __construct(string $path)

public function update(MissingTranslations $missingTranslations)
{
$existingTranslations = file_get_contents($this->path);
$existingTranslations = json_decode($existingTranslations, true);
try {
$existingTranslations = json_decode(file_get_contents($this->path), true);
} catch (\Exception $e) {
$existingTranslations = [];
}

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

0 comments on commit 7ccd96b

Please sign in to comment.