Skip to content

Commit ae78753

Browse files
committed
Added an settings option to control what languages to show in the dropdown menu
1 parent 6e4ae15 commit ae78753

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Form\Type;
25+
26+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
27+
use Symfony\Component\Form\AbstractType;
28+
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
29+
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
30+
use Symfony\Component\Intl\Languages;
31+
use Symfony\Component\OptionsResolver\OptionsResolver;
32+
33+
class LanguageMenuEntriesType extends AbstractType
34+
{
35+
public function __construct(#[Autowire(param: 'partdb.locale_menu')] private readonly array $preferred_languages)
36+
{
37+
38+
}
39+
40+
public function getParent(): string
41+
{
42+
return LanguageType::class;
43+
}
44+
45+
public function configureOptions(OptionsResolver $resolver): void
46+
{
47+
$choices = [];
48+
foreach ($this->preferred_languages as $lang_code) {
49+
$choices[Languages::getName($lang_code)] = $lang_code;
50+
}
51+
52+
$resolver->setDefaults([
53+
'choice_loader' => null,
54+
'choices' => $choices,
55+
]);
56+
}
57+
}

src/Settings/SystemSettings/LocalizationSettings.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323

2424
namespace App\Settings\SystemSettings;
2525

26+
use App\Form\Type\LanguageMenuEntriesType;
2627
use App\Form\Type\LocaleSelectType;
2728
use App\Settings\SettingsIcon;
2829
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
30+
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
31+
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
2932
use Jbtronics\SettingsBundle\Settings\Settings;
3033
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
3134
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
@@ -60,4 +63,14 @@ class LocalizationSettings
6063
envVar: "string:BASE_CURRENCY", envVarMode: EnvVarMode::OVERWRITE
6164
)]
6265
public string $baseCurrency = 'EUR';
63-
}
66+
67+
#[SettingsParameter(type: ArrayType::class,
68+
label: new TM("settings.system.localization.language_menu_entries"),
69+
description: new TM("settings.system.localization.language_menu_entries.description"),
70+
options: ['type' => StringType::class],
71+
formType: LanguageMenuEntriesType::class,
72+
formOptions: ['multiple' => true, 'required' => false, 'ordered' => true],
73+
)]
74+
#[Assert\All([new Assert\Locale()])]
75+
public array $languageMenuEntries = [];
76+
}

templates/_turbo_control.html.twig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222
<div class="d-none" data-title="{{ current_page_title|trim|raw }}" {{ stimulus_controller('turbo/title') }}></div>
2323

2424
<div class="d-none" {{ stimulus_controller('turbo/locale_menu') }}>
25-
{% for locale in locale_menu %}
25+
{% set locales = settings_instance('localization').languageMenuEntries %}
26+
{% if locales is empty %}
27+
{% set locales = locale_menu %}
28+
{% endif %}
29+
30+
{% for locale in locales %}
2631
<a class="dropdown-item" data-turbo="false" data-turbo-frame="_top" href="{{ path(app.request.attributes.get('_route'),
2732
app.request.query.all|merge(app.request.attributes.get('_route_params'))|merge({'_locale': locale})) }}">
2833
{{ locale|language_name }} ({{ locale|upper }})</a>
2934
{% endfor %}
30-
</div>
35+
</div>

translations/messages.en.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14214,5 +14214,17 @@ Please note, that you can not impersonate a disabled user. If you try you will g
1421414214
<target>Misc</target>
1421514215
</segment>
1421614216
</unit>
14217+
<unit id="mNLyYXa" name="settings.system.localization.language_menu_entries">
14218+
<segment>
14219+
<source>settings.system.localization.language_menu_entries</source>
14220+
<target>Language menu entries</target>
14221+
</segment>
14222+
</unit>
14223+
<unit id="Ej2znKK" name="settings.system.localization.language_menu_entries.description">
14224+
<segment>
14225+
<source>settings.system.localization.language_menu_entries.description</source>
14226+
<target><![CDATA[The languages to show in the language drop-down menu. Order can be changed via drag & drop. Leave empty to show all available languages.]]></target>
14227+
</segment>
14228+
</unit>
1421714229
</file>
1421814230
</xliff>

0 commit comments

Comments
 (0)