diff --git a/h5p/classes/editor_framework.php b/h5p/classes/editor_framework.php index dc4e4216990..0ab8b956338 100644 --- a/h5p/classes/editor_framework.php +++ b/h5p/classes/editor_framework.php @@ -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); } } diff --git a/h5p/classes/framework.php b/h5p/classes/framework.php index 4c1241b83d1..c43fe95f1f6 100644 --- a/h5p/classes/framework.php +++ b/h5p/classes/framework.php @@ -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; diff --git a/lib/classes/content/export/zipwriter.php b/lib/classes/content/export/zipwriter.php index 438b2e32aa8..5666cf3c83d 100644 --- a/lib/classes/content/export/zipwriter.php +++ b/lib/classes/content/export/zipwriter.php @@ -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, '/'), diff --git a/lib/classes/output/language_menu.php b/lib/classes/output/language_menu.php index 36384725a18..846362bbe5e 100644 --- a/lib/classes/output/language_menu.php +++ b/lib/classes/output/language_menu.php @@ -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); diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 613ef61dbab..c1b22e185f4 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -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; diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index e86201b8015..f8efbe6e4f0 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -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); } } diff --git a/lib/templates/custom_menu_item.mustache b/lib/templates/custom_menu_item.mustache index 5bff2f9757d..006da02e015 100644 --- a/lib/templates/custom_menu_item.mustache +++ b/lib/templates/custom_menu_item.mustache @@ -39,7 +39,7 @@