Added translation support and locale management; bumped version to 0.5.7

This commit is contained in:
pezcurrel 2024-11-07 20:34:44 +01:00
parent 2a511daa27
commit 72f1c48b44

View file

@ -18,7 +18,7 @@
$SNAME='GancioF2F';
$ENAME=strtolower($SNAME);
$SVERS='0.5.6';
$SVERS='0.5.7';
require __DIR__.'/lib/getconf.php';
require __DIR__.'/lib/ckmkeys.php';
@ -316,6 +316,17 @@ if (is_null($conf['max_post_length'])) {
}
//print_r($conf);
if (($locale=getlocale())===false) dieYoung("Error: could not get current system locale.\n",1);
if (putenv("LC_MESSAGES={$locale}")===false) dieYoung("Error: could not set environment «LC_MESSAGES» to current system locale «{$locale}».\n",1);
if (setlocale(LC_MESSAGES,$locale)===false) dieYoung("Error: could not set internal «LC_MESSAGES» to current system locale «{$locale}».\n",1);
$linkpath=__DIR__."/storage/locale/{$locale}";
if (file_exists($linkpath) && is_link($linkpath) && @unlink($linkpath)===false) dieYoung("Error: could not delete «{$linkpath}».\n",1);
$target=__DIR__."/locale/{$conf['posts_language']}";
if (symlink($target,$linkpath)===false) dieYoung("Error: could not symlink «{$target}» to «{$linkpath}».\n",1);
bindtextdomain($ENAME,__DIR__.'/storage/locale')."\n";
textdomain($ENAME)."\n";
bind_textdomain_codeset($ENAME,'UTF-8')."\n";
vecho($opts['verbose'],"Info: trying to load the references to already posted announcements from state file «{$conf['state_file_absolute_path']}».\n");
$refs=[];
if (file_exists($conf['state_file_absolute_path'])) {
@ -370,7 +381,9 @@ if ($itemsCount==0) exitYoung("Info: feed from «{$url}» was empty, bye.\n");
vecho($opts['verbose'],"Info: got feed with {$itemsCount} announcement(s) from «{$url}».\n");
//file_put_contents(__DIR__.'/storage/dump-'.time(),print_r($feed,true));
$ordinals=[-1=>_('ultimo'), 1=>_('primo'), 2=>_('secondo'), 3=>_('terzo'), 4=>_('quarto'), 5=>_('quinto'), 6=>_('sesto'), 7=>_('settimo'), 8=>_('ottavo'), 9=>_('nono'), 10=>_('decimo')];
// Translation: ordinals
$ordinals=[-1=>_('last'), 1=>_('first'), 2=>_('second'), 3=>_('third'), 4=>_('fourth'), 5=>_('fifth'), 6=>_('sixth'), 7=>_('seventh'), 8=>_('eighth'), 9=>_('ninth'), 10=>_('tenth')];
//date_default_timezone_set($conf['timezone']);
//$dfmt=datefmt_create('it',IntlDateFormatter::FULL,IntlDateFormatter::SHORT,$conf['timezone'],IntlDateFormatter::GREGORIAN,"eeee d MMMM '"._('alle')."' HH:mm");
$tsfp="{$conf['state_file_absolute_path']}.tmp";
@ -401,17 +414,19 @@ foreach ($feed as $index=>$item) {
} else {
$itemsToPost++;
$postHead="{$item['title']}\n\n";
// ogni settimana: 1w; ogni due settimane: 2w; ogni giorno 6 di ogni mese: 1m, type ordinal; ogni primo mercoledì del mese: 1m senza type
if ($item['multidate']) {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM '"._('alle')."' HH:mm");
$postHead.=_('Da').' '.datefmt_format($dfmt,$item['start_datetime']).' '._('a').' '.datefmt_format($dfmt,$item['end_datetime']);
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM");
// Translation: here, "From" refers to a date, like in "From Wednesday 5 January"
$postHead.=_('From').' '.datefmt_format($dfmt,$item['start_datetime']);
// Translation: here, "to" refers to a date, like in "to Friday 7 January"; please *don't* translate "{date}"
$postHead.=' '._('to{date}').' '.datefmt_format($dfmt,$item['end_datetime']);
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"HH:mm");
// Translation: here, "from" refers to a time, like in "from 23:15"
$postHead.=', '._('from').' '.datefmt_format($dfmt,$item['start_datetime']);
// Translation: here, "to" refers to a time, like in "to 23:30"; please *don't* translate "{time}"
$postHead.=' '._('to{time}').' '.datefmt_format($dfmt,$item['end_datetime']);
} else {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM '"._('dalle')."' HH:mm");
$postHead.=mb_ucfirst(datefmt_format($dfmt,$item['start_datetime']));
if (!is_null($item['end_datetime'])) {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"HH:mm");
$postHead.=' '._('alle').' '.datefmt_format($dfmt,$item['end_datetime']);
}
$recurrence='';
if (!is_null($item['parentId'])) {
$url="https://{$conf['feed_hostname']}/api/event/detail/{$item['parentId']}";
vecho($opts['verbose'],"Info: looks like announcement «{$postUrl}» is recurrent, trying to fetch recurrence info from «{$url}».\n");
@ -430,24 +445,44 @@ foreach ($feed as $index=>$item) {
$res=$res['content'];
if ($res['recurrent']['frequency']=='1w') {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
$postHead.=' ('.sprintf(_('e ogni %s'),datefmt_format($dfmt,$res['start_datetime'])).')';
// Translation: here, "every" refers to a day of the week, like in "and every Wednesday"
$recurrence=' ('.sprintf(_('and every %s'),datefmt_format($dfmt,$res['start_datetime'])).')';
} elseif ($res['recurrent']['frequency']=='2w') {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
$postHead.=' ('.sprintf(_('e un %s ogni due'),datefmt_format($dfmt,$res['start_datetime'])).')';
// Translation: here, "every other" refers to a day of the week, like in "and every other Wednesday"
$recurrence=' ('.sprintf(_('and every other %s'),datefmt_format($dfmt,$res['start_datetime'])).')';
} elseif ($res['recurrent']['frequency']=='1m' && isset($res['recurrent']['type']) && $res['recurrent']['type']=='ordinal') {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"d");
$postHead.=' ('.sprintf(_('e il giorno %d di ogni mese'),datefmt_format($dfmt,$res['start_datetime'])).')';
$recurrence=' ('.sprintf(_('and on day %d of each month'),datefmt_format($dfmt,$res['start_datetime'])).')';
} elseif ($res['recurrent']['frequency']=='1m' && isset($res['recurrent']['type']) && array_key_exists($res['recurrent']['type'],$ordinals)) {
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
$postHead.=' ('.sprintf(_('e ogni %s %s del mese'),$ordinals[$res['recurrent']['type']],datefmt_format($dfmt,$res['start_datetime'])).')';
// Translation: this is, for example, "and every third Wednesday of the month"
$recurrence=' ('.sprintf(_('and every %s %s of the month'),$ordinals[$res['recurrent']['type']],datefmt_format($dfmt,$res['start_datetime'])).')';
} else {
fwrite(STDERR,"Warning: could not decode recurrence info in JSON from «{$url}»; wont add recurrence.\n");
}
}
}
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM");
$postHead.=mb_ucfirst(datefmt_format($dfmt,$item['start_datetime'])).$recurrence;
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"HH:mm");
// Translation: here, "from" refers to a time, like in "from 23:15"
$postHead.=', '._('from').' '.datefmt_format($dfmt,$item['start_datetime']);
if (!is_null($item['end_datetime'])) {
// Translation: here, "to" refers to a time, like in "to 23:30"; please *don't* translate "{time}"
$postHead.=' '._('to{time}').' '.datefmt_format($dfmt,$item['end_datetime']);
}
}
$item['place']['name']=trim($item['place']['name']);
if (preg_match('#^on-?line$#i',$item['place']['name'])!==1) {
// Translation: this "at" refers to a place and address, like in "at Zam pub, 14 ABC Lane, Chicago, IL"; please *don't* translate "{place}"
$postHead.=', '._('at{place}')." {$item['place']['name']}, {$item['place']['address']}";
// Translation: this refers to web urls, like in "and online too, on https://a.net, https://b.net and https://c.net"
if (is_array($item['online_locations']) && count($item['online_locations'])>0) $postHead.='; '._('and online too, on').' '.lastplode(', ',' '._('and').' ',$item['online_locations']);
} else {
// Translation: this refers to web urls, like in "online on https://a.net, https://b.net and https://c.net"
$postHead.=', '._('online on').lastplode(', ',' '._('and').' ',$item['online_locations']);
}
$postHead.=', '._('presso')." {$item['place']['name']}, {$item['place']['address']}";
if (is_array($item['online_locations']) && count($item['online_locations'])>0) $postHead.='; '._('e anche online su ').implode(' - ',$item['online_locations']);
$postBody='';
if (!is_null($item['description']) && $item['description']!='' && $item['description']!='<p></p>') $postBody.=html2text($item['description']);
if ($postBody!='') $postBody="\n\n{$postBody}";
@ -597,6 +632,31 @@ if (!$opts['test']) {
exit(0);
function getlocale() {
if (($loc=getenv('LANG'))!='') return $loc;
if (($loc=getenv('LC_MESSAGES'))!='') return $loc;
if (($loc=getenv('LC_ALL'))!='') return $loc;
return false;
}
function lastplode($sep,$lastsep,$arr) {
$c=count($arr);
if ($c==0) return '';
if ($c==1) return current($arr);
$str='';
$i=0;
foreach ($arr as $val) {
$i++;
if ($i<$c-1)
$str.="{$val}{$sep}";
elseif ($i<$c)
$str.="{$val}{$lastsep}";
else
$str.=$val;
}
return $str;
}
function cknap($napid) {
global $naps;
$now=time();