Skip to content

Commit

Permalink
import all phrases from source
Browse files Browse the repository at this point in the history
  • Loading branch information
benaja committed Jan 13, 2024
1 parent 234630c commit fbed735
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
25 changes: 24 additions & 1 deletion src/Console/Commands/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ public function syncTranslations(Translation $translation, string $locale): void
$this->syncPhrases($translation, $key, $value, $locale, $file);
}
}

if ($locale === config('translations.source_language')) {
return;
}

$this->syncMissingTranslations($translation, $locale);
}

public function syncMissingTranslations(Translation $source, string $locale): void
{
$language = Language::where('code', $locale)->first();
$translation = Translation::firstOrCreate([
'language_id' => $language->id,
'source' => false,
]);

$source->load('phrases.translation', 'phrases.file');

$source->phrases->each(function ($phrase) use ($translation, $locale) {
if (! $translation->phrases()->where('key', $phrase->key)->first()) {
$this->syncPhrases($phrase->translation, $phrase->key, '', $locale, $phrase->file->name.'.'.$phrase->file->extension);
}
});
}

public function syncPhrases(Translation $source, $key, $value, $locale, $file): void
Expand All @@ -102,7 +125,7 @@ public function syncPhrases(Translation $source, $key, $value, $locale, $file):
$language = Language::where('code', $locale)->first();

if (! $language) {
$this->error(PHP_EOL."Language with code $locale not found");
$this->error(PHP_EOL."Language with code {$locale} not found");

exit;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Livewire/PhraseList.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ public function getPhrases(): LengthAwarePaginator
})
->when($this->status, function (Builder $query) {
if ($this->status == 1) {
$query->whereNotNull('value');
$query->where(fn($query) => $query
->where('value', '<>', '')
->orWhereNull('value'));
} elseif ($this->status == 2) {
$query->whereNull('value');
$query->where(fn($query) => $query
->where('value', '=', '')
->orWhereNull('value'));
}
})
->paginate($this->perPage)->onEachSide(0);
Expand Down
4 changes: 2 additions & 2 deletions src/Livewire/TranslationsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function getTranslations(): LengthAwarePaginator
public function getTranslationProgressPercentage(Translation $translation): float
{
$phrases = $translation->phrases()->toBase()
->selectRaw('COUNT(CASE WHEN value IS NOT NULL THEN 1 END) AS translated')
->selectRaw('COUNT(CASE WHEN value IS NULL THEN 1 END) AS untranslated')
->selectRaw('COUNT(CASE WHEN value IS NOT NULL AND value <> \'\' THEN 1 END) AS translated')
->selectRaw('COUNT(CASE WHEN value IS NULL OR value <> \'\' THEN 1 END) AS untranslated')
->selectRaw('COUNT(*) AS total')
->first();

Expand Down

0 comments on commit fbed735

Please sign in to comment.