mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
Merge branch 'MDL-72885-master-2' of https://github.com/junpataleta/moodle
This commit is contained in:
commit
32a995faf2
@ -84,7 +84,7 @@ class editor_framework implements H5peditorStorage {
|
||||
|
||||
// If current language has a dependency, then request it.
|
||||
if (count($dependencies) > 1) {
|
||||
$parentlanguage = str_replace('_', '-', $dependencies[count($dependencies) - 2]);
|
||||
$parentlanguage = get_html_lang_attribute_value($dependencies[count($dependencies) - 2]);
|
||||
$result = $this->get_language_record($name, $major, $minor, $parentlanguage);
|
||||
}
|
||||
}
|
||||
|
@ -1610,7 +1610,7 @@ class framework implements H5PFrameworkInterface {
|
||||
}
|
||||
|
||||
// Get current language in Moodle.
|
||||
$language = str_replace('_', '-', strtolower(\current_language()));
|
||||
$language = get_html_lang_attribute_value(strtolower(\current_language()));
|
||||
|
||||
// Try to map.
|
||||
return $map[$language] ?? $language;
|
||||
|
@ -255,7 +255,7 @@ class zipwriter {
|
||||
|
||||
$templatedata->global = (object) [
|
||||
'righttoleft' => right_to_left(),
|
||||
'language' => str_replace('_', '-', current_language()),
|
||||
'language' => get_html_lang_attribute_value(current_language()),
|
||||
'sitename' => format_string($SITE->fullname, true, ['context' => context_system::instance()]),
|
||||
'siteurl' => $CFG->wwwroot,
|
||||
'pathtotop' => $this->get_relative_context_path($context, $this->rootcontext, '/'),
|
||||
|
@ -96,6 +96,14 @@ class language_menu implements \renderable, \templatable {
|
||||
// Add the lang picker if needed.
|
||||
foreach ($this->langs as $langtype => $langname) {
|
||||
$isactive = $langtype == $this->currentlang;
|
||||
$attributes = [];
|
||||
if (!$isactive) {
|
||||
// Set the lang attribute for languages different from the page's current language.
|
||||
$attributes[] = [
|
||||
'key' => 'lang',
|
||||
'value' => get_html_lang_attribute_value($langtype),
|
||||
];
|
||||
}
|
||||
$node = [
|
||||
'title' => $langname,
|
||||
'text' => $langname,
|
||||
@ -103,6 +111,9 @@ class language_menu implements \renderable, \templatable {
|
||||
'isactive' => $isactive,
|
||||
'url' => $isactive ? new \moodle_url('#') : new \moodle_url($this->page->url, ['lang' => $langtype]),
|
||||
];
|
||||
if (!empty($attributes)) {
|
||||
$node['attributes'] = $attributes;
|
||||
}
|
||||
|
||||
$nodes[] = $node;
|
||||
|
||||
@ -135,8 +146,15 @@ class language_menu implements \renderable, \templatable {
|
||||
}
|
||||
$langmenu->set_menu_trigger($menuname);
|
||||
foreach ($languagedata['items'] as $node) {
|
||||
$lang = new \action_menu_link_secondary($node['url'], null, $node['title'],
|
||||
['data-lang' => $node['url']->get_param('lang')]);
|
||||
$langparam = $node['url']->get_param('lang');
|
||||
$attributes = [];
|
||||
if ($langparam) {
|
||||
$attributes = [
|
||||
'data-lang' => $langparam,
|
||||
'lang' => $langparam,
|
||||
];
|
||||
}
|
||||
$lang = new \action_menu_link_secondary($node['url'], null, $node['title'], $attributes);
|
||||
$langmenu->add($lang);
|
||||
}
|
||||
return $langmenu->export_for_template($output);
|
||||
|
@ -3487,6 +3487,9 @@ class custom_menu_item implements renderable, templatable {
|
||||
*/
|
||||
protected $lastsort = 0;
|
||||
|
||||
/** @var array Array of other HTML attributes for the custom menu item. */
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Constructs the new custom menu item
|
||||
*
|
||||
@ -3496,13 +3499,16 @@ class custom_menu_item implements renderable, templatable {
|
||||
* @param int $sort A sort or to use if we need to sort differently [Optional]
|
||||
* @param custom_menu_item $parent A reference to the parent custom_menu_item this child
|
||||
* belongs to, only if the child has a parent. [Optional]
|
||||
* @param array $attributes Array of other HTML attributes for the custom menu item.
|
||||
*/
|
||||
public function __construct($text, moodle_url $url=null, $title=null, $sort = null, custom_menu_item $parent = null) {
|
||||
public function __construct($text, moodle_url $url = null, $title = null, $sort = null, custom_menu_item $parent = null,
|
||||
array $attributes = []) {
|
||||
$this->text = $text;
|
||||
$this->url = $url;
|
||||
$this->title = $title;
|
||||
$this->sort = (int)$sort;
|
||||
$this->parent = $parent;
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3512,14 +3518,15 @@ class custom_menu_item implements renderable, templatable {
|
||||
* @param moodle_url $url
|
||||
* @param string $title
|
||||
* @param int $sort
|
||||
* @param array $attributes Array of other HTML attributes for the custom menu item.
|
||||
* @return custom_menu_item
|
||||
*/
|
||||
public function add($text, moodle_url $url = null, $title = null, $sort = null) {
|
||||
public function add($text, moodle_url $url = null, $title = null, $sort = null, $attributes = []) {
|
||||
$key = count($this->children);
|
||||
if (empty($sort)) {
|
||||
$sort = $this->lastsort + 1;
|
||||
}
|
||||
$this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this);
|
||||
$this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this, $attributes);
|
||||
$this->lastsort = (int)$sort;
|
||||
return $this->children[$key];
|
||||
}
|
||||
@ -3652,8 +3659,15 @@ class custom_menu_item implements renderable, templatable {
|
||||
$context = new stdClass();
|
||||
$context->text = external_format_string($this->text, $syscontext->id);
|
||||
$context->url = $this->url ? $this->url->out() : null;
|
||||
$context->title = external_format_string($this->title, $syscontext->id);
|
||||
// No need for the title if it's the same with text.
|
||||
if ($this->text !== $this->title) {
|
||||
// Show the title attribute only if it's different from the text.
|
||||
$context->title = external_format_string($this->title, $syscontext->id);
|
||||
}
|
||||
$context->sort = $this->sort;
|
||||
if (!empty($this->attributes)) {
|
||||
$context->attributes = $this->attributes;
|
||||
}
|
||||
$context->children = array();
|
||||
if (preg_match("/^#+$/", $this->text)) {
|
||||
$context->divider = true;
|
||||
|
@ -3823,13 +3823,21 @@ EOD;
|
||||
$strlang = get_string('language');
|
||||
$currentlang = current_language();
|
||||
if (isset($langs[$currentlang])) {
|
||||
$currentlang = $langs[$currentlang];
|
||||
$currentlangstr = $langs[$currentlang];
|
||||
} else {
|
||||
$currentlang = $strlang;
|
||||
$currentlangstr = $strlang;
|
||||
}
|
||||
$this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
|
||||
$this->language = $menu->add($currentlangstr, new moodle_url('#'), $strlang, 10000);
|
||||
foreach ($langs as $langtype => $langname) {
|
||||
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
|
||||
$attributes = [];
|
||||
// Set the lang attribute for languages different from the page's current language.
|
||||
if ($langtype !== $currentlang) {
|
||||
$attributes[] = [
|
||||
'key' => 'lang',
|
||||
'value' => get_html_lang_attribute_value($langtype),
|
||||
];
|
||||
}
|
||||
$this->language->add($langname, new moodle_url($this->page->url, ['lang' => $langtype]), null, null, $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@
|
||||
<div class="dropdown-menu" role="menu" id="drop-down-menu-{{uniqid}}" aria-labelledby="drop-down-{{uniqid}}">
|
||||
{{#children}}
|
||||
{{^divider}}
|
||||
<a class="dropdown-item" role="menuitem" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}}>{{{text}}}</a>
|
||||
<a class="dropdown-item" role="menuitem" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}} {{#attributes}}{{key}}="{{value}}" {{/attributes}}>{{{text}}}</a>
|
||||
{{/divider}}
|
||||
{{#divider}}
|
||||
<div class="dropdown-divider" role="presentation"></div>
|
||||
|
@ -41,7 +41,8 @@
|
||||
}}
|
||||
{{#items}}
|
||||
{{#link}}
|
||||
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" tabindex="-1" {{#isactive}}aria-current="true"{{/isactive}}>
|
||||
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" tabindex="-1" {{#isactive}}aria-current="true"{{/isactive}}
|
||||
{{#attributes}}{{key}}="{{value}}" {{/attributes}}>
|
||||
{{text}}
|
||||
</a>
|
||||
{{/link}}
|
||||
|
@ -72,10 +72,19 @@ class language_menu_test extends \advanced_testcase {
|
||||
// Assert that the number of language menu items matches the number of the expected items.
|
||||
$this->assertEquals(count($expected['items']), count($response['items']));
|
||||
foreach ($expected['items'] as $expecteditem) {
|
||||
$lang = $expecteditem['lang'];
|
||||
// We need to manually generate the url key and its value in the expected item array as this cannot
|
||||
// be done in the data provider due to the change of the state of $PAGE.
|
||||
$expecteditem['url'] = $expecteditem['isactive'] ? new \moodle_url('#') :
|
||||
new \moodle_url($PAGE->url, ['lang' => $expecteditem['lang']]);
|
||||
if ($expecteditem['isactive']) {
|
||||
$expecteditem['url'] = new \moodle_url('#');
|
||||
} else {
|
||||
$expecteditem['url'] = new \moodle_url($PAGE->url, ['lang' => $lang]);
|
||||
// When the language menu item is not the current language, it will contain the lang attribute.
|
||||
$expecteditem['attributes'][] = [
|
||||
'key' => 'lang',
|
||||
'value' => $lang
|
||||
];
|
||||
}
|
||||
// The lang value is only used to generate the url, so this key can be removed.
|
||||
unset($expecteditem['lang']);
|
||||
|
||||
|
@ -941,4 +941,30 @@ EXPECTED;
|
||||
|
||||
$this->assertNotEquals($policydisabled, print_password_policy());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for the testing get_html_lang_attribute_value().
|
||||
*
|
||||
* @return string[][]
|
||||
*/
|
||||
public function get_html_lang_attribute_value_provider() {
|
||||
return [
|
||||
'Empty lang code' => [' ', 'unknown'],
|
||||
'English' => ['en', 'en'],
|
||||
'English, US' => ['en_us', 'en-us'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_html_lang_attribute_value().
|
||||
*
|
||||
* @covers ::get_html_lang_attribute_value()
|
||||
* @dataProvider get_html_lang_attribute_value_provider
|
||||
* @param string $langcode The language code to convert.
|
||||
* @param string $expected The expected converted value.
|
||||
* @return void
|
||||
*/
|
||||
public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
|
||||
$this->assertEquals($expected, get_html_lang_attribute_value($langcode));
|
||||
}
|
||||
}
|
||||
|
@ -2206,6 +2206,24 @@ function highlightfast($needle, $haystack) {
|
||||
return str_replace('<span class="highlight"></span>', '', join('', $parts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a language code to hyphen-separated format in accordance to the
|
||||
* {@link https://datatracker.ietf.org/doc/html/rfc5646#section-2.1 BCP47 syntax}.
|
||||
*
|
||||
* For additional information, check out
|
||||
* {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang MDN web docs - lang}.
|
||||
*
|
||||
* @param string $langcode The language code to convert.
|
||||
* @return string
|
||||
*/
|
||||
function get_html_lang_attribute_value(string $langcode): string {
|
||||
if (empty(trim($langcode))) {
|
||||
// If the language code passed is an empty string, return 'unknown'.
|
||||
return 'unknown';
|
||||
}
|
||||
return str_replace('_', '-', $langcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string containing 'lang', xml:lang and optionally 'dir' HTML attributes.
|
||||
*
|
||||
@ -2224,7 +2242,7 @@ function get_html_lang($dir = false) {
|
||||
}
|
||||
}
|
||||
// Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
|
||||
$language = str_replace('_', '-', current_language());
|
||||
$language = get_html_lang_attribute_value(current_language());
|
||||
@header('Content-Language: '.$language);
|
||||
return ($direction.' lang="'.$language.'" xml:lang="'.$language.'"');
|
||||
}
|
||||
|
@ -53,7 +53,8 @@
|
||||
<div role="menu" aria-labelledby="lang-menu-toggle" id="lang-action-menu" class="dropdown-menu dropdown-menu-right">
|
||||
{{#items}}
|
||||
{{#link}}
|
||||
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" {{#isactive}}aria-current="true"{{/isactive}}>
|
||||
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" {{#isactive}}aria-current="true"{{/isactive}}
|
||||
{{#attributes}}{{key}}="{{value}}" {{/attributes}}>
|
||||
{{text}}
|
||||
</a>
|
||||
{{/link}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user