Skip to content
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

Laravel 10 and inertia.js implementation #301

Open
wants to merge 3 commits into
base: 2.x
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor/
/node_modules
.phpunit.result.cache
.idea

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"require": {
"php": "^8.0",
"inertiajs/inertia-laravel": "^0.6.8",
"illuminate/support": "^8.0||^9.0||^10.0",
"laravel/legacy-factories": "^1.3"
},
Expand Down
4,291 changes: 2,533 additions & 1,758 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
$router->post(config('translation.ui_url'), 'LanguageController@store')
->name('languages.store');

$router->delete(config('translation.ui_url').'/destroy', 'LanguageController@destroy')
->name('languages.destroy');

$router->get(config('translation.ui_url').'/{language}/translations', 'LanguageTranslationController@index')
->name('languages.translations.index');

Expand Down
13 changes: 10 additions & 3 deletions src/Drivers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public function addLanguage($language, $name = null)
throw new LanguageExistsException(__('translation::errors.language_exists', ['language' => $language]));
}

$this->disk->makeDirectory("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."$language");
if (! $this->disk->exists("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."{$language}.json")) {
return $this->disk->makeDirectory("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."$language");
/*if (! $this->disk->exists("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."{$language}.json")) {
$this->saveSingleTranslations($language, collect(['single' => collect()]));
}
}*/
}

/**
Expand Down Expand Up @@ -265,6 +265,7 @@ public function saveGroupTranslations($language, $group, $translations)
if (Str::contains($group, '::')) {
return $this->saveNamespacedGroupTranslations($language, $group, $translations);
}

$this->disk->put("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."{$language}".DIRECTORY_SEPARATOR."{$group}.php", "<?php\n\nreturn ".var_export($translations, true).';'.\PHP_EOL);
}

Expand Down Expand Up @@ -366,4 +367,10 @@ public function getVendorGroupFilesFor($language)

return new Collection(Arr::flatten($vendorGroups));
}

public function deleteDirectory($language)
{

return $this->disk->deleteDirectory("{$this->languageFilesPath}".DIRECTORY_SEPARATOR."$language");
}
}
17 changes: 13 additions & 4 deletions src/Http/Controllers/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Routing\Controller;
use JoeDixon\Translation\Drivers\Translation;
use JoeDixon\Translation\Http\Requests\LanguageRequest;
use JoeDixon\Translation\Http\Requests\LanguageDestroyRequest;

class LanguageController extends Controller
{
Expand All @@ -20,20 +21,28 @@ public function index(Request $request)
{
$languages = $this->translation->allLanguages();

return view('translation::languages.index', compact('languages'));
return inertia('Control/Languages/Index', compact('languages'));
}

public function create()
{
return view('translation::languages.create');
return inertia('Control/Languages/Create');
}

public function store(LanguageRequest $request)
{
$this->translation->addLanguage($request->locale, $request->name);
$this->translation->addLanguage($request->validated()['locale'], $request->validated()['name']);

return redirect()
->route('languages.index')
->with('success', __('translation::translation.language_added'));
}
}

public function destroy(LanguageDestroyRequest $request)
{

$this->translation->deleteDirectory($request->validated()['language']);

return redirect()->back();
}
}
53 changes: 14 additions & 39 deletions src/Http/Controllers/LanguageTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use JoeDixon\Translation\Drivers\Translation;
use JoeDixon\Translation\Http\Requests\TranslationRequest;
use JoeDixon\Translation\Http\Requests\LanguageRequest;
use JoeDixon\Translation\Http\Requests\LanguageDestroyRequest;

class LanguageTranslationController extends Controller
class LanguageController extends Controller
{
private $translation;

Expand All @@ -18,56 +17,32 @@ public function __construct(Translation $translation)
$this->translation = $translation;
}

public function index(Request $request, $language)
public function index(Request $request)
{
// dd($this->translation->getSingleTranslationsFor('en'));
if ($request->has('language') && $request->get('language') !== $language) {
return redirect()
->route('languages.translations.index', ['language' => $request->get('language'), 'group' => $request->get('group'), 'filter' => $request->get('filter')]);
}

$languages = $this->translation->allLanguages();
$groups = $this->translation->getGroupsFor(config('app.locale'))->merge('single');
$translations = $this->translation->filterTranslationsFor($language, $request->get('filter'));

if ($request->has('group') && $request->get('group')) {
if ($request->get('group') === 'single') {
$translations = $translations->get('single');
$translations = new Collection(['single' => $translations]);
} else {
$translations = $translations->get('group')->filter(function ($values, $group) use ($request) {
return $group === $request->get('group');
});

$translations = new Collection(['group' => $translations]);
}
}

return view('translation::languages.translations.index', compact('language', 'languages', 'groups', 'translations'));
return inertia('Control/Languages/Index', compact('languages'));
}

public function create(Request $request, $language)
public function create()
{
return view('translation::languages.translations.create', compact('language'));
return inertia('Control/Languages/Create');
}

public function store(TranslationRequest $request, $language)
public function store(LanguageRequest $request)
{
$isGroupTranslation = $request->filled('group');

$this->translation->add($request, $language, $isGroupTranslation);
$this->translation->addLanguage($request->validated()['locale'], $request->validated()['name']);

return redirect()
->route('languages.translations.index', $language)
->with('success', __('translation::translation.translation_added'));
->route('languages.index')
->with('success', __('translation::translation.language_added'));
}

public function update(Request $request, $language)
public function destroy(LanguageDestroyRequest $request)
{
$isGroupTranslation = ! Str::contains($request->get('group'), 'single');

$this->translation->add($request, $language, $isGroupTranslation);
$this->translation->deleteDirectory($request->validated()['language']);

return ['success' => true];
return redirect()->back();
}
}
31 changes: 31 additions & 0 deletions src/Http/Requests/LanguageDestroyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace JoeDixon\Translation\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use JoeDixon\Translation\Rules\LanguageNotExists;

class LanguageDestroyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'language' => ['required', !(new LanguageNotExists)],
];
}
}
4 changes: 2 additions & 2 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ private function loadMigrations()
*/
private function loadTranslations()
{
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'translation');
$this->loadTranslationsFrom(lang_path(), 'translation');

$this->publishes([
__DIR__.'/../resources/lang' => resource_path('lang/vendor/translation'),
__DIR__.'/../resources/lang' => lang_path('vendor/translation'),
]);
}

Expand Down