joedixon/laravel 翻译未更新

Posted

技术标签:

【中文标题】joedixon/laravel 翻译未更新【英文标题】:joedixon/laravel-translation not updating 【发布时间】:2021-11-19 11:28:22 【问题描述】:

我刚刚在 Laravel 版本 8 中安装了一个 Joedixon/Laravel 翻译包。 它显示了单词,但是当我想更新时它不起作用。 我做了文档中所说的所有事情,并且我已经阅读了有关它的这篇文章,但它也没有奏效。 https://github.com/joedixon/laravel-translation/issues/82

注意事项:

1:我把驱动改成数据库了。

2:添加新语言效果很好

3:在添加新翻译时,它会搜索 http://localhost/languages/en 而不是 http://localhost/project_name/languages/en

当我尝试更新一个单词时,它显示 404 not found in the console: 路线:

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

语言翻译控制器

<?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];
    


路线列表

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

【问题讨论】:

你使用的是路由模型绑定(RMB)吗? 不只是普通路由 LanguageTranslationController@update 如何获取语言是 findOrfail 吗? 我已经编辑了问题并添加了 LanguageTranslationsCONtroller 【参考方案1】:

你跑了吗

php artisan optimize

自安装后清除路由缓存并允许它拾取您添加到 web.php 的新路由?

【讨论】:

谢谢。是的,但仍然显示同样的问题【参考方案2】:

我解决了完全不同地方的问题

它在 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
                )))
            
        ,

刚刚添加了("/MY_PROJECT_NAME/public".concat("/"),问题就解决了

【讨论】:

以上是关于joedixon/laravel 翻译未更新的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Angular 4 中翻译 mat-paginator?

在后端使用NodeJS的Put方法不更新Angular7中的MongoDB

Visual Studio C# 更新本地化语言 resx

最小角回归 LARS算法包的用法以及模型参数的选择

Angular 10 中未显示验证消息

未找到默认实体翻译的翻译表单