-
Notifications
You must be signed in to change notification settings - Fork 1
/
kdk-change-locale.php
50 lines (42 loc) · 1.32 KB
/
kdk-change-locale.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
<?php
/*
* Plugin Name: KDK Change Locale (by Kodeka)
* Plugin URI: https://github.com/kodeka/kdk_change_locale
* Description: Change the site's locale based on active category
* Version: 1.0.0
* Author: Kodeka
* Author URI: https://kodeka.io
* License: GNU/GPL https://www.gnu.org/copyleft/gpl.html
*/
defined('ABSPATH') or die('Move along');
function kdk_set_locale($locale)
{
// Don't execute in the backend
if (is_admin()) {
return;
}
/*
$protocol = (stripos($_SERVER['SERVER_PROTOCOL'],'https') === true) ? 'https://' : 'http://';
$url_path = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
*/
$url_path = $_SERVER['REQUEST_URI'];
$languages = array(
'en' => 'en_US', // US English
'de' => 'de_DE', // German
'fr' => 'fr_FR', // French
'es' => 'es_ES', // Spanish
'ru' => 'ru_RU', // Russian
);
$new_locale = '';
foreach ($languages as $cat_slug => $lcl) {
if (substr($url_path, -3) == '/'.$cat_slug || stripos($url_path, '/'.$cat_slug.'/') !== false || (isset($_GET["lang"]) && $_GET["lang"] === $cat_slug)) {
$new_locale = $lcl;
}
}
if ($new_locale) {
return $new_locale;
} else {
return $locale;
}
}
add_filter('locale', 'kdk_set_locale', 10);