- md5
- b5d2913c41342542eab3b4bb7716803c
- sha1
- 0815c955ee0301e52655bb21af0fe751dea9cca0
- sha256
- 04e39685f92af4d494577c79912d6b30104289268eb62dbd18d7f2e64ad7447c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
<?php
/**
* @package Joomla.Plugin
* @subpackage System.languagecode
*
* @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\System\LanguageCode\Extension;
use Joomla\CMS\Event\Application\AfterRenderEvent;
use Joomla\CMS\Event\Model\PrepareFormEvent;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Language Code plugin class.
*
* @since 2.5
*/
final class LanguageCode extends CMSPlugin implements SubscriberInterface
{
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since 5.3.0
*/
public static function getSubscribedEvents(): array
{
return [
'onAfterRender' => 'onAfterRender',
'onContentPrepareForm' => 'onContentPrepareForm',
];
}
/**
* Plugin that changes the language code used in the <html /> tag.
*
* @param AfterRenderEvent $event The event instance.
*
* @return void
*
* @since 2.5
*/
public function onAfterRender(AfterRenderEvent $event): void
{
$app = $event->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 = $app->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 = [
\chr(1) . '(<html.*\s+xml:lang=")(' . $code . ')(".*>)' . \chr(1) . 'i',
\chr(1) . '(<html.*\s+lang=")(' . $code . ')(".*>)' . \chr(1) . 'i',
];
$replace = [
'${1}' . strtolower($new_code) . '${3}',
'${1}' . strtolower($new_code) . '${3}',
];
} else {
$patterns = [];
$replace = [];
}
// 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 PrepareFormEvent $event The event object
*
* @return void
*
* @since 2.5
*/
public function onContentPrepareForm(PrepareFormEvent $event): void
{
$form = $event->getForm();
// Check we are manipulating the languagecode plugin.
if ($form->getName() !== 'com_plugins.plugin' || !$form->getField('languagecodeplugin', 'params')) {
return;
}
// Get site languages.
if ($languages = LanguageHelper::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(Text::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>');
}
}
}
}
The same content is in 93 releases, Joomla 5.3.0-beta1 to 6.2.0. See them all, or every version of this file.