1 <?php
2 3 4 5 6 7
8 class LanguageDropdownField extends GroupedDropdownField {
9
10 11 12 13 14 15 16 17
18 function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null) {
19 $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
20 $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
21
22 if('Common-English' == $list) $allLocalesWithTitle = i18n::get_common_languages();
23 else if('Common-Native' == $list) $allLocalesWithTitle = i18n::get_common_languages(true);
24 else if('Locale-English' == $list) $allLocalesWithTitle = i18n::get_common_locales();
25 else if('Locale-Native' == $list) $allLocalesWithTitle = i18n::get_common_locales(true);
26 else $allLocalesWithTitle = i18n::get_locale_list();
27
28 if(isset($allLocales[Translatable::default_locale()])) unset($allLocales[Translatable::default_locale()]);
29
30
31
32 $allowedLocales = Translatable::get_allowed_locales();
33 foreach($allLocalesWithTitle as $locale => $localeTitle) {
34 if(
35 ($allowedLocales && !in_array($locale, $allowedLocales))
36 || ($excludeLocales && in_array($locale, $excludeLocales))
37 || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle))
38 ) {
39 unset($allLocalesWithTitle[$locale]);
40 }
41
42 if($excludeLocales && in_array($locale, $excludeLocales)) {
43 unset($usedLocalesWithTitle[$locale]);
44 }
45 }
46
47 foreach($allLocalesWithTitle as $locale => $localeTitle) {
48 if($instance && !$instance->canTranslate(null, $locale)) {
49 unset($allLocalesWithTitle[$locale]);
50 }
51 }
52 foreach($usedLocalesWithTitle as $locale => $localeTitle) {
53 if($instance && !$instance->canTranslate(null, $locale)) {
54 unset($usedLocalesWithTitle[$locale]);
55 }
56 }
57
58
59 asort($allLocalesWithTitle);
60
61 if(count($usedLocalesWithTitle)) {
62 asort($usedLocalesWithTitle);
63 $source = array(
64 _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle,
65 _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle
66 );
67 } else {
68 $source = $allLocalesWithTitle;
69 }
70
71 parent::__construct($name, $title, $source);
72 }
73 }
74
75 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-