I have just installed a Joedixon/Laravel-translation package in Laravel version 8. It shows the words but when I want to update it will not work. I did everything that was said in the documentation and also I've read this article about it but it also didn't work. https://github.com/joedixon/laravel-translation/issues/82
NOTES:
1: I changed the driver to database.
2: adding a new Language works fine
3: while adding new translation it searches in http://localhost/languages/en instead of http://localhost/project_name/languages/en
When I try to update a word it says 404 not found in the console: Routes:
ticate:sanctum |
| | POST | api/login | | App\Http\Controllers\Auth\AuthController@login | api
|
| | POST | api/register | | App\Http\Controllers\Auth\AuthController@register | api
|
| | GET|HEAD | api/user | | Closure | api
|
| | | | | | App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web
|
| | | | | | App\Http\Middleware\Authenticate |
| | GET|HEAD | languages | languages.index | JoeDixon\Translation\Http\Controllers\LanguageController@index | web
|
| | POST | languages | languages.store | JoeDixon\Translation\Http\Controllers\LanguageController@store | web
|
| | GET|HEAD | languages/create | languages.create | JoeDixon\Translation\Http\Controllers\LanguageController@create | web
|
| | POST | languages/{language} | languages.translations.update | JoeDixon\Translation\Http\Controllers\LanguageTranslationController@update | web
|
| | GET|HEAD | languages/{language}/translations | languages.translations.index | JoeDixon\Translation\Http\Controllers\LanguageTranslationController@index | web
|
| | POST | languages/{language}/translations | languages.translations.store | JoeDixon\Translation\Http\Controllers\LanguageTranslationController@store | web
|
| | GET|HEAD | languages/{language}/translations/create | languages.translations.create | JoeDixon\Translation\Http\Controllers\LanguageTranslationController@create | web
|
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web
Languages Translation Controller
<?php
namespace JoeDixon\Translation\Http\Controllers;
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;
class LanguageTranslationController extends Controller
{
private $translation;
public function __construct(Translation $translation)
{
$this->translation = $translation;
}
public function index(Request $request, $language)
{
// 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'));
}
public function create(Request $request, $language)
{
return view('translation::languages.translations.create', compact('language'));
}
public function store(TranslationRequest $request, $language)
{
if ($request->filled('group')) {
$namespace = $request->has('namespace') && $request->get('namespace') ? "{$request->get('namespace')}::" : '';
$this->translation->addGroupTranslation($language, "{$namespace}{$request->get('group')}", $request->get('key'), $request->get('value') ?: '');
} else {
$this->translation->addSingleTranslation($language, 'single', $request->get('key'), $request->get('value') ?: '');
}
return redirect()
->route('languages.translations.index', $language)
->with('success', __('translation::translation.translation_added'));
}
public function update(Request $request, $language)
{
if (! Str::contains($request->get('group'), 'single')) {
$this->translation->addGroupTranslation($language, $request->get('group'), $request->get('key'), $request->get('value') ?: '');
} else {
$this->translation->addSingleTranslation($language, $request->get('group'), $request->get('key'), $request->get('value') ?: '');
}
return ['success' => true];
}
}
List of routes
<?php
Route::group(config('translation.route_group_config') + ['namespace' => 'JoeDixon\\Translation\\Http\\Controllers'], function ($router) {
$router->get(config('translation.ui_url'), 'LanguageController@index')
->name('languages.index');
$router->get(config('translation.ui_url').'/create', 'LanguageController@create')
->name('languages.create');
$router->post(config('translation.ui_url'), 'LanguageController@store')
->name('languages.store');
$router->get(config('translation.ui_url').'/{language}/translations', 'LanguageTranslationController@index')
->name('languages.translations.index');
$router->post(config('translation.ui_url').'/{language}', 'LanguageTranslationController@update')
->name('languages.translations.update');
$router->get(config('translation.ui_url').'/{language}/translations/create', 'LanguageTranslationController@create')
->name('languages.translations.create');
$router->post(config('translation.ui_url').'/{language}/translations', 'LanguageTranslationController@store')
->name('languages.translations.store');
});
I solve the Problem which was completely in different place
it's in public/vendor/translation/js/app.js
methods: {
setActive: function () {
this.isActive = !0, this.$refs.trans.focus()
},
storeTranslation: function () {
var e = this;
this.isActive = !1, this.hasChanged && (this.isLoading = !0, window.axios.post
("/MY_PROJECT_NAME/public".concat("/").concat(this.route, "/").concat(this.language), {
language: this.language,
group: this.group,
key: this.translationKey,
value: this.translation
}).then((function (t) {
e.hasSaved = !0, e.isLoading = !1, e.hasChanged = !1
})).catch((function (t) {
e.hasErrored = !0, e.isLoading = !1
})))
}
},
just added ("/MY_PROJECT_NAME/public".concat("/") and the problem solved