Skip to content

Commit

Permalink
feat: adding no-overwrite flag (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvdigitalaaank authored Feb 3, 2025
1 parent bb4d968 commit 158a160
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion database/migrations/create_phrases_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function up(): void
{
Schema::create('ltu_phrases', function (Blueprint $table) {
$table->id();
$table->char('uuid', 36);
$table->char('uuid', 36);;
$table->foreignIdFor(Translation::class)->constrained('ltu_translations')->cascadeOnDelete();
$table->foreignIdFor(TranslationFile::class)->constrained('ltu_translation_files')->cascadeOnDelete();
$table->foreignIdFor(Phrase::class)->nullable()->constrained('ltu_phrases')->cascadeOnDelete();
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/SyncPhrasesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class SyncPhrasesAction
{
public static function execute(Translation $source, $key, $value, $locale, $file): void
public static function execute(Translation $source, $key, $value, $locale, $file, bool $overwrite = true): void
{
if (is_array($value) && empty($value)) {
return;
Expand Down Expand Up @@ -36,8 +36,8 @@ public static function execute(Translation $source, $key, $value, $locale, $file
]);

$key = config('translations.include_file_in_key') && ! $isRoot ? "{$translationFile->name}.{$key}" : $key;

$translation->phrases()->updateOrCreate([
$method = $overwrite ? 'updateOrCreate' : 'firstOrCreate';
$translation->phrases()->$method([
'key' => $key,
'group' => $translationFile->name,
'translation_file_id' => $translationFile->id,
Expand Down
10 changes: 8 additions & 2 deletions src/Console/Commands/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class ImportTranslationsCommand extends Command
{
public TranslationsManager $manager;

protected $signature = 'translations:import {--F|fresh : Truncate all translations and phrases before importing}';
private bool $overwrite = true;

protected $signature = 'translations:import {--F|fresh : Truncate all translations and phrases before importing} {--no-overwrite : Do not overwrite existing translations}';

protected $description = 'Sync translation all keys from the translation files to the database';

Expand All @@ -40,6 +42,10 @@ public function handle(): void
$this->truncateTables();
}

if ($this->option('no-overwrite')) {
$this->overwrite = false;
}

$translation = $this->createOrGetSourceLanguage();

$this->info('Importing translations...'.PHP_EOL);
Expand Down Expand Up @@ -110,7 +116,7 @@ public function syncTranslations(Translation $translation, string $locale): void
{
foreach ($this->manager->getTranslations($locale) as $file => $translations) {
foreach (Arr::dot($translations) as $key => $value) {
SyncPhrasesAction::execute($translation, $key, $value, $locale, $file);
SyncPhrasesAction::execute($translation, $key, $value, $locale, $file, $this->overwrite);
}
}

Expand Down

0 comments on commit 158a160

Please sign in to comment.