Skip to content

Laravel 6-7 support #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 86 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Begin by installing the package through Composer. Run the following command in y
$ composer require themsaid/laravel-langman
```

Once done, add the following line in your providers array of `config/app.php`:
Once done, check that the following line was added in your providers array of `config/app.php`:

```php
Themsaid\Langman\LangmanServiceProvider::class
```

This package has a single configuration option that points to the `resources/lang` directory, if only you need to change
the path then publish the config file:
This package has a single configuration option that points to the `resources/lang` directory. If you
only need to change the path then publish the config file:

```
php artisan vendor:publish --provider="Themsaid\Langman\LangmanServiceProvider"
Expand All @@ -40,12 +40,34 @@ php artisan vendor:publish --provider="Themsaid\Langman\LangmanServiceProvider"
## Usage

### Showing lines of a translation file
```
php artisan langman:show [file.][key] [--close] [--unused] [--lang <language key(s)>]
```

In the table returned by the Show command, if a translation is missing it'll be marked in red.

```
php artisan langman:show
```
Shows all keys in the JSON translation located at `lang/<locale>.json`.

Example output:
```
+-----------------------------+---------------+-------------+
| key | en | nl |
+-----------------------------+---------------+-------------+
| What is in a name? | MISSING | MISSING |
| Do you need more proof? | MISSING | MISSING |
+-----------------------------+---------------+-------------+
```

```
php artisan langman:show users
```
Shows all keys in the `lang/<locale>/users.php` translation file. If no such file exists, langman
assumes that you are searching in the list of JSON strings.

You get:
Example output:

```
+---------+---------------+-------------+
Expand All @@ -62,68 +84,97 @@ You get:
php artisan langman:show users.name
```

Brings only the translation of the `name` key in all languages.
Shows only the translation of the `name` key in all languages as found in the `lang/<locale>/users.php`
translation files.

---

```
php artisan langman:show users.name.first
```

Brings the translation of a nested key.
Shows the translation of a nested key.

---

```
php artisan langman:show package::users.name
```

Brings the translation of a vendor package language file.
Shows the translation of a vendor package language file.

---

```
php artisan langman:show users --lang=en,it
```

Brings the translation of only the "en" and "it" languages.
Shows the translation of only the "en" and "it" languages.

---

```
php artisan langman:show users.nam -c
php artisan langman:show users.nam --close
```

Brings only the translation lines with keys matching the given key via close match, so searching for `nam` brings values for
keys like (`name`, `username`, `branch_name_required`, etc...).
Shows only the translation lines with keys containing the given key via substring match, so searching for
`nam` brings values for keys like (`name`, `username`, `branch_name_required`, etc...).

If the close option is specified, the file/key option is always interpreted as a key if it does not
contain a dot, or the resulting file does not exist. E.g., if the `lang/<locale>/users.php` file does not exist,
then:
```
php artisan langman:show users --close
```
would show all keys containing the 'users' string in the JSON translation files.

Similarly:
```
php artisan langman:show "I don't know. Perhaps" --close
```
will not look for a file called `"I don't know.php"`, but interpret the whole provided element as a key to search for.

---

```
php artisan langman:show users -u
php artisan langman:show users --unused
```

Scans all the view templates and application files (see Sync command) and outputs only the keys found in the
specified file that were not used in any template or file. This helps identifying legacy elements and keep
your translation files tight and orderly.

In the table returned by this command, if a translation is missing it'll be marked in red.

### Finding a translation line

```
php artisan langman:find 'log in first'
```

You get a table of language lines where any of the values matches the given phrase by close match.
You get a table of language lines where any of the values contains the given phrase. Strings from the JSON
translation files are capped at 40 characters to keep the output tidy.

### Searching view files for missing translations

```
php artisan langman:sync
```

This command will look into all files in `resources/views` and `app` and find all translation keys that are not covered in your translation files, after
that it appends those keys to the files with a value equal to an empty string.
This command will look into all files in `resources/views` and `app` and find all translation keys that are not
covered in your translation files. After that it appends those keys to the files with an empty value. Then it
synchronises all translation files and adds all missing entries in each file, ensuring that all translation files
for all locales contain an entry for the same set of keys.

### Filling missing translations

```
php artisan langman:missing
```

It'll collect all the keys that are missing in any of the languages or has values equals to an empty string, prompt
asking you to give a translation for each, and finally save the given values to the files.
This command collects all the keys that are missing or empty in any of the languages, prompt you for a
translation for each and finally saves the given values to the proper files.

### Translating a key

Expand All @@ -132,34 +183,46 @@ php artisan langman:trans users.name
php artisan langman:trans users.name.first
php artisan langman:trans users.name --lang=en
php artisan langman:trans package::users.name
php artisan langman:trans 'Translate Me'
```

Using this command you may set a language key (plain or nested) for a given group, you may also specify which language you wish to set leaving the other languages as is.
Using this command you may set a language key (plain or nested) for a given group. You may also specify
which language you wish to set, leaving the other languages as is.

This command will add a new key if not existing, and updates the key if it is already there.
This command will add a new key if it did not exist yet and updates the key if it is already there.

### Removing a key

```
php artisan langman:remove users.name
php artisan langman:remove package::users.name
php artisan langman:remove 'Random JSON string'
```

It'll remove that key from all language files.
Removes the specified key from all relevant language files.

### Renaming a key

```
php artisan langman:rename users.name full_name
```
This will rename `users.name` to `users.full_name`. The console will output a list of files where
the key used to exist.

This will rename `users.name` to be `users.full_name`, the console will output a list of files where the key used to exist.
```
php artisan langman:rename 'Json Search String' 'New Json Search String'
```
This will rename the JSON translatable string `'Json Search String'` to `'New Json Search String'` in all
relevant JSON translation files.

## Notes

`langman:sync`, `langman:missing`, `langman:trans`, and `langman:remove` will update your language files by writing them completely, meaning that any comments or special styling will be removed, so I recommend you backup your files.
`langman:sync`, `langman:missing`, `langman:trans`, and `langman:remove` will update your language files
by rewriting them completely, meaning that any comments or special styling will be removed. I recommend
that you backup your files if this is the first time you are running the tool. Langman sorts all keys in
files alphabetically by key name.

## Web interface

If you want a web interface to manage your language files instead, I recommend [Laravel 5 Translation Manager](https://github.com/barryvdh/laravel-translation-manager)
by [Barry vd. Heuvel](https://github.com/barryvdh).
If you want a web interface to manage your language files instead, I recommend
[Laravel Translation Manager](https://github.com/barryvdh/laravel-translation-manager) by [Barry vd. Heuvel](https://github.com/barryvdh).
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
],
"require": {
"php": "^5.5.9 || ^7.0",
"illuminate/support": "~5.1",
"illuminate/console": "~5.1",
"illuminate/filesystem": "~5.1"
"illuminate/support": "~5.1 || ^6.0 || ^7.0",
"illuminate/console": "~5.1 || ^6.0 || ^7.0",
"illuminate/filesystem": "~5.1 || ^6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit" : "^4.8 || ^5.0",
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/FindCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ private function tableRows()
// Sort the language values based on language name
ksort($original);

$output[$fullKey] = array_merge(['key' => "<fg=yellow>$fullKey</>"], $original);
$okey = $fullKey;
if ($fileName == "-json") {
$okey = strlen($key) > 40 ? substr($key, 0, 36)." ..." : $key;
}
$output[$fullKey] = array_merge(['key' => "<fg=yellow>$okey</>"], $original);
}

return array_values($output);
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ private function collectValues(array $missing)
$values = [];

foreach ($missing as $missingKey) {
$key = $missingKey;
if (substr($key, 0, 6) == "-json.") {
$key = substr($key, 6);
}
$values[$missingKey] = $this->ask(
"<fg=yellow>{$missingKey}</> translation", $this->getDefaultValue($missingKey)
"<fg=yellow>{$key}</> translation", $this->getDefaultValue($missingKey)
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Commands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ public function handle()
try {
list($file, $key) = explode('.', $this->argument('key'), 2);
} catch (\ErrorException $e) {
$this->error('Could not recognize the key you want to remove.');

return;
$file = "-json";
$key = $this->argument('key');
}

if ($this->confirm("Are you sure you want to remove \"{$file}.{$key}\"?")) {
$fileName=$file.".";
if ($file === "-json") {
$fileName="";
}
if ($this->confirm("Are you sure you want to remove \"{$fileName}{$key}\"?")) {
if (Str::contains($file, '::')) {
try {
$parts = explode('::', $file);
Expand All @@ -81,7 +84,7 @@ public function handle()

$this->manager->removeKey($file, $key);

$this->info("{$file}.{$key} was removed successfully.");
$this->info("{$fileName}{$key} was removed successfully.");
}
}
}
17 changes: 9 additions & 8 deletions src/Commands/RenameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function handle()

$this->listFilesContainingOldKey();

$this->info('The key at '.$this->argument('oldKey').' was renamed to '.$this->argument('newKey').' successfully!');
$this->info('The key at "'.$this->argument('oldKey').'" was renamed to "'.$this->argument('newKey').'" successfully!');
}

/**
Expand All @@ -74,18 +74,19 @@ private function renameKey()
try {
list($file, $key) = explode('.', $this->argument('oldKey'), 2);
} catch (\ErrorException $e) {
$this->error('Could not recognize the key you want to rename.');

return;
$file = "-json";
$key = $this->argument('oldKey');
}

if (Str::contains($this->argument('newKey'), '.')) {
$this->error('Please provide the new key must not contain a dot.');

if (Str::contains($this->argument('newKey'), '.') && $file !== "-json") {
$this->error('The provided new key must not contain a dot.');
return;
}

$newKey = preg_replace('/(\w+)$/i', $this->argument('newKey'), $key);
$newKey = $this->argument('newKey');
if ($file !== "-json") {
$newKey = preg_replace('/(\w+)$/i', $newKey, $key);
}

$files = $this->manager->files()[$file];

Expand Down
Loading