@@ -30,20 +30,25 @@ public function configureOptions(OptionsResolver $resolver)
30
30
'choice_loader ' => function (Options $ options ) {
31
31
$ choiceTranslationLocale = $ options ['choice_translation_locale ' ];
32
32
$ useAlpha3Codes = $ options ['alpha3 ' ];
33
- $ choiceSelfTranslation = $ options ['choice_self_translation ' ];
33
+ $ useChoiceSelfTranslation = true === $ options ['choice_self_translation ' ];
34
+ $ whitelist = $ options ['whitelist ' ];
34
35
35
- return new IntlCallbackChoiceLoader (function () use ($ choiceTranslationLocale , $ useAlpha3Codes , $ choiceSelfTranslation ) {
36
- if (true === $ choiceSelfTranslation ) {
37
- foreach (Languages::getLanguageCodes () as $ alpha2Code ) {
38
- try {
39
- $ languageCode = $ useAlpha3Codes ? Languages::getAlpha3Code ($ alpha2Code ) : $ alpha2Code ;
40
- $ languagesList [$ languageCode ] = Languages::getName ($ alpha2Code , $ alpha2Code );
41
- } catch (MissingResourceException $ e ) {
42
- // ignore errors like "Couldn't read the indices for the locale 'meta'"
36
+ return new IntlCallbackChoiceLoader (function () use ($ choiceTranslationLocale , $ useAlpha3Codes , $ useChoiceSelfTranslation , $ whitelist ) {
37
+ if ($ whitelist ) {
38
+ $ whitelist = array_flip ($ whitelist );
39
+ }
40
+
41
+ foreach (Languages::getLanguageCodes () as $ alpha2Code ) {
42
+ try {
43
+ $ languageCode = $ useAlpha3Codes ? Languages::getAlpha3Code ($ alpha2Code ) : $ alpha2Code ;
44
+ if ($ whitelist && !isset ($ whitelist [$ languageCode ])) {
45
+ continue ;
43
46
}
47
+
48
+ $ languagesList [$ languageCode ] = Languages::getName ($ alpha2Code , $ useChoiceSelfTranslation ? $ alpha2Code : $ choiceTranslationLocale );
49
+ } catch (MissingResourceException $ e ) {
50
+ // ignore errors like "Couldn't read the indices for the locale 'meta'"
44
51
}
45
- } else {
46
- $ languagesList = $ useAlpha3Codes ? Languages::getAlpha3Names ($ choiceTranslationLocale ) : Languages::getNames ($ choiceTranslationLocale );
47
52
}
48
53
49
54
return array_flip ($ languagesList );
@@ -53,11 +58,13 @@ public function configureOptions(OptionsResolver $resolver)
53
58
'choice_translation_locale ' => null ,
54
59
'alpha3 ' => false ,
55
60
'choice_self_translation ' => false ,
61
+ 'whitelist ' => null ,
56
62
]);
57
63
58
64
$ resolver ->setAllowedTypes ('choice_self_translation ' , ['bool ' ]);
59
65
$ resolver ->setAllowedTypes ('choice_translation_locale ' , ['null ' , 'string ' ]);
60
66
$ resolver ->setAllowedTypes ('alpha3 ' , 'bool ' );
67
+ $ resolver ->setAllowedTypes ('whitelist ' , ['null ' , 'array ' ]);
61
68
62
69
$ resolver ->setNormalizer ('choice_self_translation ' , function (Options $ options , $ value ) {
63
70
if (true === $ value && $ options ['choice_translation_locale ' ]) {
0 commit comments