Hi Michael,
That was it. After installing the update everything works great, and I was able to get my plugin working too! I decided the easiest way to do it was pull the normal list using get_list_of_translations and then remove anything that is not in the custom course field 'courselanglist', which is a comma separated list of ISO lang codes. Interestingly, after implementing this, your formatlangname function had to be adjusted to remove the "-4" when removing the ISO code in parentheses. Not sure why. I'm also guessing there is a way to capture my course field without needing to cycle through them all, but I couldn't figure it out. (I should probably sit down and really learn PHP instead of this hacky approach I'm taking. I keep hoping that I'm done programming, but then keep finding new things to do. ;) )
That was it. After installing the update everything works great, and I was able to get my plugin working too! I decided the easiest way to do it was pull the normal list using get_list_of_translations and then remove anything that is not in the custom course field 'courselanglist', which is a comma separated list of ISO lang codes. Interestingly, after implementing this, your formatlangname function had to be adjusted to remove the "-4" when removing the ISO code in parentheses. Not sure why. I'm also guessing there is a way to capture my course field without needing to cycle through them all, but I couldn't figure it out. (I should probably sit down and really learn PHP instead of this hacky approach I'm taking. I keep hoping that I'm done programming, but then keep finding new things to do. ;) )
The last little annoyance I have is that when switching to a course from a language that is not allowed in the course. For example, if I'm on the dashboard in French and go to a course that only has English and German, the course content is displayed in English as it should, but all the structure around it is still in French. See the screenshot below. It stays that way until selecting a language from the language menu. So apparently "$this->page->course->lang = "en";" isn't doing all I had hoped it would. Any ideas on how to set the language for the whole page to a default?
Thanks,
Aaron
class theme_elemacad_core_renderer extends core_renderer {
/* Language menu customization by Michael Milette - May 3, 2020
* Trim unwanted parts of the language name.
*/
private function formatlangname($langname) {
// Remove the ISO language code between parentheses. e.g. English (en).
if (strpos($langname, '(')) {
// $langname = substr($langname, 0, strpos($langname, '(') - 4);
$langname = substr($langname, 0, strpos($langname, '('));
}
// Remove the dialect part of the language (e.g. Français - Canada (fr_ca)).
if (strpos($langname, ' - ')) {
$langname = substr($langname, 0, strpos($langname, ' - '));
}
return trim($langname);
}
/**
* We want to show the custom menus as a list of links in the footer on small screens.
* Just return the menu object exported so we can render it differently.
*/
public function custom_menu_flat() {
global $CFG;
$custommenuitems = '';
if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
$custommenuitems = $CFG->custommenuitems;
}
$custommenu = new custom_menu($custommenuitems, current_language());
// Set the language list using custom function.
$langs = $this->get_course_langlist();
if (empty($langs)){
// Use the default function
$langs = get_string_manager()->get_list_of_translations();
}
$haslangmenu = $this->lang_menu() != '';
if ($haslangmenu) {
$strlang = get_string('language');
$currentlang = current_language();
if (isset($langs[$currentlang])) {
$currentlang = $langs[$currentlang];
} else {
$currentlang = $strlang;
$this->page->course->lang = "en";
}
$currentlang = $this->formatlangname($currentlang);
$this->language = $custommenu->add($currentlang, new moodle_url('#'), $strlang, 10000);
foreach ($langs as $langtype => $langname) {
$langname = $this->formatlangname($langname);
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
}
}
return $custommenu->export_for_template($this);
}
/**
* Renders a custom menu object (located in outputcomponents.php)
*
* The custom menu this method produces makes use of the YUI3 menunav widget
* and requires very specific html elements and classes.
*
* @staticvar int $menucount
* @param custom_menu $menu
* @return string
*/
protected function render_custom_menu(custom_menu $menu) {
global $CFG;
// Set the language list using custom function.
$langs = $this->get_course_langlist();
if (empty($langs)){
// Use the default function
$langs = get_string_manager()->get_list_of_translations();
}
$haslangmenu = $this->lang_menu() != '';
if (!$menu->has_children() && !$haslangmenu) {
return '';
}
if ($haslangmenu) {
$strlang = get_string('language');
$currentlang = current_language();
if (isset($langs[$currentlang])) {
$currentlang = $langs[$currentlang];
} else {
$currentlang = $strlang;
$this->page->course->lang = "en";
}
$currentlang = $this->formatlangname($currentlang);
$this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
foreach ($langs as $langtype => $langname) {
$langname = $this->formatlangname($langname);
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
}
}
$content = '';
foreach ($menu->get_children() as $item) {
$context = $item->export_for_template($this);
$content .= $this->render_from_template('core/custom_menu_item', $context);
}
return $content;
}
private function get_course_langlist() {
global $CFG, $PAGE;
// Get site lang list.
$site_langs = get_string_manager()->get_list_of_translations();
// Get the custom course field with lang menu list (courselanglist). Modified from filter_filtercodes
static $coursefields;
$course_langs = [];
if (!isset($coursefields)) {
$handler = core_course\customfield\course_handler::create();
$coursefields = $handler->export_instance_data_object($PAGE->course->id, true);
}
foreach ($coursefields as $field => $value) {
$shortname = strtolower($field);
// Just get courselanglist
if ($shortname === 'courselanglist') {
$courselangliststring = $value;
}
}
// Remove all language options from site lang list that aren't in course lang list.
if(empty($courselangliststring)){
return $site_langs;
} else {
$course_langs = $site_langs;
foreach ($course_langs as $field => $value) {
if (strpos($courselangliststring, $field) === false) {
unset($course_langs[$field]);
}
}
}
return $course_langs;
}
}
/* Language menu customization by Michael Milette - May 3, 2020
* Trim unwanted parts of the language name.
*/
private function formatlangname($langname) {
// Remove the ISO language code between parentheses. e.g. English (en).
if (strpos($langname, '(')) {
// $langname = substr($langname, 0, strpos($langname, '(') - 4);
$langname = substr($langname, 0, strpos($langname, '('));
}
// Remove the dialect part of the language (e.g. Français - Canada (fr_ca)).
if (strpos($langname, ' - ')) {
$langname = substr($langname, 0, strpos($langname, ' - '));
}
return trim($langname);
}
/**
* We want to show the custom menus as a list of links in the footer on small screens.
* Just return the menu object exported so we can render it differently.
*/
public function custom_menu_flat() {
global $CFG;
$custommenuitems = '';
if (empty($custommenuitems) && !empty($CFG->custommenuitems)) {
$custommenuitems = $CFG->custommenuitems;
}
$custommenu = new custom_menu($custommenuitems, current_language());
// Set the language list using custom function.
$langs = $this->get_course_langlist();
if (empty($langs)){
// Use the default function
$langs = get_string_manager()->get_list_of_translations();
}
$haslangmenu = $this->lang_menu() != '';
if ($haslangmenu) {
$strlang = get_string('language');
$currentlang = current_language();
if (isset($langs[$currentlang])) {
$currentlang = $langs[$currentlang];
} else {
$currentlang = $strlang;
$this->page->course->lang = "en";
}
$currentlang = $this->formatlangname($currentlang);
$this->language = $custommenu->add($currentlang, new moodle_url('#'), $strlang, 10000);
foreach ($langs as $langtype => $langname) {
$langname = $this->formatlangname($langname);
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
}
}
return $custommenu->export_for_template($this);
}
/**
* Renders a custom menu object (located in outputcomponents.php)
*
* The custom menu this method produces makes use of the YUI3 menunav widget
* and requires very specific html elements and classes.
*
* @staticvar int $menucount
* @param custom_menu $menu
* @return string
*/
protected function render_custom_menu(custom_menu $menu) {
global $CFG;
// Set the language list using custom function.
$langs = $this->get_course_langlist();
if (empty($langs)){
// Use the default function
$langs = get_string_manager()->get_list_of_translations();
}
$haslangmenu = $this->lang_menu() != '';
if (!$menu->has_children() && !$haslangmenu) {
return '';
}
if ($haslangmenu) {
$strlang = get_string('language');
$currentlang = current_language();
if (isset($langs[$currentlang])) {
$currentlang = $langs[$currentlang];
} else {
$currentlang = $strlang;
$this->page->course->lang = "en";
}
$currentlang = $this->formatlangname($currentlang);
$this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
foreach ($langs as $langtype => $langname) {
$langname = $this->formatlangname($langname);
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
}
}
$content = '';
foreach ($menu->get_children() as $item) {
$context = $item->export_for_template($this);
$content .= $this->render_from_template('core/custom_menu_item', $context);
}
return $content;
}
private function get_course_langlist() {
global $CFG, $PAGE;
// Get site lang list.
$site_langs = get_string_manager()->get_list_of_translations();
// Get the custom course field with lang menu list (courselanglist). Modified from filter_filtercodes
static $coursefields;
$course_langs = [];
if (!isset($coursefields)) {
$handler = core_course\customfield\course_handler::create();
$coursefields = $handler->export_instance_data_object($PAGE->course->id, true);
}
foreach ($coursefields as $field => $value) {
$shortname = strtolower($field);
// Just get courselanglist
if ($shortname === 'courselanglist') {
$courselangliststring = $value;
}
}
// Remove all language options from site lang list that aren't in course lang list.
if(empty($courselangliststring)){
return $site_langs;
} else {
$course_langs = $site_langs;
foreach ($course_langs as $field => $value) {
if (strpos($courselangliststring, $field) === false) {
unset($course_langs[$field]);
}
}
}
return $course_langs;
}
}