diff --git a/config/language-manager.php b/config/translation-manager.php similarity index 87% rename from config/language-manager.php rename to config/translation-manager.php index ef49284..4ba6670 100644 --- a/config/language-manager.php +++ b/config/translation-manager.php @@ -25,4 +25,7 @@ // Display source column // Adds a column to the translation lines table to show the source, either database or file 'display_source' => false, + + // If you have Editable Columns addon, Backpack will use it in the translations table. + 'use_editable_columns' => true, ]; diff --git a/license.md b/license.md index de6ba3f..028b2f1 100644 --- a/license.md +++ b/license.md @@ -1,21 +1,21 @@ -# The MIT License (MIT) +MIT License -Copyright (c) Cristian Tabacitu +Copyright (c) 2022 Cristian Tabacitu -> Permission is hereby granted, free of charge, to any person obtaining a copy -> of this software and associated documentation files (the "Software"), to deal -> in the Software without restriction, including without limitation the rights -> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -> copies of the Software, and to permit persons to whom the Software is -> furnished to do so, subject to the following conditions: -> -> The above copyright notice and this permission notice shall be included in -> all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -> THE SOFTWARE. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/phpstan.neon b/phpstan.neon index 17f8e98..0eba06a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,6 +10,6 @@ parameters: - routes/ excludePaths: - - src/Http/Operations/MinorUpdateOperation.php + - src/Http/Operations/CanUseEditableColumns.php level: 5 \ No newline at end of file diff --git a/readme.md b/readme.md index bcaf589..a882578 100644 --- a/readme.md +++ b/readme.md @@ -1,20 +1,20 @@ # Translation Manager -[![Latest Version on Packagist][ico-version]][link-packagist] [![Total Downloads][ico-downloads]][link-downloads] [![The Whole Fruit Manifesto](https://img.shields.io/badge/writing%20standard-the%20whole%20fruit-brightgreen)](https://github.com/the-whole-fruit/manifesto) -Translation Manager provides a user interface for Backpack to manage translations. It allows you to: +Translation Manager provides a simple user interface to help you deal with translations in your Backpack application. +At a quick glance, some of the most relevant features are: - View a list of all translations present in your application's language files (including vendor translations). -- Edit translations directly within the list. +- Edit translations directly from the interface. - Search and filter translations for easy management. -This package leverages the functionalities of `spatie/laravel-translation-loader` to Backpack for Laravel, providing a user interface to manage translations. +This package uses the battle tested [spatie/laravel-translation-loader](https://github.com/spatie/laravel-translation-loader) under the hood. ## Preview -![](https://user-images.githubusercontent.com/1032474/205863022-827f3248-a9f3-4d05-896f-5fa7a40227be.gif) +![](https://private-user-images.githubusercontent.com/1032474/318216127-f65a24ea-473d-4fec-8ffc-b8137bcb1b9f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTE4MjI3NTcsIm5iZiI6MTcxMTgyMjQ1NywicGF0aCI6Ii8xMDMyNDc0LzMxODIxNjEyNy1mNjVhMjRlYS00NzNkLTRmZWMtOGZmYy1iODEzN2JjYjFiOWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMzBUMTgxNDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjFiMTg2MDFkOTMzODBlODU1MWRjNjdhMTkxNjE5Y2ExNTg4MTE2ZWZmMWU4ZDcyZjdhYmNkZDQzODk1YTYzMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.4DN0BGCI86PS2-CP0qF7299xBEq1I_4RLUcYtjPWmS4) ## Demo @@ -23,64 +23,77 @@ Try it right now, edit some translations in [our online demo](https://demo.backp ## Installation -In your Laravel + Backpack project, install this package: +In your Laravel + Backpack project: -1) Install the package using Composer: +**1) Install the package using Composer**: ```bash composer require backpack/translation-manager ``` -2) Add menu items to `sidebar_content.blade.php`: +**2) Configure the application** + +> _If you already had [spatie/laravel-translation-loader](https://github.com/spatie/laravel-translation-loader) installed and configured, you can skip to the next step. Otherwise, follow along._ + +2.1) In your `config/app.php` you must replace Laravel's translation service provider: + +```diff +-Illuminate\Translation\TranslationServiceProvider::class, ++Spatie\TranslationLoader\TranslationServiceProvider::class, +``` + +2.2) You must publish and run the migrations to create the `language_lines` table: +```bash +php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="migrations" +php artisan migrate +``` + +**3) Optional setup options** + +3.1) Add a menu item to `menu_items.blade.php` for easy access: ```bash -php artisan backpack:add-menu-content "" +php artisan backpack:add-menu-content "" ``` -3) Optionally, publish the config file: +3.2) Publish the config files: ```bash php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="config" +php artisan vendor:publish --provider="Backpack\TranslationManager\AddonServiceProvider" --tag="config" ``` -4) But also, if your package didn't already have [`spatie/laravel-translation-loader`](https://github.com/spatie/laravel-translation-loader) installed and set up, please [follow the installation steps in their docs](https://github.com/spatie/laravel-translation-loader#installation). We'll also copy-paste them here, for your convenience: +**NOTE:** We highly recommend you to use this package alongside [Language Switcher](https://github.com/Laravel-Backpack/language-switcher) package, so that you can easily switch between languages in your panel. - 4.1) In `config/app.php` you should replace Laravel's translation service provider +## Usage - ```diff - -Illuminate\Translation\TranslationServiceProvider::class, - +Spatie\TranslationLoader\TranslationServiceProvider::class, - ``` +### List View: - 4.2) You must publish and run the migrations to create the `language_lines` table: +![](https://private-user-images.githubusercontent.com/1032474/318216122-88996f7c-6807-4c54-a3f8-10ab18afaa24.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTE4MjI3NTcsIm5iZiI6MTcxMTgyMjQ1NywicGF0aCI6Ii8xMDMyNDc0LzMxODIxNjEyMi04ODk5NmY3Yy02ODA3LTRjNTQtYTNmOC0xMGFiMThhZmFhMjQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMzBUMTgxNDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWFjNWMzMGEyOWZhMmUwYjQ5OGVhNmRiYTZlNjJiYmE1ZTk0NTllMDk4MTkyMjcxMjIwNjQ3NjNhMjdhNzdmNCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.VSr1PIyGkrb13awM5kA9tJK4vf8SPwcVyA1mFpHIUd4) - ```bash - php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="migrations" - php artisan migrate - ``` +The list view displays a comprehensive list of all translations within your application. By default, all translations including vendor translations are displayed in the list view. If you don't want to see vendor translations, you can set the config option `load_all_registered_translation_paths` to `false` in `config/backpack/translation-manager.php`. - 4.3) Optionally you could publish the config file using this command. +Additionally, if you have [Backpack Pro](https://backpackforlaravel.com/products/pro-for-unlimited-projects) installed, your admin can also see and use the filters, to quickly narrow down translations. - ```bash - php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="config" - ``` +### Edit View -5) We highly recommend you to use this package allong with the [Language Switcher](https://github.com/Laravel-Backpack/language-switcher) package, so you can easily switch between languages in your panel. +![](https://private-user-images.githubusercontent.com/1032474/318216125-13fa216a-24e0-4a82-b949-d24124c8ee2a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTE4MjI3NTcsIm5iZiI6MTcxMTgyMjQ1NywicGF0aCI6Ii8xMDMyNDc0LzMxODIxNjEyNS0xM2ZhMjE2YS0yNGUwLTRhODItYjk0OS1kMjQxMjRjOGVlMmEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMzBUMTgxNDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MDc1ZGM5ZjVlMjE4MWNiMDlkYzZmNzk2YWQ3MjYzNDA3NGQwNTU2ZGQ0M2ViZDc4Njc1OTNmMjU2Yjc4MjljZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.voeYunEbwpqn9DXZbsxrWE9TE7HsYMVcb7sQ7PJzwO8) +Any translation can be edited by clicking the Edit button. It will open a page where the admin can input the new value, for all languages. -## Usage +Once edited, the changes are saved to the database for persistence. All translations on the database have priority over the ones in the language files. This means that you can safely let your admin edit translations, in production. Your auto-deploys will continue working, because no files get edited, only DB entries. -### Translation List View: +### Editable Columns -The list view displays a comprehensive list of all translations within your application. -You can search and filter translations using provided functionalities. Filters are available with the [Backpack Pro](https://backpackforlaravel.com/products/pro-for-unlimited-projects) package. -All translations including vendor translations are displayed in the list view, if you don't want to see vendor translations, you can filter them out setting the `load_all_registered_translation_paths` config option to `false`. +![](https://private-user-images.githubusercontent.com/1032474/318216122-88996f7c-6807-4c54-a3f8-10ab18afaa24.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTE4MjI3NTcsIm5iZiI6MTcxMTgyMjQ1NywicGF0aCI6Ii8xMDMyNDc0LzMxODIxNjEyMi04ODk5NmY3Yy02ODA3LTRjNTQtYTNmOC0xMGFiMThhZmFhMjQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMzBUMTgxNDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWFjNWMzMGEyOWZhMmUwYjQ5OGVhNmRiYTZlNjJiYmE1ZTk0NTllMDk4MTkyMjcxMjIwNjQ3NjNhMjdhNzdmNCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.VSr1PIyGkrb13awM5kA9tJK4vf8SPwcVyA1mFpHIUd4) -### Editing Translations: +If you have the [Editable Columns](https://backpackforlaravel.com/products/editable-columns) package installed, the admin can directly edit translations within the List View. This provides an excellent UX - because they can translate everything much much faster, without having to switch pages. -You can directly edit translations within the list view itself if you have the [Editable Columns](https://backpackforlaravel.com/products/editable-columns) package. -Once edited, the changes are saved to the database for persistence. All translations on the database have priority over the ones in the language files. +If you don't want that behavior you can disable it in the `config/backpack/translation-manager.php` file by setting `use_editable_columns => false`. +If you don't find that file, see above the optional steps to publish the config files. + +Once edited, the changes are saved to the database for persistence. All translations on the database have priority over the ones in the language files. This means that you can safely let your admin edit translations, in production. Your auto-deploys will continue working, because no files get edited, only DB entries. ## Security @@ -89,16 +102,17 @@ If you discover any security related issues, please email cristian.tabacitu@back ## Credits - [Antonio Almeida](https://github.com/promatik) +- [Pedro Martins](https://github.com/pxpm) - [Cristian Tabacitu](https://github.com/tabacitu) - [All Contributors][link-contributors] ## License -This project was released under MIT License, so you can install it on top of any Backpack & Laravel project. Please see the [license file](license.md) for more information. +This project was released under MIT License, so you can install it on top of any Backpack & Laravel project. Please see the [license file](https://backpackforlaravel.com/products/translation-manager/license.md) for more information. [ico-version]: https://img.shields.io/packagist/v/backpack/translation-manager.svg?style=flat-square [ico-downloads]: https://img.shields.io/packagist/dt/backpack/translation-manager.svg?style=flat-square -[link-packagist]: https://packagist.org/packages/backpack/translation-manager -[link-downloads]: https://packagist.org/packages/backpack/translation-manager +[link-author]: https://github.com/laravel-backpack [link-contributors]: ../../contributors +[link-downloads]: https://packagist.org/packages/backpack/translation-manager diff --git a/resources/lang/en/language_manager.php b/resources/lang/en/translation_manager.php similarity index 93% rename from resources/lang/en/language_manager.php rename to resources/lang/en/translation_manager.php index ecc0ec5..08bd07b 100644 --- a/resources/lang/en/language_manager.php +++ b/resources/lang/en/translation_manager.php @@ -15,7 +15,7 @@ 'database' => 'database', 'file' => 'file', - 'show-all-languages' => 'show all languages', + 'show_all_languages' => 'show all languages', 'revert' => 'revert', 'revert_confirm' => 'Are you sure you want to revert this translation?', diff --git a/resources/lang/pt/language_manager.php b/resources/lang/pt/translation_manager.php similarity index 93% rename from resources/lang/pt/language_manager.php rename to resources/lang/pt/translation_manager.php index 25e3075..d9c7ce2 100644 --- a/resources/lang/pt/language_manager.php +++ b/resources/lang/pt/translation_manager.php @@ -15,7 +15,7 @@ 'database' => 'base de dados', 'file' => 'ficheiro', - 'show-all-languages' => 'mostrar todos os idiomas', + 'show_all_languages' => 'mostrar todos os idiomas', 'revert' => 'reverter', 'revert_confirm' => 'Tem a certeza que quer reverter esta tradução?', diff --git a/resources/views/columns/language-preview-table.blade.php b/resources/views/columns/translation-preview-table.blade.php similarity index 100% rename from resources/views/columns/language-preview-table.blade.php rename to resources/views/columns/translation-preview-table.blade.php diff --git a/resources/views/fields/language-edit-field.blade.php b/resources/views/fields/translation-edit-field.blade.php similarity index 98% rename from resources/views/fields/language-edit-field.blade.php rename to resources/views/fields/translation-edit-field.blade.php index 3a597af..2d4e23f 100644 --- a/resources/views/fields/language-edit-field.blade.php +++ b/resources/views/fields/translation-edit-field.blade.php @@ -30,7 +30,7 @@ class="form-select"> - + diff --git a/routes/language-manager.php b/routes/translation-manager.php similarity index 100% rename from routes/language-manager.php rename to routes/translation-manager.php diff --git a/src/Http/Controllers/LanguageManagerCrudController.php b/src/Http/Controllers/TranslationManagerCrudController.php similarity index 85% rename from src/Http/Controllers/LanguageManagerCrudController.php rename to src/Http/Controllers/TranslationManagerCrudController.php index 140bf14..6a91cef 100644 --- a/src/Http/Controllers/LanguageManagerCrudController.php +++ b/src/Http/Controllers/TranslationManagerCrudController.php @@ -4,7 +4,7 @@ use Backpack\CRUD\app\Http\Controllers\CrudController; use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; -use Backpack\TranslationManager\Models\TranslationLineOriginal; +use Backpack\TranslationManager\Models\TranslationLine; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\App; use Illuminate\Support\Str; @@ -20,10 +20,7 @@ class TranslationManagerCrudController extends CrudController use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; - use \Backpack\TranslationManager\Http\Operations\MinorUpdateOperation; - - private $minorUpdateEntry = null; - private $minorUpdateRequest = null; + use \Backpack\TranslationManager\Http\Operations\CanUseEditableColumns; /** * Setup @@ -194,43 +191,4 @@ public function setupFilters(): void CRUD::addClause('where', 'database', $option === 'database'); }); } - - /** - * Override the parent method to customize the saving - */ - public function saveMinorUpdateEntry() - { - $entry = $this->minorUpdateEntry; - $request = $this->minorUpdateRequest; - $locale = App::getLocale(); - - // update - if ($entry->id_database) { - $text = $entry->text; - $text[$locale] = $request->value; - - $entry = TranslationLineOriginal::find($entry->id_database); - $entry->text = $text; - $entry->save(); - } - - // create - else { - [$group, $key] = explode('.', $request->id); - - TranslationLineOriginal::create([ - 'group' => $group, - 'key' => $key, - 'text' => [ - $locale => $request->value, - ], - ]); - } - - // fetch the entry from sushi - $entry = TranslationLine::find($request->id); - $entry->database = true; - - return $entry; - } } diff --git a/src/Http/Operations/CanUseEditableColumns.php b/src/Http/Operations/CanUseEditableColumns.php new file mode 100644 index 0000000..a259013 --- /dev/null +++ b/src/Http/Operations/CanUseEditableColumns.php @@ -0,0 +1,66 @@ +minorUpdateEntry; + $request = $this->minorUpdateRequest; + $locale = App::getLocale(); + + // update + if ($entry->id_database) { + $text = $entry->text; + $text[$locale] = $request->value; + + $entry = TranslationLineOriginal::find($entry->id_database); + $entry->text = $text; + $entry->save(); + } else { + [$group, $key] = explode('.', $request->id); + + TranslationLineOriginal::create([ + 'group' => $group, + 'key' => $key, + 'text' => [ + $locale => $request->value, + ], + ]); + } + + // fetch the entry from sushi + $entry = TranslationLine::find($request->id); + $entry->database = true; + + return $entry; + } + } +} else { + trait CanUseEditableColumns + { + private function editableColumnsEnabled(): bool + { + return false; + } + } +} diff --git a/src/Http/Operations/MinorUpdateOperation.php b/src/Http/Operations/MinorUpdateOperation.php deleted file mode 100644 index d4c49fa..0000000 --- a/src/Http/Operations/MinorUpdateOperation.php +++ /dev/null @@ -1,23 +0,0 @@ -