diff --git a/app/Listeners/Update/V30/Version3013.php b/app/Listeners/Update/V30/Version3013.php new file mode 100644 index 000000000..4bda06ab4 --- /dev/null +++ b/app/Listeners/Update/V30/Version3013.php @@ -0,0 +1,71 @@ +skipThisUpdate($event)) { + return; + } + + Log::channel('stdout')->info('Updating to 3.0.13 version...'); + + DB::transaction(function () { + $types = $this->getTypesByAllowedTranslations(); + + foreach ($types as $type => $translations) { + DB::table('categories')->whereIn('type', $translations)->update(['type' => $type]); + } + }); + + Log::channel('stdout')->info('Done!'); + } + + protected function getTypesByAllowedTranslations(): array + { + $types = $this->getCategoryTypes(false); + $lang_codes = array_keys(language()->allowed()); + + foreach ($types as $type => $trans_name) { + $translations_for_type = []; + + foreach ($lang_codes as $lang_code) { + $translation = trans_choice($trans_name, 1, locale: $lang_code); + + if ($translation === $trans_name) { + continue; + } + + $translations_for_type[] = $translation; + } + + $types[$type] = $translations_for_type; + } + + /** + * Example: en-GB es-ES + * 'income' => ['Income', 'Ingresos'] + */ + return $types; + } +} diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 498fe7a85..9593392f5 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -21,6 +21,7 @@ class Event extends Provider 'App\Listeners\Update\V30\Version305', 'App\Listeners\Update\V30\Version307', 'App\Listeners\Update\V30\Version309', + 'App\Listeners\Update\V30\Version3013', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/app/Traits/Categories.php b/app/Traits/Categories.php index 14789fdd8..c2ee8f5fb 100644 --- a/app/Traits/Categories.php +++ b/app/Traits/Categories.php @@ -7,7 +7,7 @@ use Illuminate\Support\Str; trait Categories { - public function getCategoryTypes(): array + public function getCategoryTypes(bool $translate = true): array { $types = []; $configs = config('type.category'); @@ -21,7 +21,7 @@ trait Categories $name = $attr['alias'] . '::' . $name; } - $types[$type] = trans_choice($name, 1); + $types[$type] = $translate ? trans_choice($name, 1) : $name; } return $types;