-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageSwitcherPlugin.php
184 lines (161 loc) · 4.49 KB
/
LanguageSwitcherPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Class LanguageSelectorPlugin
*/
class LanguageSwitcherPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = [
'initialize',
'config_form',
'config',
'public_head',
'language_selector'
];
protected $_filters = ['locale'];
/**
* Returns current locale and sets locale to current session
*
* @return bool|string
*/
private function getCurrentLocale()
{
$session_locale = isset($_SESSION['current_locale']) ? $_SESSION['current_locale'] : false;
$options_locale = get_option('current_locale');
if (!$options_locale) {
$options_locale = false;
}
$locale = $this->getDefaultLocale();
if ($session_locale) {
$locale = $session_locale;
}
elseif ($options_locale) {
$locale = $options_locale;
}
$_SESSION['current_locale'] = $locale;
return $locale;
}
/**
* Loads language selector's assets
*/
public function hookPublicHead()
{
queue_css_file('language_selector');
}
/**
* Custom hook for displaying language selector widget
*/
public function hookLanguageSelector()
{
$languages = $this->getLanguagesArray();
$lang_vals = array_map( function ($item) { return $item[0]; }, $languages);
$lang_labels = array_map( function ($item) { return $item[1]; }, $languages);
$languages = array_combine($lang_vals, $lang_labels);
$current_language = $this->getCurrentLocale();
include('public_lang_selector.php');
}
/**
* Omeka's filter "locale"
*
* @param $locale
* @return bool|string
*/
public function filterLocale($locale)
{
if (isset($_GET['language'])) {
$locale = Zend_Locale::findLocale($_GET['language']);
$_SESSION['current_locale'] = $locale;
return $locale;
}
else {
return $this->getCurrentLocale();
}
}
/**
* Plugin init
*/
public function hookInitialize()
{
$this->getCurrentLocale();
}
public function hookConfig($args)
{
set_option('languages_list', $args['post']['languages_list_textarea']);
}
/**
* Returns parsed languages list
* @return array|string
*/
public function getLanguagesArray()
{
$languages = get_option('languages_list');
$languages = explode("\n", $languages);
$languages = array_map(
function ($item) {
return explode(';', $item);
},
$languages
);
return $languages;
}
/**
* Config form render
*/
public function hookConfigForm()
{
$form = new Omeka_Form(
[
'type' => 'languages_list'
]
);
$languages = get_option('languages_list');
if (!$languages) {
$languages = "en_GB;English";
}
$form->addElement(
'textarea',
'languages_list_textarea',
[
'id' => 'simple-pages-title',
'cols' => 50,
'rows' => 25,
'value' => $languages,
'label' => __('Languages in select'),
'description' => __(
'List of languages available from public in select. Each item in one line. Value and label separated by ";"'
),
'required' => true
]
);
$form->removeDecorator('form');
echo $form;
echo "<h3>" . __('List of available locales:') . "</h3>";
$locale_groups = [];
$current_group = null;
foreach (Zend_Locale::getLocaleList() as $locale => $v) {
if (strpos($locale, '_') === false) {
$current_group = $locale;
} else {
$locale_groups[$current_group][] = $locale;
}
}
foreach ($locale_groups as $group => $locales) {
echo "";
foreach ($locales as $i => $l) {
echo "<code>" . $l . "</code>";
if ($i < count($locales) - 1) {
echo ', ';
}
}
echo "<hr>";
}
}
/**
* Returns default locale (first element in the list)
*
* @return mixed
*/
function getDefaultLocale()
{
return $this->getLanguagesArray()[0][0];
}
}