Skip to content

Commit

Permalink
Rename: Language Manager > Translation Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
promatik committed Apr 1, 2024
2 parents 4c414df + 50c1194 commit 130d509
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
38 changes: 19 additions & 19 deletions license.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# The MIT License (MIT)
MIT License

Copyright (c) Cristian Tabacitu <[email protected]>
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.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ parameters:
- routes/

excludePaths:
- src/Http/Operations/MinorUpdateOperation.php
- src/Http/Operations/CanUseEditableColumns.php

level: 5
88 changes: 51 additions & 37 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 "<x-backpack::menu-item title=\"Translation Managers\" icon=\"la la-stream\" :link=\"backpack_url('translation-manager')\" />"
php artisan backpack:add-menu-content "<x-backpack::menu-item title=\"Translation Manager\" icon=\"la la-stream\" :link=\"backpack_url('translation-manager')\" />"
```

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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class="form-select">
<label class="form-switch switch switch-sm switch-label switch-pill switch-primary mb-0">
<input type="checkbox" class="switch-input form-check-input" id="switch_translation_edit_field">
</label>
<label class="font-weight-normal mb-0 ml-2" for="switch_translation_edit_field">{{ ucfirst(__('backpack.translation-manager::translation_manager.show-all-translations')) }}</label>
<label class="font-weight-normal mb-0 ml-2" for="switch_translation_edit_field">{{ ucfirst(__('backpack.translation-manager::translation_manager.show_all_languages')) }}</label>
</div>
</div>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
66 changes: 66 additions & 0 deletions src/Http/Operations/CanUseEditableColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Backpack\TranslationManager\Http\Operations;

use Backpack\TranslationManager\Models\TranslationLine;
use Backpack\TranslationManager\Models\TranslationLineOriginal;
use Illuminate\Support\Facades\App;

if (class_exists(\Backpack\EditableColumns\AddonServiceProvider::class)) {
trait CanUseEditableColumns
{
use \Backpack\EditableColumns\Http\Controllers\Operations\MinorUpdateOperation;

private $minorUpdateEntry = null;
private $minorUpdateRequest = null;

private function editableColumnsEnabled(): bool
{
return config('backpack.translation-manager.use_editable_columns');
}

/**
* 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();
} 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;
}
}
}
Loading

0 comments on commit 130d509

Please sign in to comment.