Restructured a bit the language management code

This commit is contained in:
pezcurrel 2022-12-28 23:47:23 +01:00
parent db749d2e7d
commit ec33912ff8

View file

@ -630,7 +630,6 @@ if (is_null($oidata)) {
if (!$opts['dryrun']) myq($link,'INSERT INTO InstChecks (InstID, Time, Status) VALUES ('.$instid.', '.$now.', '.$idata['WasLastCheckOk'].')',__LINE__);
// yes: we don't do anything on InstLangs and InstOurLangs otherwise
if ($instanswered && isset($idata['languages']) && is_array($idata['languages']) && count($idata['languages'])>0) {
eecho(1,'«'.$opts['hostname'].'»: declared languages: '.implode(', ',$idata['languages']).N);
if (!$opts['dryrun']) {
@ -642,27 +641,30 @@ if ($instanswered && isset($idata['languages']) && is_array($idata['languages'])
myq($link,'INSERT INTO InstLangs SET InstID='.$instid.', LangID='.$langid.', Pos='.$pos,__LINE__);
}
}
// we try to detect languages only if it's set to the default "en", otherwise it has been explicitly set by admin and we assume it's the mostly used language
$dool=true;
if (!is_null($oidata) && $oidata['OurLangsLock']==1) {
$dool=false;
} elseif ($idata['languages'][0]=='en') {
$idata['ourlanguages']=get_instance_langs($opts['hostname']);
while (count($idata['ourlanguages'])>5)
array_pop($languages);
foreach ($idata['ourlanguages'] as $key=>$lang)
$idata['ourlanguages'][$key]=str_replace('-','_',$lang);
eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);
eecho(1,'«'.$opts['hostname'].'»: wont touch “our languages” because they are locked.'.N);
} else {
$idata['ourlanguages']=$idata['languages'];
}
if ($dool && !$opts['dryrun']) {
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
$pos=0;
foreach ($idata['ourlanguages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],__LINE__);
$pos++;
myq($link,'INSERT INTO InstOurLangs SET InstID='.$instid.', OurLangID='.$langid.', Pos='.$pos,__LINE__);
// we try to detect languages only if first declared language (the only one currently definable by admins)
// is different from the default "en", assuming it's been set to the mostly used language on the instance
if ($idata['languages'][0]=='en') {
$idata['ourlanguages']=get_instance_langs($opts['hostname']);
while (count($idata['ourlanguages'])>5)
array_pop($languages);
foreach ($idata['ourlanguages'] as $key=>$lang)
$idata['ourlanguages'][$key]=str_replace('-','_',$lang);
eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);
} else {
$idata['ourlanguages']=$idata['languages'];
eecho(1,'«'.$opts['hostname'].'»: copied declared languages to detected languages.'.N);
}
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
$pos=0;
foreach ($idata['ourlanguages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],__LINE__);
$pos++;
myq($link,'INSERT INTO InstOurLangs SET InstID='.$instid.', OurLangID='.$langid.', Pos='.$pos,__LINE__);
}
}
}
}