MDL-80185 lang: use iso6391 language code for HTML lang attributes.

This resolves accessibility issues where Moodle language pack codes
didn't always map to correspondingly named iso6391 codes.

Where this value is defined in the language configuration, it will
now be used.
This commit is contained in:
Paul Holden
2023-11-21 13:58:37 +00:00
parent b58d1fd4e2
commit 4be39296d6
3 changed files with 16 additions and 7 deletions
+1 -1
View File
@@ -151,7 +151,7 @@ class language_menu implements \renderable, \templatable {
if ($langparam) {
$attributes = [
'data-lang' => $langparam,
'lang' => $langparam,
'lang' => get_html_lang_attribute_value($langparam),
];
}
$lang = new \action_menu_link_secondary($node['url'], null, $node['title'], $attributes);
+3 -2
View File
@@ -1163,9 +1163,10 @@ EXPECTED;
*/
public function get_html_lang_attribute_value_provider() {
return [
'Empty lang code' => [' ', 'unknown'],
'Empty lang code' => [' ', 'en'],
'English' => ['en', 'en'],
'English, US' => ['en_us', 'en-us'],
'English, US' => ['en_us', 'en'],
'Unknown' => ['xx', 'en'],
];
}
+12 -4
View File
@@ -2297,11 +2297,19 @@ function highlightfast($needle, $haystack) {
* @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';
$langcode = clean_param($langcode, PARAM_LANG);
if ($langcode === '') {
return 'en';
}
return str_replace('_', '-', $langcode);
// Grab language ISO code from lang config. If it differs from English, then it's been specified and we can return it.
$langiso = (string) (new lang_string('iso6391', 'core_langconfig', null, $langcode));
if ($langiso !== 'en') {
return $langiso;
}
// Where we cannot determine the value from lang config, use the first two characters from the lang code.
return substr($langcode, 0, 2);
}
/**