Skip to content

Commit 39a077c

Browse files
committed
fix localization
1 parent 1dc21b5 commit 39a077c

File tree

2 files changed

+71
-36
lines changed

2 files changed

+71
-36
lines changed

packages/localization/src/Filament/Resources/LocalizationResource.php

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,41 +75,52 @@ public static function form(Schema $schema): Schema
7575
if ($state) {
7676
$language = StaticLanguage::find($state);
7777
if ($language) {
78-
$set('locale_variant', $language->alpha2);
78+
$mainLocale = $language->alpha2 . '_' . strtoupper($language->alpha2);
79+
$set('locale_variant', $mainLocale);
7980
}
8081
}
8182
}),
8283
TextInput::make('title')
8384
->label(__('localization::fields.title'))
8485
->required()
85-
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
86+
->afterStateUpdated(fn(Set $set, ?string $state) => $set('slug', Str::slug($state))),
8687
TextInput::make('slug')
8788
->label(__('localization::fields.slug'))
8889
->required(),
8990
Select::make('locale_variant')
9091
->label('Locale Variant')
9192
->options(function ($get) {
9293
$languageId = $get('language_id');
93-
if (! $languageId) {
94+
if (!$languageId) {
9495
return [];
9596
}
9697

9798
$language = StaticLanguage::find($languageId);
98-
if (! $language) {
99+
if (!$language) {
99100
return [];
100101
}
101102

102103
$baseLanguage = $language->alpha2;
104+
$mainLocale = $baseLanguage . '_' . strtoupper($baseLanguage);
103105

104106
$locales = StaticLocale::where('language_id', $languageId)->with('country')->get();
105107

106-
$options = [
107-
$baseLanguage => $language->common_name.' ('.$baseLanguage.')',
108-
];
108+
$options = [];
109109

110110
foreach ($locales as $locale) {
111111
$countryName = $locale->country ? $locale->country->common_name : 'Unknown';
112-
$options[$locale->locale] = $language->common_name.' ('.$countryName.')';
112+
113+
// Wenn es der Haupt-Locale ist (z.B. de_DE), zeige "Standard"
114+
if ($locale->locale === $mainLocale) {
115+
$options[$locale->locale] = $language->common_name . ' (Standard)';
116+
} else {
117+
$options[$locale->locale] = $language->common_name . ' (' . $countryName . ')';
118+
}
119+
}
120+
121+
// Falls kein Locale in der DB existiert, füge den Haupt-Locale hinzu
122+
if (empty($options)) {
123+
$options[$mainLocale] = $language->common_name . ' (Standard)';
113124
}
114125

115126
return $options;
@@ -193,35 +204,40 @@ public static function table(Table $table): Table
193204
{
194205
return $table
195206
->columns([
207+
// Basic Info Group
196208
IconColumn::make('table_flag')
197209
->label('Flag')
198-
->icon(fn (string $state): string => $state),
210+
->icon(fn(string $state): string => $state),
199211
TextColumn::make('display_name')
200212
->label(__('localization::fields.language'))
201-
->searchable(),
213+
->searchable()
214+
->width(150),
202215
TextColumn::make('locale_variant')
203-
->label('Locale Variant'),
204-
TextColumn::make('display_name')
205-
->label(__('localization::fields.title'))
206-
->searchable(),
216+
->label('Locale')
217+
->width(100),
207218
TextColumn::make('slug')
208-
->label(__('localization::fields.slug')),
209-
TextColumn::make('fallbackLanguage.title')
210-
->label(__('localization::fields.fallback_language')),
219+
->label(__('localization::fields.slug'))
220+
->width(120),
221+
// Status Toggles Group
211222
ToggleColumn::make('is_active_admin')
212-
->label(__('localization::fields.is_activ_admin')),
223+
->label('Admin')
224+
->width(80),
213225
ToggleColumn::make('is_active_frontend')
214-
->label(__('localization::fields.is_activ_frontend')),
226+
->label('Frontend')
227+
->width(80),
215228
ToggleColumn::make('is_default')
216-
->label(__('localization::fields.is_default'))
229+
->label('Default')
230+
->width(80)
217231
->afterStateUpdated(function ($state, $record) {
218232
if ($state) {
219233
static::getModel()::where('id', '!=', $record->id)
220234
->update(['is_default' => false]);
221235
}
222236
}),
237+
// Config Toggles Group
223238
ToggleColumn::make('language_settings->use_native_names')
224-
->label(__('localization::fields.use_native_names'))
239+
->label('Native')
240+
->width(80)
225241
->getStateUsing(function ($record) {
226242
return $record->getLanguageSetting('use_native_names');
227243
})
@@ -231,7 +247,8 @@ public static function table(Table $table): Table
231247
$record->update(['language_settings' => $settings]);
232248
}),
233249
ToggleColumn::make('language_settings->show_regional_variants')
234-
->label(__('localization::fields.show_regional_variants'))
250+
->label('Regional')
251+
->width(80)
235252
->getStateUsing(function ($record) {
236253
return $record->getLanguageSetting('show_regional_variants');
237254
})
@@ -241,7 +258,8 @@ public static function table(Table $table): Table
241258
$record->update(['language_settings' => $settings]);
242259
}),
243260
ToggleColumn::make('language_settings->use_country_translations')
244-
->label(__('localization::fields.use_country_translations'))
261+
->label('Country')
262+
->width(80)
245263
->getStateUsing(function ($record) {
246264
return $record->getLanguageSetting('use_country_translations');
247265
})
@@ -260,7 +278,7 @@ public static function table(Table $table): Table
260278
->groups([
261279
Group::make('language.common_name')
262280
->label(__('localization::fields.language'))
263-
->getTitleFromRecordUsing(fn (Localization $record): string => $record->language->common_name)
281+
->getTitleFromRecordUsing(fn(Localization $record): string => $record->language->common_name)
264282
->collapsible(),
265283
])
266284
->defaultGroup('language.common_name')

packages/localization/src/Models/Localization.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getLocaleAttribute(): string
7979
if (str_contains($locale, '_')) {
8080
$parts = explode('_', $locale, 2);
8181
if (count($parts) === 2) {
82-
$locale = $parts[0].'_'.strtoupper($parts[1]);
82+
$locale = $parts[0] . '_' . strtoupper($parts[1]);
8383
}
8484
}
8585

@@ -93,7 +93,7 @@ public function getLanguageSetting(string $key): bool
9393
{
9494
$settings = $this->language_settings ?? [];
9595

96-
if (! isset($settings[$key])) {
96+
if (!isset($settings[$key])) {
9797
return config("localization.language_selector.{$key}", true);
9898
}
9999

@@ -111,7 +111,7 @@ public function getDisplayNameAttribute(): string
111111

112112
$baseName = $useNativeNames ? $this->language->native_name : $this->language->common_name;
113113

114-
if (! $showRegionalVariants) {
114+
if (!$showRegionalVariants) {
115115
return $baseName;
116116
}
117117

@@ -121,8 +121,8 @@ public function getDisplayNameAttribute(): string
121121
$countryCode = strtolower($parts[1] ?? '');
122122

123123
$country = StaticCountry::where('alpha2', $countryCode)->first();
124-
if (! $country) {
125-
return $baseName.' ('.strtoupper($countryCode).')';
124+
if (!$country) {
125+
return $baseName . ' (' . strtoupper($countryCode) . ')';
126126
}
127127

128128
$countryName = $country->common_name; // Default fallback
@@ -134,7 +134,7 @@ public function getDisplayNameAttribute(): string
134134
}
135135
}
136136

137-
return $baseName.' ('.$countryName.')';
137+
return $baseName . ' (' . $countryName . ')';
138138
}
139139

140140
return $baseName;
@@ -147,7 +147,7 @@ public function getDisplayFlagAttribute(): string
147147
{
148148
$showRegionalVariants = $this->getLanguageSetting('show_regional_variants');
149149

150-
if (! $showRegionalVariants) {
150+
if (!$showRegionalVariants) {
151151
return $this->language->flag_icon;
152152
}
153153

@@ -157,7 +157,25 @@ public function getDisplayFlagAttribute(): string
157157
$countryCode = strtolower($parts[1] ?? '');
158158

159159
if ($countryCode && $this->flagExists($countryCode)) {
160-
return 'flag-'.$countryCode;
160+
return 'flag-' . $countryCode;
161+
}
162+
}
163+
164+
return $this->language->flag_icon;
165+
}
166+
167+
/**
168+
* Get the table flag for this localization (always shows regional variant)
169+
*/
170+
public function getTableFlagAttribute(): string
171+
{
172+
$locale = $this->locale;
173+
if (str_contains($locale, '_')) {
174+
$parts = explode('_', $locale, 2);
175+
$countryCode = strtolower($parts[1] ?? '');
176+
177+
if ($countryCode && $this->flagExists($countryCode)) {
178+
return 'flag-' . $countryCode;
161179
}
162180
}
163181

@@ -169,12 +187,12 @@ public function getDisplayFlagAttribute(): string
169187
*/
170188
public function flagExists(string $flagCode): bool
171189
{
172-
$packagePath = base_path('packages/flag-icons-circle/resources/svg/'.$flagCode.'.svg');
190+
$packagePath = base_path('packages/flag-icons-circle/resources/svg/' . $flagCode . '.svg');
173191
if (file_exists($packagePath)) {
174192
return true;
175193
}
176194

177-
$publicPath = public_path('vendor/flag-icons-circle/'.$flagCode.'.svg');
195+
$publicPath = public_path('vendor/flag-icons-circle/' . $flagCode . '.svg');
178196

179197
return file_exists($publicPath);
180198
}
@@ -227,10 +245,9 @@ private function getCountryFlag(): string
227245
$countryCode = $languageToCountry[$this->language->alpha2] ?? $this->language->alpha2;
228246

229247
if ($this->flagExists($countryCode)) {
230-
return 'flag-'.$countryCode;
248+
return 'flag-' . $countryCode;
231249
}
232250

233-
// Last resort: return original language flag
234251
return $this->language->flag_icon;
235252
}
236253
}

0 commit comments

Comments
 (0)