Skip to content

Commit 1990289

Browse files
committed
Improve list_languages to show the local name of the language, too
1 parent 3cad370 commit 1990289

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

classes/output/filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ protected function export_for_template_flng(\renderer_base $outpup, object $filt
518518
'someselected' => false,
519519
];
520520

521-
foreach (\mlang_tools::list_languages(false) as $langcode => $langname) {
521+
foreach (\mlang_tools::list_languages(false, true, true, true) as $langcode => $langname) {
522522
$option = [
523523
'value' => $langcode,
524524
'text' => $langname,

mlanglib.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,9 +1735,10 @@ class mlang_tools {
17351735
* @param bool $english shall the English be included?
17361736
* @param bool $usecache can the internal cache be used?
17371737
* @param bool $showcode append language code to the name
1738+
* @param bool $showlocal also show the language name in the language itself
17381739
* @return array (string)langcode => (string)langname
17391740
*/
1740-
public static function list_languages($english=true, $usecache=true, $showcode=true) {
1741+
public static function list_languages($english=true, $usecache=true, $showcode=true, $showlocal=false) {
17411742
global $DB;
17421743

17431744
$cache = cache::make('local_amos', 'lists');
@@ -1769,9 +1770,9 @@ public static function list_languages($english=true, $usecache=true, $showcode=t
17691770
$rs = $DB->get_recordset_sql($sql);
17701771

17711772
foreach ($rs as $lang) {
1772-
if (!isset($langs[$lang->code])) {
1773+
if (!isset($langs[$lang->code][$lang->strname])) {
17731774
// Use the first returned value, all others are historical records.
1774-
$langs[$lang->code] = $lang->name;
1775+
$langs[$lang->code][$lang->strname] = $lang->name;
17751776
}
17761777
}
17771778

@@ -1780,16 +1781,26 @@ public static function list_languages($english=true, $usecache=true, $showcode=t
17801781
$cache->set('languages', $langs);
17811782
}
17821783

1783-
if ($showcode) {
1784-
foreach (array_keys($langs) as $code) {
1785-
$langs[$code] .= ' (' . $code . ')';
1784+
$result = [];
1785+
1786+
foreach ($langs as $code => $names) {
1787+
$lang = $names['thislanguageint'] ?? '???';
1788+
1789+
if ($showlocal && $code !== 'en' && substr($code, 0, 3) !== 'en_') {
1790+
$lang .= ' / ' . ($names['thislanguage'] ?? '???');
17861791
}
1792+
1793+
if ($showcode) {
1794+
$lang .= ' [' . $code . ']';
1795+
}
1796+
1797+
$result[$code] = $lang;
17871798
}
17881799

17891800
if ($english) {
1790-
return $langs;
1801+
return $result;
17911802
} else {
1792-
return array_diff_key($langs, array('en' => 'English'));
1803+
return array_diff_key($result, ['en' => '']);
17931804
}
17941805
}
17951806

tests/mlanglib_test.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,34 +1063,60 @@ public function test_list_languages() {
10631063
$stage = new mlang_stage();
10641064
$component = new mlang_component('langconfig', 'en', mlang_version::by_branch('MOODLE_19_STABLE'));
10651065
$component->add_string(new mlang_string('thislanguageint', 'English'));
1066+
$component->add_string(new mlang_string('thislanguage', 'English'));
10661067
$stage->add($component);
10671068
$component->clear();
10681069

10691070
$component = new mlang_component('langconfig', 'cs', mlang_version::by_branch('MOODLE_20_STABLE'));
10701071
$component->add_string(new mlang_string('thislanguageint', 'Czech'));
1072+
$component->add_string(new mlang_string('thislanguage', 'Česky'));
10711073
$stage->add($component);
10721074
$component->clear();
10731075

10741076
$component = new mlang_component('langconfig', 'cs', mlang_version::by_branch('MOODLE_19_STABLE'));
10751077
$component->add_string(new mlang_string('thislanguageint', 'CS'));
1078+
$component->add_string(new mlang_string('thislanguage', 'ČS'));
10761079
$stage->add($component);
10771080
$component->clear();
10781081

1079-
$stage->commit('Registering two languages', array('source' => 'unittest'));
1082+
$component = new mlang_component('langconfig', 'xx', mlang_version::by_branch('MOODLE_21_STABLE'));
1083+
$component->add_string(new mlang_string('thislanguage', 'Xx'));
1084+
$stage->add($component);
1085+
$component->clear();
1086+
1087+
$component = new mlang_component('langconfig', 'yy', mlang_version::by_branch('MOODLE_23_STABLE'));
1088+
$component->add_string(new mlang_string('thislanguageint', 'Yy'));
1089+
$stage->add($component);
1090+
$component->clear();
1091+
1092+
$stage->commit('Registering languages', array('source' => 'unittest'));
10801093

10811094
$langs = mlang_tools::list_languages(true, true, false);
10821095
$this->assertEquals(gettype($langs), 'array');
1083-
$this->assertEquals(count($langs), 2);
1096+
$this->assertEquals(count($langs), 4);
10841097
$this->assertTrue(array_key_exists('cs', $langs));
10851098
$this->assertTrue(array_key_exists('en', $langs));
10861099
$this->assertEquals($langs['en'], 'English');
10871100
$this->assertEquals($langs['cs'], 'Czech');
1101+
$this->assertEquals($langs['xx'], '???');
1102+
$this->assertEquals($langs['yy'], 'Yy');
10881103

10891104
$langs = mlang_tools::list_languages(false, true, true);
10901105
$this->assertEquals(gettype($langs), 'array');
1091-
$this->assertEquals(count($langs), 1);
1106+
$this->assertEquals(count($langs), 3);
1107+
$this->assertTrue(array_key_exists('cs', $langs));
1108+
$this->assertEquals($langs['cs'], 'Czech [cs]');
1109+
$this->assertEquals($langs['xx'], '??? [xx]');
1110+
$this->assertEquals($langs['yy'], 'Yy [yy]');
1111+
1112+
$langs = mlang_tools::list_languages(true, true, true, true);
1113+
$this->assertEquals(gettype($langs), 'array');
1114+
$this->assertEquals(count($langs), 4);
10921115
$this->assertTrue(array_key_exists('cs', $langs));
1093-
$this->assertEquals($langs['cs'], 'Czech (cs)');
1116+
$this->assertEquals($langs['en'], 'English [en]');
1117+
$this->assertEquals($langs['cs'], 'Czech / Česky [cs]');
1118+
$this->assertEquals($langs['xx'], '??? / Xx [xx]');
1119+
$this->assertEquals($langs['yy'], 'Yy / ??? [yy]');
10941120
}
10951121

10961122
public function test_list_components() {

0 commit comments

Comments
 (0)