Skip to content

Commit

Permalink
WIP: locales (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennmuel authored and ubma committed Jun 10, 2020
1 parent 01101c2 commit f02a197
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 35 deletions.
13 changes: 9 additions & 4 deletions config-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
define('TEXTS', [
'a3' => 'Bibliotheksbereich A3',
'eh' => 'Bibliotheksbereich Schloss Ehrenhof',
'no' => 'keine Buchung'
'no' => _('bookings.none')
]);

define('LIMIT', [
Expand Down Expand Up @@ -76,7 +76,12 @@ function get_authorization($uid, $password) {
// Mail implementation using Swift Mailer.
// See documentation: https://swiftmailer.symfony.com/docs/introduction.html
function sendmail($uid, $email, $text) {
$text = "Diese Reservierungen sind für die Benutzerkennung $uid vorgemerkt:\n\n" . $text;
$body = sprintf(_('mail.salutation'), $uid);
$body .= "\n\n" . sprintf(_('mail.bookings'), $uid);
$body .= "\n\n" . $text;
$body .= "\n" . _('mail.link');
$body .= "\nhttps://" .$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?uid=" . $uid;
$body .= "\n\n" . _('mail.greetings');

// Sendmail for transport.
$transport = new Swift_SendmailTransport('/usr/sbin/sendmail -bs');
Expand All @@ -87,10 +92,10 @@ function sendmail($uid, $email, $text) {
$from = ['[email protected]' => 'Demo Sender'];

// Create a message
$message = (new Swift_Message('Sitzplatzreservierung'))
$message = (new Swift_Message(_('mail.subject')))
->setFrom($from)
->setTo($email)
->setBody($text)
->setBody($body)
;

// Send the message
Expand Down
96 changes: 65 additions & 31 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@
// Read configuration for database access.
require_once 'config.php';

$locale = getLang();
putenv("LANG=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain('MArs', 'locale');
bind_textdomain_codeset('MArs', 'UTF-8');
textdomain('MArs');

function getLang() {
if (isset($_REQUEST['lang'])) {
// User requested language by URL parameter.
$locale = $_REQUEST['lang'];
$_SESSION['lang'] = $locale;
} elseif (isset($_SESSION['lang'])) {
// Get language from session data.
$locale = $_SESSION['lang'];
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// Get language from browser settings.
$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
} else {
$locale = 'en';
}

switch (substr($locale, 0, 2)) {
case 'de':
$locale = 'de_DE';
break;
case 'en':
$locale = 'en_US';
break;
// more locales would go here
default:
// To Do: make default locale configurable?
$locale = 'en_US';
break;
}
return $locale . ".UTF-8";
}

function alert($text)
{
print("<script>alert('$text');</script>");
Expand Down Expand Up @@ -144,8 +182,8 @@ function update_database($uid, $date, $oldvalue, $value)
$table = DB_TABLE;
$comment = "";
$no_reservation = 'no';
$success_text = '<span class="success">Aktion erfolgreich</span>';
$failure_text = '<span class="failure">Aktion nicht erfolgreich</span>';
$success_text = '<span class="success">' . _('form.action.success') . '</span>';
$failure_text = '<span class="failure">'. _('form.action.failure') .'</span>';
if ($value == $no_reservation) {
// Delete booking.
$result = $db->query("DELETE FROM $table WHERE name='$uid' AND date='$date'");
Expand All @@ -160,11 +198,11 @@ function update_database($uid, $date, $oldvalue, $value)
$result = $db->query("SELECT COUNT(*) FROM $table WHERE date>='$today' AND name='$uid'");
$personal_bookings = $result ? $result->fetch_row()[0] : 999;
if ($count >= $limit) {
$comment = '<span class="failure">Bibliotheksbereich ausgebucht</span>';
$comment = '<span class="failure">' . _('form.failure.libraryfull') . '</span>';
} elseif ($oldvalue == $no_reservation) {
// New bookings.
if ($personal_bookings >= PERSONAL_LIMIT) {
$comment = '<span class="failure">Persönliches Buchungslimit erreicht</span>';
$comment = '<span class="failure">' . _('form.failure.personallimit') . '</span>';
} else {
$result = $db->query("INSERT INTO $table (name, text, date) VALUES ('$uid','$value','$date')");
$success = $result ? $success_text : $failure_text;
Expand All @@ -173,7 +211,7 @@ function update_database($uid, $date, $oldvalue, $value)
} else {
// Modified booking.
if ($personal_bookings > PERSONAL_LIMIT) {
$comment = '<span class="failure">Persönliches Buchungslimit erreicht</span>';
$comment = '<span class="failure">' . _('form.failure.personallimit') . '</span>';
} else {
$result = $db->query("DELETE FROM $table WHERE name='$uid' AND date='$date'");
$result = $db->query("INSERT INTO $table (name, text, date) VALUES ('$uid','$value','$date')");
Expand Down Expand Up @@ -215,7 +253,7 @@ function show_database($uid, $lastuid)
$first = $now;

print('<fieldset>');
print('<legend>Buchungen / bookings</legend>');
print('<legend>' . _('bookings.form.legend') . '</legend>');

// Get the first reserved day from the booking list.
$i = 0;
Expand Down Expand Up @@ -250,12 +288,12 @@ function show_database($uid, $lastuid)
foreach (CLOSED as $condition) {
$closed = ($weekday == $condition);
if ($closed) {
print("<div class=\"closed\">$label geschlossen / closed</div>");
print("<div class=\"closed\">$label " . _('closed') . "</div>");
break;
}
$closed = ($day == $condition);
if ($closed) {
print("<div class=\"closed\">$label geschlossen / closed</div>");
print("<div class=\"closed\">$label " . _('closed') . "</div>");
break;
}
}
Expand Down Expand Up @@ -364,7 +402,7 @@ function day_report($location = false)

if (!preg_match('/^[a-z_0-9]{0,8}$/', $uid)) {
// uid is longer than 8 characters or contains invalid characters.
alert("Ungültige Universitätskennung");
alert(_('auth.invalid.id'));
$uid = '';
}

Expand All @@ -373,9 +411,9 @@ function day_report($location = false)

?>
<!DOCTYPE html>
<html lang="en">
<html lang="<?php echo(substr($locale,0,2)) ?>">
<head>
<title>UB Sitzplatzbuchung</title>
<title><?php echo _("title")?></title>
<link rel="stylesheet" type="text/css" href="mars.css" media="all">
</head>
<body>
Expand All @@ -389,19 +427,19 @@ function day_report($location = false)
<form id="reservation" method="post">

<fieldset class="personaldata">
<legend>Benutzerdaten / personal data</legend>
<label class="uid" for="uid">Universitätskennung:*</label>
<legend><?php echo _('auth.form.legend') ?></legend>
<label class="uid" for="uid"><?php echo _('auth.form.id') ?>:*</label>
<input class="uid" id="uid" name="uid" placeholder="user id" maxlength="8"
pattern="^([a-z_0-9]{0,8})$" required="required" value="<?=$uid?>"/>
<label class="password" for="password">Passwort:*</label><input id="password" name="password" placeholder="********" required="required" type="password" value="<?=$password?>"/>
<label class="password" for="password"><?php echo _('auth.form.password') ?>:*</label><input id="password" name="password" placeholder="********" required="required" type="password" value="<?=$password?>"/>
<input id="lastuid" name="lastuid" type="hidden" value="<?=$authorized ? $uid : ''?>"/>
</fieldset>
<?php
}

if ($authorized && $task == '') {
?>
<button class="logout" type="button"><a class="logout" href=".">Abmelden / Logout</a></button>
<button class="logout" type="button"><a class="logout" href="."><?php echo _('auth.logout') ?></a></button>
<?php
}

Expand Down Expand Up @@ -434,7 +472,7 @@ function day_report($location = false)
// Show some information for the current uid.
$usertext = get_usertext();
print("<p>$usertext</p>");
print("<h3>Meine Sitzplatzbuchungen / My seat bookings</h3>");
print("<h3>" . _('bookings.mybookings') . "</h3>");
// Show all bookings.
show_database($uid, $lastuid);
if ($email != '') {
Expand All @@ -443,44 +481,40 @@ function day_report($location = false)
}
} elseif ($uid != '') {
if ($password == '') {
print('<p>Bitte ergänzen Sie Ihre Anmeldedaten um Ihr Passwort.</p>');
print('<p>' . _('auth.password.needed') . '</p>');
} else {
print('<p>Die Anmeldedaten sind ungültig. Bitte prüfen Sie Universitätskennung und Passwort.</p>');
print('<p>' . _('auth.invalid.credentials') . '</p>');
}
} else {
?>
<p>Die <a href="/datenschutzerklaerung/" target="_blank">Informationen zum Datenschutz</a> wurden mir zur Verfügung gestellt.<br/>
The <a href="/en/privacy-policy/" target="_blank">privacy information</a> was provided to me.</p>
<?php
print('<p>' . _('privacy') . '</p>');
}
//<button type="reset">Eingaben zurücksetzen</button>

if ($uid == '' || $task == '') {
if ($authorized) {
?>
<button class="submit" type="submit">Eingaben absenden</button>
<button class="submit" type="submit"><?php echo _('bookings.form.submit') ?></button>
<br/>
<input type="checkbox" name="email" id="email" value="checked" <?=$email?>/>
<label for="email">Informieren Sie mich bitte per E-Mail über meine aktuellen Sitzplatzbuchungen.
Please inform me by e-mail about my current bookings.</label>
<label for="email"><?php echo _('bookings.form.email') ?></label>
<?php
} else {
?>
<button class="submit" type="submit">Anmelden</button>
<button class="submit" type="submit"><?php echo _('auth.form.login') ?></button>
<?php
}
?>

<?php
if ($master) {
?>
<h3>Admin-Funktionen</h3>
<h3><?php echo _('admin.functions') ?></h3>
<p>
<ul>
<li><a href="./?task=dump" target="_blank">Alle Buchungen ausgeben</a>
<li><a href="./?task=day-report" target="_blank">Buchungsübersicht</a>
<li><a href="./?task=a3-report" target="_blank">Tagesplanung A3</a>
<li><a href="./?task=eh-report" target="_blank">Tagesplanung Schloss Ehrenhof</a>
<li><a href="./?task=dump" target="_blank"><?php echo _('admin.bookings.all') ?></a>
<li><a href="./?task=day-report" target="_blank"><?php echo _('admin.bookings.report.day.all') ?></a>
<li><a href="./?task=a3-report" target="_blank"><?php echo _('admin.bookings.report.day.a3') ?></a>
<li><a href="./?task=eh-report" target="_blank"><?php echo _('admin.bookings.report.day.be') ?></a>
</ul>
</p>
<?php
Expand Down
138 changes: 138 additions & 0 deletions locale/MArs.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Locale template for MArs
# Copyright (C) 2020
# This file is distributed under the same license as the MArs package.
# Stefan Weil [email protected]
# Dennis Müller [email protected]
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-10 16:31+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: config-template.php:35
msgid "bookings.none"
msgstr ""

#: config-template.php:80
msgid "mail.salutation"
msgstr ""

#: config-template.php:81
msgid "mail.bookings"
msgstr ""

#: config-template.php:83
msgid "mail.link"
msgstr ""

#: config-template.php:85
msgid "mail.greetings"
msgstr ""

#: config-template.php:96
msgid "mail.subject"
msgstr ""

#: index.php:186
msgid "form.action.success"
msgstr ""

#: index.php:187
msgid "form.action.failure"
msgstr ""

#: index.php:202
msgid "form.failure.libraryfull"
msgstr ""

#: index.php:206 index.php:215
msgid "form.failure.personallimit"
msgstr ""

#: index.php:257
msgid "bookings.form.legend"
msgstr ""

#: index.php:292 index.php:297
msgid "closed"
msgstr ""

#: index.php:406
msgid "auth.invalid.id"
msgstr ""

#: index.php:417
msgid "title"
msgstr ""

#: index.php:431
msgid "auth.form.legend"
msgstr ""

#: index.php:432
msgid "auth.form.id"
msgstr ""

#: index.php:435
msgid "auth.form.password"
msgstr ""

#: index.php:443
msgid "auth.logout"
msgstr ""

#: index.php:476
msgid "bookings.mybookings"
msgstr ""

#: index.php:485
msgid "auth.password.needed"
msgstr ""

#: index.php:487
msgid "auth.invalid.credentials"
msgstr ""

#: index.php:490
msgid "privacy"
msgstr ""

#: index.php:497
msgid "bookings.form.submit"
msgstr ""

#: index.php:500
msgid "bookings.form.email"
msgstr ""

#: index.php:504
msgid "auth.form.login"
msgstr ""

#: index.php:512
msgid "admin.functions"
msgstr ""

#: index.php:515
msgid "admin.bookings.all"
msgstr ""

#: index.php:516
msgid "admin.bookings.report.day.all"
msgstr ""

#: index.php:517
msgid "admin.bookings.report.day.a3"
msgstr ""

#: index.php:518
msgid "admin.bookings.report.day.be"
msgstr ""
Binary file added locale/de_DE.UTF-8/LC_MESSAGES/MArs.mo
Binary file not shown.
Loading

0 comments on commit f02a197

Please sign in to comment.