languagecode.php
4.02 KB
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
<?php
/**
* @package Joomla.Plugin
* @subpackage System.languagecode
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Language Code plugin class.
*
* @since 2.5
*/
class PlgSystemLanguagecode extends JPlugin
{
/**
* Plugin that changes the language code used in the <html /> tag.
*
* @return void
*
* @since 2.5
*/
public function onAfterRender()
{
$app = JFactory::getApplication();
// Use this plugin only in site application.
if ($app->isClient('site'))
{
// Get the response body.
$body = $app->getBody();
// Get the current language code.
$code = JFactory::getDocument()->getLanguage();
// Get the new code.
$new_code = $this->params->get($code);
// Replace the old code by the new code in the <html /> tag.
if ($new_code)
{
// Replace the new code in the HTML document.
$patterns = array(
chr(1) . '(<html.*\s+xml:lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
chr(1) . '(<html.*\s+lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
);
$replace = array(
'${1}' . strtolower($new_code) . '${3}',
'${1}' . strtolower($new_code) . '${3}'
);
}
else
{
$patterns = array();
$replace = array();
}
// Replace codes in <link hreflang="" /> attributes.
preg_match_all(chr(1) . '(<link.*\s+hreflang=")([0-9a-z\-]*)(".*\s+rel="alternate".*/>)' . chr(1) . 'i', $body, $matches);
foreach ($matches[2] as $match)
{
$new_code = $this->params->get(strtolower($match));
if ($new_code)
{
$patterns[] = chr(1) . '(<link.*\s+hreflang=")(' . $match . ')(".*\s+rel="alternate".*/>)' . chr(1) . 'i';
$replace[] = '${1}' . $new_code . '${3}';
}
}
preg_match_all(chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")([0-9A-Za-z\-]*)(".*/>)' . chr(1) . 'i', $body, $matches);
foreach ($matches[2] as $match)
{
$new_code = $this->params->get(strtolower($match));
if ($new_code)
{
$patterns[] = chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")(' . $match . ')(".*/>)' . chr(1) . 'i';
$replace[] = '${1}' . $new_code . '${3}';
}
}
// Replace codes in itemprop content
preg_match_all(chr(1) . '(<meta.*\s+itemprop="inLanguage".*\s+content=")([0-9A-Za-z\-]*)(".*/>)' . chr(1) . 'i', $body, $matches);
foreach ($matches[2] as $match)
{
$new_code = $this->params->get(strtolower($match));
if ($new_code)
{
$patterns[] = chr(1) . '(<meta.*\s+itemprop="inLanguage".*\s+content=")(' . $match . ')(".*/>)' . chr(1) . 'i';
$replace[] = '${1}' . $new_code . '${3}';
}
}
$app->setBody(preg_replace($patterns, $replace, $body));
}
}
/**
* Prepare form.
*
* @param JForm $form The form to be altered.
* @param mixed $data The associated data for the form.
*
* @return boolean
*
* @since 2.5
*/
public function onContentPrepareForm(JForm $form, $data)
{
// Check we are manipulating the languagecode plugin.
if ($form->getName() !== 'com_plugins.plugin' || !$form->getField('languagecodeplugin', 'params'))
{
return true;
}
// Get site languages.
if ($languages = JLanguageHelper::getKnownLanguages(JPATH_SITE))
{
// Inject fields into the form.
foreach ($languages as $tag => $language)
{
$form->load('
<form>
<fields name="params">
<fieldset
name="languagecode"
label="PLG_SYSTEM_LANGUAGECODE_FIELDSET_LABEL"
description="PLG_SYSTEM_LANGUAGECODE_FIELDSET_DESC"
>
<field
name="' . strtolower($tag) . '"
type="text"
label="' . $tag . '"
description="' . htmlspecialchars(JText::sprintf('PLG_SYSTEM_LANGUAGECODE_FIELD_DESC', $language['name']), ENT_COMPAT, 'UTF-8') . '"
translate_description="false"
translate_label="false"
size="7"
filter="cmd"
/>
</fieldset>
</fields>
</form>
');
}
}
return true;
}
}