Skip to content

Commit 6d72968

Browse files
committed
fix(localization): boolean display columns and single default locale
1 parent b4ae9b2 commit 6d72968

8 files changed

Lines changed: 111 additions & 169 deletions

File tree

packages/data/src/Models/StaticLanguage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,7 @@ public function getFlagIconAttribute(): string
190190
*/
191191
public function getDisplayNameAttribute(): string
192192
{
193-
$useNativeNames = config('localization.language_selector.use_native_names', true);
194-
195-
if (! $useNativeNames) {
196-
return $this->common_name;
197-
}
198-
199-
if (is_string($this->native_name) && ! empty($this->native_name)) {
193+
if (is_string($this->native_name) && $this->native_name !== '') {
200194
return $this->native_name;
201195
}
202196

packages/localization/README.md

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ display_flag ← use_country_icon
6767
| Column | Type | Default | Role |
6868
|--------|------|---------|------|
6969
| `locale_variant` | string | required | e.g. `de_CH`, `en_US`, `fr_CH` |
70-
| `language_settings` | JSON | `null` | Per-row overrides for name settings |
71-
| `use_country_icon` | boolean | `false` | Per-row: country vs language icon |
72-
73-
Global defaults for `language_settings` keys: `config/localization.php``language_selector`.
70+
| `use_native_names` | boolean | `true` | Native vs English language name |
71+
| `show_regional_variants` | boolean | `true` | Append country from locale in name |
72+
| `use_country_translations` | boolean | `true` | Translated country name in parentheses |
73+
| `use_country_icon` | boolean | `false` | Country vs language flag icon |
7474

7575
### Display name (`display_name`)
7676

@@ -83,8 +83,6 @@ Built by `Localization::getDisplayNameAttribute()`.
8383
| `StaticLanguage::native_name` | `StaticLanguage::common_name` (usually English) |
8484
| Deutsch | German |
8585

86-
Config: `language_selector.use_native_names` (default: `true`).
87-
8886
#### Regional (`show_regional_variants`)
8987

9088
When **on** and `locale_variant` contains `_` (e.g. `de_CH`), appends the country in parentheses:
@@ -94,7 +92,7 @@ When **on** and `locale_variant` contains `_` (e.g. `de_CH`), appends the countr
9492
| Deutsch (Schweiz) | Deutsch |
9593
| English (United States) | English |
9694

97-
No effect when the locale has no region suffix (`de` only). Config: `language_selector.show_regional_variants` (default: `true`).
95+
No effect when the locale has no region suffix (`de` only).
9896

9997
#### Country names (`use_country_translations`)
10098

@@ -105,8 +103,6 @@ Only when **Regional** is on. Turning Regional off also turns Country names off.
105103
| Translated name from `static_countries.translations` | `StaticCountry::common_name` |
106104
| Deutsch (Schweiz) | Deutsch (Switzerland) |
107105

108-
Config: `language_selector.use_country_translations` (default: `true`).
109-
110106
#### Name examples for `de_CH`
111107

112108
| Native | Regional | Country names | Result |
@@ -147,29 +143,12 @@ With `use_country_icon` on all three, all show CH—usually not desired for a mu
147143

148144
### Filament resource toggles
149145

150-
| Column label | Setting | Affects |
151-
|--------------|---------|---------|
152-
| Native | `language_settings.use_native_names` | Name |
153-
| Regional | `language_settings.show_regional_variants` | Name |
154-
| Country names | `language_settings.use_country_translations` | Name (parentheses) |
155-
| Country flag | `use_country_icon` (DB column) | Flag icon |
156-
157-
Per-row `language_settings` override global config when a key is set on that record.
158-
159-
### Global configuration
160-
161-
```php
162-
// config/localization.php
163-
'language_selector' => [
164-
'use_native_names' => true,
165-
'show_regional_variants' => true,
166-
'use_country_translations' => true,
167-
],
168-
```
169-
170-
Publish: `php artisan vendor:publish --tag=localization-config`
171-
172-
`use_country_icon` is only per localization row (migration default: `false`).
146+
| Column label | Database column | Affects |
147+
|--------------|-----------------|---------|
148+
| Native | `use_native_names` | Name |
149+
| Regional | `show_regional_variants` | Name |
150+
| Country names | `use_country_translations` | Name (parentheses) |
151+
| Country flag | `use_country_icon` | Flag icon |
173152

174153
### Programmatic access
175154

@@ -182,7 +161,8 @@ $localization->display_name; // e.g. "Deutsch (Schweiz)"
182161
$localization->display_flag; // e.g. "flag-de"
183162
$localization->table_flag; // same as display_flag
184163

185-
$localization->getLanguageSetting('show_regional_variants'); // row or config fallback
164+
$localization->show_regional_variants;
165+
$localization->use_country_icon;
186166
```
187167

188168
### Recommended defaults
@@ -200,7 +180,7 @@ $localization->getLanguageSetting('show_regional_variants'); // row or config fa
200180
| `src/Models/Localization.php` | Names + flag resolution |
201181
| `resources/views/lang-selector.blade.php` | Admin dropdown |
202182
| `src/Filament/Resources/LocalizationResource.php` | Form and table toggles |
203-
| `config/localization.php` | Global name defaults |
183+
| `config/localization.php` | Filament labels, navigation, panel |
204184
| `database/migrations/create_localizations_table.php.stub` | Schema |
205185

206186
## Localization panel

packages/localization/config/localization.php

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,7 @@
3636
|
3737
*/
3838
'tabs' => [
39-
'all' => [
40-
'label' => 'trans//core::core.all',
41-
'icon' => 'gmdi-filter-list',
42-
'query' => [],
43-
],
44-
'0' => [
45-
'label' => 'LTR',
46-
'icon' => 'gmdi-filter-list',
47-
'query' => [
48-
[
49-
'field' => 'type',
50-
'operator' => '=',
51-
'value' => 'LTR',
52-
],
53-
],
54-
],
55-
'1' => [
56-
'label' => 'RTL',
57-
'icon' => 'gmdi-filter-list',
58-
'query' => [
59-
[
60-
'field' => 'type',
61-
'operator' => '=',
62-
'value' => 'RTL',
63-
],
64-
],
65-
],
39+
6640
],
6741

6842
/*
@@ -77,27 +51,4 @@
7751
'navigation_group' => 'trans//core::core.system',
7852
'enable-panel' => false,
7953

80-
/*
81-
|--------------------------------------------------------------------------
82-
| Language Selector
83-
|--------------------------------------------------------------------------
84-
|
85-
| Configuration for the language selector component.
86-
|
87-
*/
88-
/*
89-
| Defaults for display_name (language selector labels). Can be overridden per
90-
| localization row in language_settings. Does NOT control flags — see use_country_icon.
91-
|
92-
| See README: Language selector section
93-
*/
94-
'language_selector' => [
95-
// Native name (Deutsch) vs English name (German)
96-
'use_native_names' => true,
97-
// Append country from locale_variant: Deutsch (Schweiz) for de_CH
98-
'show_regional_variants' => true,
99-
// Translated country in parentheses: Schweiz vs Switzerland
100-
'use_country_translations' => true,
101-
],
102-
10354
];

packages/localization/database/migrations/create_localizations_table.php.stub

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ return new class extends Migration {
4444
$table->string('routing_domain')->nullable()->unique();
4545

4646
$table->unsignedInteger('translation_status')->nullable();
47-
$table->json('language_settings')->nullable();
47+
48+
$table->boolean('use_native_names')->default(true);
49+
$table->boolean('show_regional_variants')->default(true);
50+
$table->boolean('use_country_translations')->default(true);
4851
$table->boolean('use_country_icon')->default(false);
4952

5053
$table->unique(['language_id', 'locale_variant'], 'unique_language_locale');

packages/localization/database/seeders/LocalizationSeeder.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function run(): void
3838
'routing_subdomain' => null,
3939
'routing_domain' => null,
4040
'translation_status' => 100,
41-
'language_settings' => json_encode(['locale' => 'en_US']),
41+
'use_native_names' => true,
42+
'show_regional_variants' => true,
43+
'use_country_translations' => true,
44+
'use_country_icon' => false,
4245
'created_at' => now(),
4346
'updated_at' => now(),
4447
],
@@ -57,7 +60,10 @@ public function run(): void
5760
'routing_subdomain' => null,
5861
'routing_domain' => null,
5962
'translation_status' => 80,
60-
'language_settings' => json_encode(['locale' => 'es_ES']),
63+
'use_native_names' => true,
64+
'show_regional_variants' => true,
65+
'use_country_translations' => true,
66+
'use_country_icon' => false,
6167
'created_at' => now(),
6268
'updated_at' => now(),
6369
],
@@ -76,7 +82,10 @@ public function run(): void
7682
'routing_subdomain' => null,
7783
'routing_domain' => null,
7884
'translation_status' => 90,
79-
'language_settings' => json_encode(['locale' => 'de_DE']),
85+
'use_native_names' => true,
86+
'show_regional_variants' => true,
87+
'use_country_translations' => true,
88+
'use_country_icon' => false,
8089
'created_at' => now(),
8190
'updated_at' => now(),
8291
],

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

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace Moox\Localization\Filament\Resources;
66

77
use Filament\Forms\Components\Select;
8-
use Filament\Forms\Components\Textarea;
98
use Filament\Forms\Components\TextInput;
109
use Filament\Forms\Components\Toggle;
1110
use Filament\Schemas\Components\Grid;
1211
use Filament\Schemas\Components\Section;
12+
use Filament\Schemas\Components\Utilities\Get;
1313
use Filament\Schemas\Components\Utilities\Set;
1414
use Filament\Schemas\Schema;
1515
use Filament\Tables\Columns\IconColumn;
@@ -142,7 +142,6 @@ public static function form(Schema $schema): Schema
142142
->label(__('localization::fields.is_activ_admin'))
143143
->default(true)
144144
->disabled(function ($get, $livewire) {
145-
// Disabled when this localization is set as default
146145
$record = $livewire->record;
147146
$isDefault = $get('is_default') ?? ($record !== null ? $record->is_default : false);
148147

@@ -151,47 +150,59 @@ public static function form(Schema $schema): Schema
151150
Toggle::make('is_active_frontend')
152151
->label(__('localization::fields.is_activ_frontend'))
153152
->default(false),
154-
Toggle::make('use_country_icon')
155-
->label(__('localization::fields.use_country_icon'))
156-
->default(false)
157-
->helperText(__('localization::fields.use_country_icon_help')),
158153
Toggle::make('is_default')
159154
->label(__('localization::fields.is_default'))
160155
->default(false)
161156
->disabled(function ($get, $livewire) {
162-
// Disabled when English is selected as default
163157
$record = $livewire->record;
164158
$localeVariant = $get('locale_variant') ?? ($record !== null ? $record->locale_variant : '');
165159
$isDefault = $get('is_default') ?? ($record !== null ? $record->is_default : false);
166160

167-
// If it is an English localization AND already set as default
168161
if (strpos($localeVariant, 'en_') === 0 && $isDefault) {
169162
return true;
170163
}
171164

172165
return false;
173166
})
174-
->afterStateUpdated(function ($state, $set, $get, $livewire) {
167+
->afterStateUpdated(function ($state, $set, $livewire) {
175168
if ($state) {
176-
// When activated as default, automatically set is_active_admin to true
177169
$set('is_active_admin', true);
178170

179171
$currentRecordId = $livewire->record?->id;
180-
$languageId = $get('language_id');
181172

182-
Localization::where('language_id', $languageId)
183-
->when($currentRecordId, function ($query) use ($currentRecordId) {
184-
$query->where('id', '!=', $currentRecordId);
185-
})
173+
Localization::query()
174+
->when($currentRecordId, fn ($query) => $query->where('id', '!=', $currentRecordId))
186175
->update(['is_default' => false]);
187176
} else {
188-
// When default is deactivated, always set en_US as default
189177
$enUsLocale = Localization::where('locale_variant', 'en_US')->first();
190-
if ($enUsLocale) {
178+
if ($enUsLocale && $enUsLocale->id !== $livewire->record?->id) {
191179
$enUsLocale->update(['is_default' => true, 'is_active_admin' => true]);
192180
}
193181
}
194182
}),
183+
Toggle::make('use_native_names')
184+
->label(__('localization::fields.use_native_names'))
185+
->default(true),
186+
Toggle::make('show_regional_variants')
187+
->label(__('localization::fields.show_regional_variants'))
188+
->default(true)
189+
->live()
190+
->afterStateUpdated(function (bool $state, Set $set): void {
191+
if (! $state) {
192+
$set('use_country_translations', false);
193+
}
194+
}),
195+
Toggle::make('use_country_translations')
196+
->label(__('localization::fields.use_country_translations'))
197+
->default(true)
198+
->disabled(fn (Get $get): bool => ! $get('show_regional_variants'))
199+
->helperText(fn (Get $get): ?string => $get('show_regional_variants')
200+
? null
201+
: __('localization::fields.country_names_requires_regional')),
202+
Toggle::make('use_country_icon')
203+
->label(__('localization::fields.use_country_icon'))
204+
->default(false)
205+
->helperText(__('localization::fields.use_country_icon_help')),
195206
TextInput::make('routing_path')
196207
->label(__('localization::fields.routing_path'))
197208
->nullable(),
@@ -205,9 +216,6 @@ public static function form(Schema $schema): Schema
205216
->label(__('localization::fields.translation_status'))
206217
->numeric()
207218
->nullable(),
208-
Textarea::make('language_settings')
209-
->label(__('localization::fields.language_settings'))
210-
->json(),
211219
])
212220
->columnSpan(2),
213221
Grid::make()
@@ -301,25 +309,24 @@ public static function table(Table $table): Table
301309
}
302310
}),
303311
// Display name & flag toggles (order: Native → Regional → Country names → Country flag)
304-
ToggleColumn::make('use_native_names_setting')
312+
ToggleColumn::make('use_native_names')
305313
->label(__('localization::fields.native'))
306-
->width(80)
307-
->getStateUsing(fn (Localization $record): bool => $record->getLanguageSetting('use_native_names'))
308-
->updateStateUsing(fn (Localization $record, bool $state): bool => static::persistLanguageSetting($record, 'use_native_names', $state)),
309-
ToggleColumn::make('show_regional_variants_setting')
314+
->width(80),
315+
ToggleColumn::make('show_regional_variants')
310316
->label(__('localization::fields.regional'))
311317
->width(80)
312-
->getStateUsing(fn (Localization $record): bool => $record->getLanguageSetting('show_regional_variants'))
313-
->updateStateUsing(fn (Localization $record, bool $state): bool => static::persistLanguageSetting($record, 'show_regional_variants', $state)),
314-
ToggleColumn::make('use_country_translations_setting')
318+
->afterStateUpdated(function (bool $state, Localization $record): void {
319+
if (! $state) {
320+
$record->update(['use_country_translations' => false]);
321+
}
322+
}),
323+
ToggleColumn::make('use_country_translations')
315324
->label(__('localization::fields.country_names'))
316325
->width(95)
317-
->tooltip(fn (Localization $record): ?string => $record->getLanguageSetting('show_regional_variants')
326+
->tooltip(fn (Localization $record): ?string => $record->show_regional_variants
318327
? null
319328
: __('localization::fields.country_names_requires_regional'))
320-
->disabled(fn (Localization $record): bool => ! $record->getLanguageSetting('show_regional_variants'))
321-
->getStateUsing(fn (Localization $record): bool => $record->getLanguageSetting('use_country_translations'))
322-
->updateStateUsing(fn (Localization $record, bool $state): bool => static::persistLanguageSetting($record, 'use_country_translations', $state)),
329+
->disabled(fn (Localization $record): bool => ! $record->show_regional_variants),
323330
ToggleColumn::make('use_country_icon')
324331
->label(__('localization::fields.country_flag'))
325332
->width(95),
@@ -352,17 +359,4 @@ public static function getPages(): array
352359
];
353360
}
354361

355-
protected static function persistLanguageSetting(Localization $record, string $key, bool $state): bool
356-
{
357-
$settings = $record->language_settings ?? [];
358-
$settings[$key] = $state;
359-
360-
if ($key === 'show_regional_variants' && ! $state) {
361-
$settings['use_country_translations'] = false;
362-
}
363-
364-
$record->update(['language_settings' => $settings]);
365-
366-
return $state;
367-
}
368362
}

packages/localization/src/Installers/DefaultEnglishLocalizationInstaller.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ public function install(array $assets): bool
126126
'routing_subdomain' => null,
127127
'routing_domain' => null,
128128
'translation_status' => 100,
129-
'language_settings' => [
130-
'locale' => 'en_US',
131-
],
129+
'use_native_names' => true,
130+
'show_regional_variants' => true,
131+
'use_country_translations' => true,
132+
'use_country_icon' => false,
132133
]);
133134

134135
note('✅ Default English localization created ('.$localization->locale.').');

0 commit comments

Comments
 (0)