BRACKETS-by-TRIAD / craftable-pro-docs

Craftable PRO is an admin panel for your Laravel project build with InertiaJS, Vue and TailwindCSS.
https://docs.craftable.pro
MIT License
7 stars 1 forks source link

Translation list duplicated lines #86

Open roolian opened 5 months ago

roolian commented 5 months ago

Issue

After a scan, listing show the same string multiple times :

image

Edit

By adding this log in vendor/brackets/craftable-pro/src/Translations/Repositories/LanguageLineRepository.php

public function createLanguageLineIfDoesntExist($group, $key, $language = null, $text = null): LanguageLine | null
       {
            if (empty(trim($key))) {
                return null;
          }

        /** @var LanguageLine $translation */
        $languageLine = LanguageLine::withTrashed()
            ->where('group', $group)
            ->where('key', $key)
            ->first();

            if (str_contains($key, 'ame') && str_contains($key, 'irst')) {
                if ($languageLine) {
                    Log::info(["Scan: ".$group."/".$key, "Found key: ".$languageLine->key, $languageLine->key === $key ?  "Confirm match" : "New trans: ".$group."/".$key]);
                } else {
                    Log::info(["Scan: ".$group."/".$key, "New trans: ".$group."/".$key]);
                }
            }

         //...
        }

Its a problem with case-insensitive query because it always return the first match:

[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'New trans: craftable-pro/First name', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'Found key: First name', 2 => 'Confirm match', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'Found key: First name', 2 => 'Confirm match', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First Name', 1 => 'Found key: First name', 2 => 'New trans: craftable-pro/First Name', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First Name', 1 => 'Found key: First name', 2 => 'New trans: craftable-pro/First Name', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'Found key: First name', 2 => 'Confirm match', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'Found key: First name', 2 => 'Confirm match', )
[2024-05-03 09:05:37] local.INFO: array ( 0 => 'Scan: craftable-pro/First name', 1 => 'Found key: First name', 2 => 'Confirm match', )
[2024-05-03 09:05:38] local.INFO: array ( 0 => 'Scan: craftable-pro/First Name', 1 => 'Found key: First name', 2 => 'New trans: craftable-pro/First Name', )
[2024-05-03 09:05:38] local.INFO: array ( 0 => 'Scan: craftable-pro/First Name', 1 => 'Found key: First name', 2 => 'New trans: craftable-pro/First Name', )

roolian commented 5 months ago

Solution

Case sensitive can be set on column (for mysql) in the migration:

Schema::create('language_lines', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('group')->index();
            $table->text('key')->collation('latin7_general_cs');
            $table->json('text');
            $table->timestamps();
            $table->softDeletes();
        });

So in vendor/brackets/craftable-pro/src/Translations/Repositories/LanguageLineRepository.php

// because Laravel & MySQL are case-insensitive by default, let's double check we have the right $languageLine
        if ($languageLine && $languageLine->key === $key) {

Now i go back to my project 😄