Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TranslationServiceProvider.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Providers;
  3. use Closure;
  4. use Filament\Forms\Components\Field;
  5. use Filament\Navigation\NavigationGroup;
  6. use Filament\Resources\Components\Tab;
  7. use Filament\Tables\Columns\Column;
  8. use Illuminate\Contracts\Support\Htmlable;
  9. use Illuminate\Support\ServiceProvider;
  10. class TranslationServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * Register services.
  14. */
  15. public function register(): void
  16. {
  17. //
  18. }
  19. /**
  20. * Bootstrap services.
  21. */
  22. public function boot(): void
  23. {
  24. Field::macro('localizeLabel', function (string | Htmlable | Closure | null $customLabel = null): static {
  25. return TranslationServiceProvider::localizeLabelGeneric($this, $customLabel);
  26. });
  27. Column::macro('localizeLabel', function (string | Htmlable | Closure | null $customLabel = null): static {
  28. return TranslationServiceProvider::localizeLabelGeneric($this, $customLabel);
  29. });
  30. NavigationGroup::macro('localizeLabel', function () {
  31. $label = $this->getLabel();
  32. if (filled($label)) {
  33. $translatedLabel = translate($label);
  34. $this->label(ucfirst($translatedLabel));
  35. }
  36. return $this;
  37. });
  38. Tab::macro('localizeLabel', function () {
  39. $label = $this->getLabel();
  40. if (filled($label)) {
  41. $translatedLabel = translate($label);
  42. $this->label(ucfirst($translatedLabel));
  43. }
  44. return $this;
  45. });
  46. }
  47. public static function localizeLabelGeneric($object, string | Htmlable | Closure | null $customLabel = null)
  48. {
  49. $label = filled($customLabel) ? $customLabel : static::processedLabel($object->getLabel());
  50. $object->label(translate($label));
  51. return $object;
  52. }
  53. public static function processedLabel(Htmlable | null | string $label): string
  54. {
  55. if (str_ends_with($label, ' id')) {
  56. $label = str_replace(' id', '', $label);
  57. }
  58. return ucfirst($label);
  59. }
  60. }