Version Description
- Enhancement: Improved compatibility with multilingual plugins like Polylang.
- Fixed: Fixes an issue with keyboard navigation.
- Fixed: Make sure the correct languages are retrieved when updating the settings.
Download this release
Release Info
Developer | swissspidy |
Plugin | Preferred Languages |
Version | 1.5.0 |
Comparing to | |
See all releases |
Code changes from version 1.4.0 to 1.5.0
- CHANGELOG.md +6 -0
- css/preferred-languages-rtl.css +1 -1
- css/preferred-languages.css +1 -1
- inc/functions.php +20 -7
- js/preferred-languages.js +304 -326
- js/preferred-languages.min.js +1 -1
- preferred-languages.php +1 -1
- readme.txt +11 -1
CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1 |
# Changelog
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
## 1.4.0
|
4 |
|
5 |
* New: Keyboard navigation improvements.
|
1 |
# Changelog
|
2 |
|
3 |
+
## 1.5.0
|
4 |
+
|
5 |
+
* Enhancement: Improved compatibility with multilingual plugins like Polylang.
|
6 |
+
* Fixed: Fixes an issue with keyboard navigation.
|
7 |
+
* Fixed: Make sure the correct languages are retrieved when updating the settings.
|
8 |
+
|
9 |
## 1.4.0
|
10 |
|
11 |
* New: Keyboard navigation improvements.
|
css/preferred-languages-rtl.css
CHANGED
@@ -1 +1 @@
|
|
1 |
-
.active-locales{margin:1em 0}.active-locales-list,.inactive-locales-list{width:25em;margin:0 0 1em 1em;clear:both;float:right}.active-locales-controls{clear:both}.active-locales-controls .button,.active-locales-controls ul{margin:0}@media screen and (min-width:510px){.active-locales-controls{clear:none}}.active-locales-list{height:98px;overflow-y:scroll;margin-top:0;background:#fff;box-shadow:inset 0 1px 2px rgba(0,0,0,.07);border:1px solid #ddd;color:#32373c;box-sizing:border-box}.active-locales-list.empty-list{background:hsla(0,0%,100%,.5);border-color:hsla(0,0%,87%,.75);box-shadow:inset 0 1px 2px rgba(0,0,0,.04);display
|
1 |
+
.active-locales{margin:1em 0}.active-locales-list,.inactive-locales-list{width:25em;margin:0 0 1em 1em;clear:both;float:right}.active-locales-controls{clear:both}.active-locales-controls .button,.active-locales-controls ul{margin:0}@media screen and (min-width:510px){.active-locales-controls{clear:none}}.active-locales-list{height:98px;overflow-y:scroll;margin-top:0;background:#fff;box-shadow:inset 0 1px 2px rgba(0,0,0,.07);border:1px solid #ddd;color:#32373c;box-sizing:border-box}.active-locales-list.empty-list{background:hsla(0,0%,100%,.5);border-color:hsla(0,0%,87.1%,.75);box-shadow:inset 0 1px 2px rgba(0,0,0,.04);display:flex;align-items:center}.active-locales-list li{box-sizing:border-box;width:100%;height:28px;line-height:28px;margin:0;padding:0 8px 0 0;cursor:pointer}.active-locales-list li:hover{background:rgba(0,0,0,.07)}.active-locales-list li[aria-selected=true]{background:rgba(0,0,0,.15)}#active-locales-empty-message{display:flex;justify-content:center;flex-direction:column;position:relative;text-align:center;width:25em;height:98px;margin-bottom:-98px;box-sizing:border-box;color:rgba(51,51,51,.5)}#active-locales-empty-message.hidden{display:none}.inactive-locales{clear:both}.inactive-locales select{width:100%}.preferred-languages .notice{display:inline-block}.no-js #preferred-languages-label,.no-js .preferred-languages,.no-js .user-preferred-languages-wrap{display:none}@keyframes tooltip-appear{0%{opacity:0}to{opacity:1}}.tooltipped{position:relative}.tooltipped:active:after,.tooltipped:active:before,.tooltipped:focus:after,.tooltipped:focus:before,.tooltipped:hover:after,.tooltipped:hover:before{opacity:1;display:inline-block;text-decoration:none;animation-name:tooltip-appear;animation-duration:.1s;animation-fill-mode:backwards;animation-timing-function:ease-in;animation-delay:.4s}.tooltipped:before{z-index:1000001;width:0;height:0;color:#191e23;content:"";top:-7px;left:50%;bottom:auto;margin-left:-6px;border:6px solid transparent;border-top-color:#191e23}.tooltipped:after,.tooltipped:before{position:absolute;display:none;pointer-events:none;opacity:0}.tooltipped:after{z-index:1000000;padding:.5em .75em;font:normal normal 11px/1.5 -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;-webkit-font-smoothing:subpixel-antialiased;color:#fff;text-align:center;text-decoration:none;text-shadow:none;text-transform:none;letter-spacing:normal;word-wrap:break-word;white-space:pre;content:attr(data-tooltip);background:#191e23;border-radius:3px;left:auto;right:50%;margin-right:-16px;bottom:100%;margin-bottom:6px}
|
css/preferred-languages.css
CHANGED
@@ -1 +1 @@
|
|
1 |
-
.active-locales{margin:1em 0}.active-locales-list,.inactive-locales-list{width:25em;margin:0 1em 1em 0;clear:both;float:left}.active-locales-controls{clear:both}.active-locales-controls .button,.active-locales-controls ul{margin:0}@media screen and (min-width:510px){.active-locales-controls{clear:none}}.active-locales-list{height:98px;overflow-y:scroll;margin-top:0;background:#fff;box-shadow:inset 0 1px 2px rgba(0,0,0,.07);border:1px solid #ddd;color:#32373c;box-sizing:border-box}.active-locales-list.empty-list{background:hsla(0,0%,100%,.5);border-color:hsla(0,0%,87%,.75);box-shadow:inset 0 1px 2px rgba(0,0,0,.04);display
|
1 |
+
.active-locales{margin:1em 0}.active-locales-list,.inactive-locales-list{width:25em;margin:0 1em 1em 0;clear:both;float:left}.active-locales-controls{clear:both}.active-locales-controls .button,.active-locales-controls ul{margin:0}@media screen and (min-width:510px){.active-locales-controls{clear:none}}.active-locales-list{height:98px;overflow-y:scroll;margin-top:0;background:#fff;box-shadow:inset 0 1px 2px rgba(0,0,0,.07);border:1px solid #ddd;color:#32373c;box-sizing:border-box}.active-locales-list.empty-list{background:hsla(0,0%,100%,.5);border-color:hsla(0,0%,87.1%,.75);box-shadow:inset 0 1px 2px rgba(0,0,0,.04);display:flex;align-items:center}.active-locales-list li{box-sizing:border-box;width:100%;height:28px;line-height:28px;margin:0;padding:0 0 0 8px;cursor:pointer}.active-locales-list li:hover{background:rgba(0,0,0,.07)}.active-locales-list li[aria-selected=true]{background:rgba(0,0,0,.15)}#active-locales-empty-message{display:flex;justify-content:center;flex-direction:column;position:relative;text-align:center;width:25em;height:98px;margin-bottom:-98px;box-sizing:border-box;color:rgba(51,51,51,.5)}#active-locales-empty-message.hidden{display:none}.inactive-locales{clear:both}.inactive-locales select{width:100%}.preferred-languages .notice{display:inline-block}.no-js #preferred-languages-label,.no-js .preferred-languages,.no-js .user-preferred-languages-wrap{display:none}@keyframes tooltip-appear{0%{opacity:0}to{opacity:1}}.tooltipped{position:relative}.tooltipped:active:after,.tooltipped:active:before,.tooltipped:focus:after,.tooltipped:focus:before,.tooltipped:hover:after,.tooltipped:hover:before{opacity:1;display:inline-block;text-decoration:none;animation-name:tooltip-appear;animation-duration:.1s;animation-fill-mode:backwards;animation-timing-function:ease-in;animation-delay:.4s}.tooltipped:before{z-index:1000001;width:0;height:0;color:#191e23;content:"";top:-7px;right:50%;bottom:auto;margin-right:-6px;border:6px solid transparent;border-top-color:#191e23}.tooltipped:after,.tooltipped:before{position:absolute;display:none;pointer-events:none;opacity:0}.tooltipped:after{z-index:1000000;padding:.5em .75em;font:normal normal 11px/1.5 -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;-webkit-font-smoothing:subpixel-antialiased;color:#fff;text-align:center;text-decoration:none;text-shadow:none;text-transform:none;letter-spacing:normal;word-wrap:break-word;white-space:pre;content:attr(data-tooltip);background:#191e23;border-radius:3px;right:auto;left:50%;margin-left:-16px;bottom:100%;margin-bottom:6px}
|
inc/functions.php
CHANGED
@@ -31,7 +31,9 @@ function preferred_languages_register_setting() {
|
|
31 |
*/
|
32 |
function preferred_languages_register_meta() {
|
33 |
register_meta(
|
34 |
-
'user',
|
|
|
|
|
35 |
'type' => 'string',
|
36 |
'description' => 'List of preferred languages',
|
37 |
'single' => true,
|
@@ -173,7 +175,7 @@ function preferred_languages_update_user_meta( $meta_id, $object_id, $meta_key,
|
|
173 |
add_filter( 'update_user_meta', 'preferred_languages_update_user_meta', 10, 4 );
|
174 |
|
175 |
// Reload translations after save.
|
176 |
-
$preferred_languages_list =
|
177 |
load_default_textdomain( reset( $preferred_languages_list ) );
|
178 |
}
|
179 |
|
@@ -218,7 +220,7 @@ function preferred_languages_update_option( $old_value, $value ) {
|
|
218 |
add_filter( 'update_option_preferred_languages', 'preferred_languages_update_option', 10, 2 );
|
219 |
|
220 |
// Reload translations after save.
|
221 |
-
$preferred_languages_list =
|
222 |
load_default_textdomain( reset( $preferred_languages_list ) );
|
223 |
}
|
224 |
|
@@ -332,6 +334,11 @@ function preferred_languages_load_textdomain_mofile( $mofile ) {
|
|
332 |
|
333 |
$current_locale = get_locale();
|
334 |
|
|
|
|
|
|
|
|
|
|
|
335 |
foreach ( $preferred_locales as $locale ) {
|
336 |
$preferred_mofile = str_replace( $current_locale, $locale, $mofile );
|
337 |
|
@@ -359,7 +366,7 @@ function preferred_languages_register_scripts() {
|
|
359 |
'jquery-ui-sortable',
|
360 |
'wp-a11y',
|
361 |
),
|
362 |
-
'
|
363 |
true
|
364 |
);
|
365 |
|
@@ -459,7 +466,8 @@ function preferred_languages_display_form( $args = array() ) {
|
|
459 |
wp_enqueue_script( 'preferred-languages' );
|
460 |
|
461 |
$args = wp_parse_args(
|
462 |
-
$args,
|
|
|
463 |
'selected' => array(),
|
464 |
'show_available_translations' => current_user_can( 'install_languages' ),
|
465 |
'show_option_site_default' => false,
|
@@ -654,7 +662,7 @@ function preferred_languages_init_registry() {
|
|
654 |
}
|
655 |
|
656 |
/**
|
657 |
-
* Filters gettext
|
658 |
*
|
659 |
* @since 1.1.0
|
660 |
*
|
@@ -675,7 +683,7 @@ function preferred_languages_filter_gettext( $translation, $text, $domain ) {
|
|
675 |
/* @var Preferred_Languages_Textdomain_Registry $preferred_languages_textdomain_registry */
|
676 |
global $preferred_languages_textdomain_registry;
|
677 |
|
678 |
-
if ( ! $preferred_languages_textdomain_registry instanceof
|
679 |
preferred_languages_init_registry();
|
680 |
}
|
681 |
|
@@ -693,6 +701,11 @@ function preferred_languages_filter_gettext( $translation, $text, $domain ) {
|
|
693 |
|
694 |
$preferred_locales = preferred_languages_get_list();
|
695 |
|
|
|
|
|
|
|
|
|
|
|
696 |
foreach ( $preferred_locales as $locale ) {
|
697 |
$mofile = "{$path}/{$domain}-{$locale}.mo";
|
698 |
|
31 |
*/
|
32 |
function preferred_languages_register_meta() {
|
33 |
register_meta(
|
34 |
+
'user',
|
35 |
+
'preferred_languages',
|
36 |
+
array(
|
37 |
'type' => 'string',
|
38 |
'description' => 'List of preferred languages',
|
39 |
'single' => true,
|
175 |
add_filter( 'update_user_meta', 'preferred_languages_update_user_meta', 10, 4 );
|
176 |
|
177 |
// Reload translations after save.
|
178 |
+
$preferred_languages_list = preferred_languages_get_user_list( $object_id );
|
179 |
load_default_textdomain( reset( $preferred_languages_list ) );
|
180 |
}
|
181 |
|
220 |
add_filter( 'update_option_preferred_languages', 'preferred_languages_update_option', 10, 2 );
|
221 |
|
222 |
// Reload translations after save.
|
223 |
+
$preferred_languages_list = preferred_languages_get_site_list();
|
224 |
load_default_textdomain( reset( $preferred_languages_list ) );
|
225 |
}
|
226 |
|
334 |
|
335 |
$current_locale = get_locale();
|
336 |
|
337 |
+
// Locale has been filtered by something else.
|
338 |
+
if ( ! in_array( $current_locale, $preferred_locales, true ) ) {
|
339 |
+
return $mofile;
|
340 |
+
}
|
341 |
+
|
342 |
foreach ( $preferred_locales as $locale ) {
|
343 |
$preferred_mofile = str_replace( $current_locale, $locale, $mofile );
|
344 |
|
366 |
'jquery-ui-sortable',
|
367 |
'wp-a11y',
|
368 |
),
|
369 |
+
'20181010',
|
370 |
true
|
371 |
);
|
372 |
|
466 |
wp_enqueue_script( 'preferred-languages' );
|
467 |
|
468 |
$args = wp_parse_args(
|
469 |
+
$args,
|
470 |
+
array(
|
471 |
'selected' => array(),
|
472 |
'show_available_translations' => current_user_can( 'install_languages' ),
|
473 |
'show_option_site_default' => false,
|
662 |
}
|
663 |
|
664 |
/**
|
665 |
+
* Filters gettext calls to work around limitations in just-in-time loading of translations.
|
666 |
*
|
667 |
* @since 1.1.0
|
668 |
*
|
683 |
/* @var Preferred_Languages_Textdomain_Registry $preferred_languages_textdomain_registry */
|
684 |
global $preferred_languages_textdomain_registry;
|
685 |
|
686 |
+
if ( ! $preferred_languages_textdomain_registry instanceof Preferred_Languages_Textdomain_Registry ) {
|
687 |
preferred_languages_init_registry();
|
688 |
}
|
689 |
|
701 |
|
702 |
$preferred_locales = preferred_languages_get_list();
|
703 |
|
704 |
+
// Locale has been filtered by something else.
|
705 |
+
if ( ! in_array( get_locale(), $preferred_locales, true ) ) {
|
706 |
+
return $translation;
|
707 |
+
}
|
708 |
+
|
709 |
foreach ( $preferred_locales as $locale ) {
|
710 |
$mofile = "{$path}/{$domain}-{$locale}.mo";
|
711 |
|
js/preferred-languages.js
CHANGED
@@ -1,329 +1,307 @@
|
|
1 |
-
|
2 |
|
3 |
(function (wp, settings, $) {
|
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 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
}
|
308 |
-
|
309 |
-
e.preventDefault();
|
310 |
-
}
|
311 |
-
});
|
312 |
-
|
313 |
-
// Add new locale to list.
|
314 |
-
$inactiveLocalesControls.find('.locales-add').on('click', function () {
|
315 |
-
makeLocaleActive($inactiveLocales.find('option:selected'));
|
316 |
-
});
|
317 |
-
|
318 |
-
// Select a locale.
|
319 |
-
$activeLocales.on('click', '.active-locale', function (e) {
|
320 |
-
toggleLocale($(e.currentTarget));
|
321 |
-
});
|
322 |
-
|
323 |
-
$activeLocalesControls.find('.locales-move-up').on('click', moveLocaleUp);
|
324 |
-
|
325 |
-
$activeLocalesControls.find('.locales-move-down').on('click', moveLocaleDown);
|
326 |
-
|
327 |
-
// Remove locale from list.
|
328 |
-
$activeLocalesControls.find('.locales-remove').on('click', makeLocaleInactive);
|
329 |
})(wp, preferredLanguages, jQuery);
|
1 |
+
"use strict";
|
2 |
|
3 |
(function (wp, settings, $) {
|
4 |
+
var $document = $(document);
|
5 |
+
var $activeLocales = $('.active-locales-list');
|
6 |
+
var $activeLocalesControls = $('.active-locales-controls');
|
7 |
+
var $emptyMessage = $('#active-locales-empty-message');
|
8 |
+
var $inactiveLocalesWrap = $('.inactive-locales-list');
|
9 |
+
var $inactiveLocales = $inactiveLocalesWrap.find('select');
|
10 |
+
var $inactiveLocalesControls = $('.inactive-locales-controls');
|
11 |
+
var $inputField = $('input[name="preferred_languages"]');
|
12 |
+
var $selectedLocale = $activeLocales.find('li[aria-selected="true"]');
|
13 |
+
var KEY_UP = 38;
|
14 |
+
var KEY_DOWN = 40;
|
15 |
+
var KEY_BACKSPACE = 8;
|
16 |
+
var KEY_A = 65;
|
17 |
+
/**
|
18 |
+
* Changes the move button states.
|
19 |
+
*
|
20 |
+
* @since 1.0.0
|
21 |
+
*
|
22 |
+
* @param {jQuery} activeLocale Active locale element.
|
23 |
+
*/
|
24 |
+
|
25 |
+
function changeButtonState(activeLocale) {
|
26 |
+
var activeLocalesList = $activeLocales.find('.active-locale');
|
27 |
+
$activeLocalesControls.find('.locales-move-up').attr('disabled', $activeLocales.hasClass('empty-list') || activeLocalesList.first().is(activeLocale));
|
28 |
+
$activeLocalesControls.find('.locales-move-down').attr('disabled', $activeLocales.hasClass('empty-list') || activeLocalesList.last().is(activeLocale));
|
29 |
+
$activeLocalesControls.find('.locales-remove').attr('disabled', $activeLocales.hasClass('empty-list'));
|
30 |
+
$inactiveLocalesControls.find('.locales-add').attr('disabled', 'disabled' === $inactiveLocales.attr('disabled'));
|
31 |
+
}
|
32 |
+
/**
|
33 |
+
* Toggles a locale.
|
34 |
+
*
|
35 |
+
* @since 1.0.0
|
36 |
+
*
|
37 |
+
* @param {jQuery} $locale Locale element.
|
38 |
+
*/
|
39 |
+
|
40 |
+
|
41 |
+
function toggleLocale($locale) {
|
42 |
+
var selected = $locale.attr('aria-selected');
|
43 |
+
var newState = 'true' !== selected; // It's already the current locale, so nothing to do here.
|
44 |
+
|
45 |
+
if ('true' === selected) {
|
46 |
+
return;
|
47 |
+
}
|
48 |
+
|
49 |
+
$selectedLocale.attr('aria-selected', false);
|
50 |
+
$locale.attr('aria-selected', newState);
|
51 |
+
|
52 |
+
if (true === newState) {
|
53 |
+
$selectedLocale = $locale;
|
54 |
+
$activeLocales.attr('aria-activedescendant', $selectedLocale.attr('id'));
|
55 |
+
}
|
56 |
+
|
57 |
+
changeButtonState($locale);
|
58 |
+
}
|
59 |
+
/**
|
60 |
+
* Updates the preferred languages input field after a change.
|
61 |
+
*
|
62 |
+
* @since 1.0.0
|
63 |
+
*/
|
64 |
+
|
65 |
+
|
66 |
+
function updateHiddenInput() {
|
67 |
+
var locales = [];
|
68 |
+
$activeLocales.children('.active-locale').each(function (index, el) {
|
69 |
+
locales.push($(el).attr('id'));
|
70 |
+
});
|
71 |
+
$inputField.val(locales.join(','));
|
72 |
+
}
|
73 |
+
/**
|
74 |
+
* Moves a locale up in the list.
|
75 |
+
*
|
76 |
+
* @since 1.0.0
|
77 |
+
*/
|
78 |
+
|
79 |
+
|
80 |
+
function moveLocaleUp() {
|
81 |
+
// 1. Change position if possible.
|
82 |
+
$selectedLocale.insertBefore($selectedLocale.prev()); // 2. Update hidden input field.
|
83 |
+
|
84 |
+
updateHiddenInput(); // 3. Update buttons.
|
85 |
+
|
86 |
+
changeButtonState($selectedLocale); // 4. Announce to assistive technologies.
|
87 |
+
|
88 |
+
wp.a11y.speak(settings.l10n.movedUp);
|
89 |
+
}
|
90 |
+
/**
|
91 |
+
* Moves a locale down in the list.
|
92 |
+
*
|
93 |
+
* @since 1.0.0
|
94 |
+
*/
|
95 |
+
|
96 |
+
|
97 |
+
function moveLocaleDown() {
|
98 |
+
// 1. Change position if possible.
|
99 |
+
$selectedLocale.insertAfter($selectedLocale.next()); // 2. Update hidden input field.
|
100 |
+
|
101 |
+
updateHiddenInput(); // 3. Update buttons.
|
102 |
+
|
103 |
+
changeButtonState($selectedLocale); // 4. Announce to assistive technologies.
|
104 |
+
|
105 |
+
wp.a11y.speak(settings.l10n.movedDown);
|
106 |
+
}
|
107 |
+
/**
|
108 |
+
* Displays a message in case the list of active locales is empty.
|
109 |
+
*
|
110 |
+
* @since 1.1.1
|
111 |
+
*/
|
112 |
+
|
113 |
+
|
114 |
+
function toggleEmptyListMessage() {
|
115 |
+
if ($activeLocales.hasClass('empty-list')) {
|
116 |
+
$activeLocales.removeClass('empty-list');
|
117 |
+
$emptyMessage.addClass('hidden');
|
118 |
+
} else {
|
119 |
+
$activeLocales.addClass('empty-list');
|
120 |
+
$activeLocales.attr('aria-activedescendant', '');
|
121 |
+
$emptyMessage.removeClass('hidden');
|
122 |
+
wp.a11y.speak($emptyMessage.data('a11y-message'));
|
123 |
+
}
|
124 |
+
}
|
125 |
+
/**
|
126 |
+
* Removes an active locale from the list.
|
127 |
+
*
|
128 |
+
* @since 1.0.0
|
129 |
+
*/
|
130 |
+
|
131 |
+
|
132 |
+
function makeLocaleInactive() {
|
133 |
+
var locale = $selectedLocale.attr('id');
|
134 |
+
var $successor;
|
135 |
+
$successor = $selectedLocale.prevAll(':first');
|
136 |
+
|
137 |
+
if (0 === $successor.length) {
|
138 |
+
$successor = $selectedLocale.nextAll(':first');
|
139 |
+
} // 1. Remove selected locale.
|
140 |
+
|
141 |
+
|
142 |
+
$selectedLocale.remove(); // 2. Make another locale the current one.
|
143 |
+
|
144 |
+
if ($successor.length) {
|
145 |
+
toggleLocale($successor);
|
146 |
+
} else {
|
147 |
+
toggleEmptyListMessage();
|
148 |
+
} // 3. Make visible in dropdown again.
|
149 |
+
|
150 |
+
|
151 |
+
$inactiveLocales.find("option[value=\"".concat(locale, "\"]")).removeClass('hidden');
|
152 |
+
$inactiveLocales.attr('disabled', false); // 4. Update hidden input field.
|
153 |
+
|
154 |
+
updateHiddenInput(); // 5. Update buttons.
|
155 |
+
|
156 |
+
changeButtonState($selectedLocale); // 6. Announce to assistive technologies.
|
157 |
+
|
158 |
+
wp.a11y.speak(settings.l10n.localeRemoved);
|
159 |
+
}
|
160 |
+
/**
|
161 |
+
* Makes an inactive locale active.
|
162 |
+
*
|
163 |
+
* @since 1.0.0
|
164 |
+
*
|
165 |
+
* @param {jQuery} option The locale element.
|
166 |
+
*/
|
167 |
+
|
168 |
+
|
169 |
+
function makeLocaleActive(option) {
|
170 |
+
var locale = option.val() || 'en_US';
|
171 |
+
var $newLocale = $('<li/>', {
|
172 |
+
text: option.text(),
|
173 |
+
'role': 'option',
|
174 |
+
'aria-selected': false,
|
175 |
+
'id': locale,
|
176 |
+
'class': 'active-locale'
|
177 |
+
});
|
178 |
+
var $successor; // 1. Hide from dropdown.
|
179 |
+
|
180 |
+
option.removeAttr('selected').addClass('hidden');
|
181 |
+
$successor = option.prevAll(':not(.hidden):first');
|
182 |
+
|
183 |
+
if (!$successor.length) {
|
184 |
+
$successor = option.nextAll(':not(.hidden):first');
|
185 |
+
} // Empty optgroup, just select the first option we can find.
|
186 |
+
|
187 |
+
|
188 |
+
if (!$successor.length) {
|
189 |
+
$successor = $inactiveLocales.find('option:not(.hidden):first');
|
190 |
+
}
|
191 |
+
|
192 |
+
if (!$successor.length) {
|
193 |
+
$inactiveLocales.attr('disabled', true);
|
194 |
+
} // 2. Change selected value in dropdown.
|
195 |
+
|
196 |
+
|
197 |
+
$successor.attr('selected', true);
|
198 |
+
$inactiveLocalesControls.val($successor.val()); // It's already in the list of active locales, stop here.
|
199 |
+
|
200 |
+
if ($activeLocales.find("#".concat(locale)).length) {
|
201 |
+
return;
|
202 |
+
} // 3. Hide empty list message if present.
|
203 |
+
|
204 |
+
|
205 |
+
if ($activeLocales.hasClass('empty-list')) {
|
206 |
+
toggleEmptyListMessage();
|
207 |
+
} // 4. Add to list.
|
208 |
+
|
209 |
+
|
210 |
+
$activeLocales.append($newLocale);
|
211 |
+
toggleLocale($newLocale); // 5. Scroll into view.
|
212 |
+
|
213 |
+
$activeLocales.animate({
|
214 |
+
scrollTop: $newLocale.offset().top - $activeLocales.offset().top + $activeLocales.scrollTop()
|
215 |
+
}); // 5. Update hidden input field.
|
216 |
+
|
217 |
+
updateHiddenInput(); // 6. Announce to assistive technologies.
|
218 |
+
|
219 |
+
wp.a11y.speak(settings.l10n.localeAdded);
|
220 |
+
} // Hide original language settings.
|
221 |
+
|
222 |
+
|
223 |
+
$('.user-language-wrap').remove();
|
224 |
+
$('#WPLANG').parent().parent().remove(); // Remove en_US as an option from the dropdown.
|
225 |
+
|
226 |
+
$inactiveLocalesWrap.filter('[data-show-en_US="false"]').find('[lang="en"][value=""]').remove(); // Change initial button state.
|
227 |
+
|
228 |
+
changeButtonState($selectedLocale); // Initially hide already active locales from dropdown.
|
229 |
+
|
230 |
+
if ($inputField.val().length) {
|
231 |
+
$.each($inputField.val().split(','), function (index, value) {
|
232 |
+
value = 'en_US' === value ? '' : value;
|
233 |
+
var $option = $inactiveLocales.find("[value=\"".concat(value, "\"]")); // 2. Hide from dropdown.
|
234 |
+
|
235 |
+
$option.removeAttr('selected').addClass('hidden');
|
236 |
+
});
|
237 |
+
var $firstInactiveLocale = $inactiveLocales.find('option:not(.hidden):first');
|
238 |
+
$firstInactiveLocale.attr('selected', true);
|
239 |
+
$inactiveLocalesControls.val($firstInactiveLocale.val());
|
240 |
+
} // Enabling sorting locales using drag and drop.
|
241 |
+
|
242 |
+
|
243 |
+
$activeLocales.sortable({
|
244 |
+
axis: 'y',
|
245 |
+
cursor: 'move',
|
246 |
+
items: ':not(#active-locales-list-empty-message)'
|
247 |
+
}); // Active locales keyboard shortcuts.
|
248 |
+
|
249 |
+
$document.on('keydown', function (e) {
|
250 |
+
if (!document.querySelector('.preferred-languages').contains(document.activeElement)) {
|
251 |
+
return;
|
252 |
+
}
|
253 |
+
|
254 |
+
switch (e.which) {
|
255 |
+
case KEY_UP:
|
256 |
+
if (e.altKey) {
|
257 |
+
moveLocaleUp();
|
258 |
+
} else if ($selectedLocale.prev().length) {
|
259 |
+
toggleLocale($selectedLocale.prev());
|
260 |
+
} else {
|
261 |
+
// We're at the top of the list.
|
262 |
+
$activeLocales.focus();
|
263 |
+
}
|
264 |
+
|
265 |
+
e.preventDefault();
|
266 |
+
break;
|
267 |
+
|
268 |
+
case KEY_DOWN:
|
269 |
+
if (e.altKey) {
|
270 |
+
moveLocaleDown();
|
271 |
+
} else if ($selectedLocale.next().length) {
|
272 |
+
toggleLocale($selectedLocale.next());
|
273 |
+
} else {
|
274 |
+
// We're at the bottom of the list.
|
275 |
+
$activeLocales.focus();
|
276 |
+
}
|
277 |
+
|
278 |
+
e.preventDefault();
|
279 |
+
break;
|
280 |
+
|
281 |
+
case KEY_A:
|
282 |
+
if (e.altKey) {
|
283 |
+
makeLocaleActive($inactiveLocales.find('option:selected'));
|
284 |
+
}
|
285 |
+
|
286 |
+
e.preventDefault();
|
287 |
+
break;
|
288 |
+
|
289 |
+
case KEY_BACKSPACE:
|
290 |
+
makeLocaleInactive();
|
291 |
+
e.preventDefault();
|
292 |
+
break;
|
293 |
+
}
|
294 |
+
}); // Add new locale to list.
|
295 |
+
|
296 |
+
$inactiveLocalesControls.find('.locales-add').on('click', function () {
|
297 |
+
makeLocaleActive($inactiveLocales.find('option:selected'));
|
298 |
+
}); // Select a locale.
|
299 |
+
|
300 |
+
$activeLocales.on('click', '.active-locale', function (e) {
|
301 |
+
toggleLocale($(e.currentTarget));
|
302 |
+
});
|
303 |
+
$activeLocalesControls.find('.locales-move-up').on('click', moveLocaleUp);
|
304 |
+
$activeLocalesControls.find('.locales-move-down').on('click', moveLocaleDown); // Remove locale from list.
|
305 |
+
|
306 |
+
$activeLocalesControls.find('.locales-remove').on('click', makeLocaleInactive);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
})(wp, preferredLanguages, jQuery);
|
js/preferred-languages.min.js
CHANGED
@@ -1 +1 @@
|
|
1 |
-
"use strict";(function(wp,settings,$){var $document=$(document);var $activeLocales=$(".active-locales-list");var $activeLocalesControls=$(".active-locales-controls");var $emptyMessage=$("#active-locales-empty-message");var $inactiveLocalesWrap=$(".inactive-locales-list");var $inactiveLocales=$inactiveLocalesWrap.find("select");var $inactiveLocalesControls=$(".inactive-locales-controls");var $inputField=$('input[name="preferred_languages"]');var $selectedLocale=$activeLocales.find('li[aria-selected="true"]');var KEY_UP=38;var KEY_DOWN=40;var KEY_BACKSPACE=8;function changeButtonState(activeLocale){var activeLocalesList=$activeLocales.find(".active-locale");$activeLocalesControls.find(".locales-move-up").attr("disabled",$activeLocales.hasClass("empty-list")||activeLocalesList.first().is(activeLocale));$activeLocalesControls.find(".locales-move-down").attr("disabled",$activeLocales.hasClass("empty-list")||activeLocalesList.last().is(activeLocale));$activeLocalesControls.find(".locales-remove").attr("disabled",$activeLocales.hasClass("empty-list"));$inactiveLocalesControls.find(".locales-add").attr("disabled","disabled"===$inactiveLocales.attr("disabled"))}function toggleLocale($locale){var selected=$locale.attr("aria-selected");var newState="true"!==selected;if("true"===selected){return}$selectedLocale.attr("aria-selected",false);$locale.attr("aria-selected",newState);if(true===newState){$selectedLocale=$locale;$activeLocales.attr("aria-activedescendant",$selectedLocale.attr("id"))}changeButtonState($locale)}function updateHiddenInput(){var locales=[];$activeLocales.children(".active-locale").each(function(index,el){locales.push($(el).attr("id"))});$inputField.val(locales.join(","))}function moveLocaleUp(){$selectedLocale.insertBefore($selectedLocale.prev());updateHiddenInput();changeButtonState($selectedLocale);wp.a11y.speak(settings.l10n.movedUp)}function moveLocaleDown(){$selectedLocale.insertAfter($selectedLocale.next());updateHiddenInput();changeButtonState($selectedLocale);wp.a11y.speak(settings.l10n.movedDown)}function toggleEmptyListMessage(){if($activeLocales.hasClass("empty-list")){$activeLocales.removeClass("empty-list");$emptyMessage.addClass("hidden")}else{$activeLocales.addClass("empty-list");$activeLocales.attr("aria-activedescendant","");$emptyMessage.removeClass("hidden");wp.a11y.speak($emptyMessage.data("a11y-message"))}}function makeLocaleInactive(){var locale=$selectedLocale.attr("id");var $successor
|
1 |
+
"use strict";(function(wp,settings,$){var $document=$(document);var $activeLocales=$(".active-locales-list");var $activeLocalesControls=$(".active-locales-controls");var $emptyMessage=$("#active-locales-empty-message");var $inactiveLocalesWrap=$(".inactive-locales-list");var $inactiveLocales=$inactiveLocalesWrap.find("select");var $inactiveLocalesControls=$(".inactive-locales-controls");var $inputField=$('input[name="preferred_languages"]');var $selectedLocale=$activeLocales.find('li[aria-selected="true"]');var KEY_UP=38;var KEY_DOWN=40;var KEY_BACKSPACE=8;var KEY_A=65;function changeButtonState(activeLocale){var activeLocalesList=$activeLocales.find(".active-locale");$activeLocalesControls.find(".locales-move-up").attr("disabled",$activeLocales.hasClass("empty-list")||activeLocalesList.first().is(activeLocale));$activeLocalesControls.find(".locales-move-down").attr("disabled",$activeLocales.hasClass("empty-list")||activeLocalesList.last().is(activeLocale));$activeLocalesControls.find(".locales-remove").attr("disabled",$activeLocales.hasClass("empty-list"));$inactiveLocalesControls.find(".locales-add").attr("disabled","disabled"===$inactiveLocales.attr("disabled"))}function toggleLocale($locale){var selected=$locale.attr("aria-selected");var newState="true"!==selected;if("true"===selected){return}$selectedLocale.attr("aria-selected",false);$locale.attr("aria-selected",newState);if(true===newState){$selectedLocale=$locale;$activeLocales.attr("aria-activedescendant",$selectedLocale.attr("id"))}changeButtonState($locale)}function updateHiddenInput(){var locales=[];$activeLocales.children(".active-locale").each(function(index,el){locales.push($(el).attr("id"))});$inputField.val(locales.join(","))}function moveLocaleUp(){$selectedLocale.insertBefore($selectedLocale.prev());updateHiddenInput();changeButtonState($selectedLocale);wp.a11y.speak(settings.l10n.movedUp)}function moveLocaleDown(){$selectedLocale.insertAfter($selectedLocale.next());updateHiddenInput();changeButtonState($selectedLocale);wp.a11y.speak(settings.l10n.movedDown)}function toggleEmptyListMessage(){if($activeLocales.hasClass("empty-list")){$activeLocales.removeClass("empty-list");$emptyMessage.addClass("hidden")}else{$activeLocales.addClass("empty-list");$activeLocales.attr("aria-activedescendant","");$emptyMessage.removeClass("hidden");wp.a11y.speak($emptyMessage.data("a11y-message"))}}function makeLocaleInactive(){var locale=$selectedLocale.attr("id");var $successor;$successor=$selectedLocale.prevAll(":first");if(0===$successor.length){$successor=$selectedLocale.nextAll(":first")}$selectedLocale.remove();if($successor.length){toggleLocale($successor)}else{toggleEmptyListMessage()}$inactiveLocales.find('option[value="'.concat(locale,'"]')).removeClass("hidden");$inactiveLocales.attr("disabled",false);updateHiddenInput();changeButtonState($selectedLocale);wp.a11y.speak(settings.l10n.localeRemoved)}function makeLocaleActive(option){var locale=option.val()||"en_US";var $newLocale=$("<li/>",{text:option.text(),role:"option","aria-selected":false,id:locale,class:"active-locale"});var $successor;option.removeAttr("selected").addClass("hidden");$successor=option.prevAll(":not(.hidden):first");if(!$successor.length){$successor=option.nextAll(":not(.hidden):first")}if(!$successor.length){$successor=$inactiveLocales.find("option:not(.hidden):first")}if(!$successor.length){$inactiveLocales.attr("disabled",true)}$successor.attr("selected",true);$inactiveLocalesControls.val($successor.val());if($activeLocales.find("#".concat(locale)).length){return}if($activeLocales.hasClass("empty-list")){toggleEmptyListMessage()}$activeLocales.append($newLocale);toggleLocale($newLocale);$activeLocales.animate({scrollTop:$newLocale.offset().top-$activeLocales.offset().top+$activeLocales.scrollTop()});updateHiddenInput();wp.a11y.speak(settings.l10n.localeAdded)}$(".user-language-wrap").remove();$("#WPLANG").parent().parent().remove();$inactiveLocalesWrap.filter('[data-show-en_US="false"]').find('[lang="en"][value=""]').remove();changeButtonState($selectedLocale);if($inputField.val().length){$.each($inputField.val().split(","),function(index,value){value="en_US"===value?"":value;var $option=$inactiveLocales.find('[value="'.concat(value,'"]'));$option.removeAttr("selected").addClass("hidden")});var $firstInactiveLocale=$inactiveLocales.find("option:not(.hidden):first");$firstInactiveLocale.attr("selected",true);$inactiveLocalesControls.val($firstInactiveLocale.val())}$activeLocales.sortable({axis:"y",cursor:"move",items:":not(#active-locales-list-empty-message)"});$document.on("keydown",function(e){if(!document.querySelector(".preferred-languages").contains(document.activeElement)){return}switch(e.which){case KEY_UP:if(e.altKey){moveLocaleUp()}else if($selectedLocale.prev().length){toggleLocale($selectedLocale.prev())}else{$activeLocales.focus()}e.preventDefault();break;case KEY_DOWN:if(e.altKey){moveLocaleDown()}else if($selectedLocale.next().length){toggleLocale($selectedLocale.next())}else{$activeLocales.focus()}e.preventDefault();break;case KEY_A:if(e.altKey){makeLocaleActive($inactiveLocales.find("option:selected"))}e.preventDefault();break;case KEY_BACKSPACE:makeLocaleInactive();e.preventDefault();break}});$inactiveLocalesControls.find(".locales-add").on("click",function(){makeLocaleActive($inactiveLocales.find("option:selected"))});$activeLocales.on("click",".active-locale",function(e){toggleLocale($(e.currentTarget))});$activeLocalesControls.find(".locales-move-up").on("click",moveLocaleUp);$activeLocalesControls.find(".locales-move-down").on("click",moveLocaleDown);$activeLocalesControls.find(".locales-remove").on("click",makeLocaleInactive)})(wp,preferredLanguages,jQuery);
|
preferred-languages.php
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
* Plugin Name: Preferred Languages
|
4 |
* Plugin URI: https://github.com/swissspidy/preferred-languages/
|
5 |
* Description: Choose languages for displaying WordPress in, in order of preference.
|
6 |
-
* Version: 1.
|
7 |
* Author: Pascal Birchler
|
8 |
* Author URI: https://pascalbirchler.com
|
9 |
* License: GPL-2.0+
|
3 |
* Plugin Name: Preferred Languages
|
4 |
* Plugin URI: https://github.com/swissspidy/preferred-languages/
|
5 |
* Description: Choose languages for displaying WordPress in, in order of preference.
|
6 |
+
* Version: 1.5.0
|
7 |
* Author: Pascal Birchler
|
8 |
* Author URI: https://pascalbirchler.com
|
9 |
* License: GPL-2.0+
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ Tags: internationalization, i18n, localization, l10n, language, locale, translat
|
|
4 |
Requires at least: 4.9
|
5 |
Tested up to: 4.9
|
6 |
Requires PHP: 5.2
|
7 |
-
Stable tag: 1.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -31,6 +31,12 @@ If you want to get involved, check out [open issues](https://github.com/swissspi
|
|
31 |
|
32 |
== Changelog ==
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
= 1.4.0 =
|
35 |
|
36 |
* New: Keyboard navigation improvements.
|
@@ -69,6 +75,10 @@ If you want to get involved, check out [open issues](https://github.com/swissspi
|
|
69 |
|
70 |
== Upgrade Notice ==
|
71 |
|
|
|
|
|
|
|
|
|
72 |
= 1.4.0 =
|
73 |
|
74 |
This release contains improvements regarding translation downloads upon saving.
|
4 |
Requires at least: 4.9
|
5 |
Tested up to: 4.9
|
6 |
Requires PHP: 5.2
|
7 |
+
Stable tag: 1.5.0
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
31 |
|
32 |
== Changelog ==
|
33 |
|
34 |
+
= 1.5.0 =
|
35 |
+
|
36 |
+
* Enhancement: Improved compatibility with multilingual plugins like Polylang.
|
37 |
+
* Fixed: Fixes an issue with keyboard navigation.
|
38 |
+
* Fixed: Make sure the correct languages are retrieved when updating the settings.
|
39 |
+
|
40 |
= 1.4.0 =
|
41 |
|
42 |
* New: Keyboard navigation improvements.
|
75 |
|
76 |
== Upgrade Notice ==
|
77 |
|
78 |
+
= 1.5.0 =
|
79 |
+
|
80 |
+
This release addresses an issue with keyboard navigation.
|
81 |
+
|
82 |
= 1.4.0 =
|
83 |
|
84 |
This release contains improvements regarding translation downloads upon saving.
|