kongulov / nova-tab-translatable

This package contains a NovaTabTranslatable class you can use to make any Nova field type translatable with tabs.
MIT License
79 stars 16 forks source link

File Download doesn't work when locale contains underscore #43

Open Rinze-Smits opened 9 months ago

Rinze-Smits commented 9 months ago

I use locales with an underscore, e.g. uk_UA and enUS. The various Field...Controllers assume that a locale doesn't contain an and get the original field by splitting the translated attribute name by _ and then using everything but first and last, e.g. translations_attribute_en_US should reference the attribute named attribute, but instead tries to use attribute_en. Perhaps checking if an attribute has a locale appended could be done by looping through the list of supported locales?

Rinze-Smits commented 9 months ago

The versions I use are all the latest as of now.

kongulov/nova-tab-translatable 2.1.3 spatie/laravel-translatable 6.6.1 laravel/nova 4.32.15 laravel/framework v10.46.0

Example code: config/tab-translatable.php

return [
    'source' => 'array',
    'locales' => [
        'en_US', 'uk_UA', 'fy_NL',
    ],

Model:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class TestModel extends Model
{
    use HasFactory;
    use HasTranslations;

    public $translatable = ['test_file'];
}

Migration:

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('test_models', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->json('test_file')->nullable();
        });

    }
    public function down(): void{Schema::dropIfExists('test_models');}
};

Resource:

<?php
namespace App\Nova;
use Kongulov\NovaTabTranslatable\NovaTabTranslatable;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;
class TestModel extends Resource
{
    public static $model = \App\Models\TestModel::class;
    public static $title = 'id';
    public static $search = ['id',];
    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
            NovaTabTranslatable::make([
                \Laravel\Nova\Fields\File::make('test_file'),
            ]),
        ];
    }
    public function cards(NovaRequest $request){return [];}
    public function filters(NovaRequest $request){return [];}
    public function lenses(NovaRequest $request){return [];}
    public function actions(NovaRequest $request){return [];}
}