Skip to content
Advertisement

joedixon/laravel-translation not updating

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: Console view Routes:

ticate:sanctum    |
|        | POST     | api/login                                |                               | AppHttpControllersAuthAuthController@login                             | api
                  |
|        | POST     | api/register                             |                               | AppHttpControllersAuthAuthController@register                          | api
                  |
|        | GET|HEAD | api/user                                 |                               | Closure                                                                    | api
                  |
|        |          |                                          |                               |                                                                            | AppHttpMiddlewareAuthenticate:sanctum    |
|        | GET|HEAD | home                                     | home                          | AppHttpControllersHomeController@index                                  | web
                  |
|        |          |                                          |                               |                                                                            | AppHttpMiddlewareAuthenticate            |
|        | GET|HEAD | languages                                | languages.index               | JoeDixonTranslationHttpControllersLanguageController@index             | web
                  |
|        | POST     | languages                                | languages.store               | JoeDixonTranslationHttpControllersLanguageController@store             | web
                  |
|        | GET|HEAD | languages/create                         | languages.create              | JoeDixonTranslationHttpControllersLanguageController@create            | web
                  |
|        | POST     | languages/{language}                     | languages.translations.update | JoeDixonTranslationHttpControllersLanguageTranslationController@update | web
                  |
|        | GET|HEAD | languages/{language}/translations        | languages.translations.index  | JoeDixonTranslationHttpControllersLanguageTranslationController@index  | web
                  |
|        | POST     | languages/{language}/translations        | languages.translations.store  | JoeDixonTranslationHttpControllersLanguageTranslationController@store  | web
                  |
|        | GET|HEAD | languages/{language}/translations/create | languages.translations.create | JoeDixonTranslationHttpControllersLanguageTranslationController@create | web
                  |
|        | GET|HEAD | login                                    | login                         | AppHttpControllersAuthLoginController@showLoginForm                    | web

Languages Translation Controller

<?php

namespace JoeDixonTranslationHttpControllers;

use IlluminateHttpRequest;
use IlluminateRoutingController;
use IlluminateSupportCollection;
use IlluminateSupportStr;
use JoeDixonTranslationDriversTranslation;
use JoeDixonTranslationHttpRequestsTranslationRequest;

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');
});

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement