From 8b69f29ccce4f236a8954a80c837bcd20987ca53 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Fri, 18 Nov 2016 05:57:28 +0100 Subject: [PATCH] add some base work for KaiRo bug 396 - adding L10n to the auth system --- app/authsystem.inc.php | 10 +++++++--- app/authutils.php-class | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/authsystem.inc.php b/app/authsystem.inc.php index 6e30200..372c4db 100644 --- a/app/authsystem.inc.php +++ b/app/authsystem.inc.php @@ -26,14 +26,18 @@ require_once('../kairo-utils/email.php-class'); // Class for sending emails require_once(__DIR__.'/authutils.php-class'); -bindtextdomain('kairo_auth', 'en'); // XXX: Should negotiate locale. -bind_textdomain_codeset('kairo_auth', 'utf-8'); - // Connect to our MySQL DB $db = new PDO($dbdata['dsn'], $dbdata['username'], $dbdata['password']); // Instantiate auth utils. $utils = new AuthUtils($settings, $db); +$textdomain = 'kairo_auth'; +$textlocale = $utils->negotiateLocale(array('en', 'de')); +putenv('LC_ALL='.$textlocale); +bindtextdomain($textdomain, '../locale'); +bind_textdomain_codeset($textdomain, 'utf-8'); +textdomain($textdomain); + /* Creating the DB tables: CREATE TABLE `auth_sessions` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , diff --git a/app/authutils.php-class b/app/authutils.php-class index 44152a4..f76ac18 100755 --- a/app/authutils.php-class +++ b/app/authutils.php-class @@ -82,6 +82,9 @@ class AuthUtils { // function pwdNeedsRehash($user) // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed. // + // function negotiateLocale($supportedLanguages) + // Return the language to use out of the given array of supported locales, via netotiation based on the HTTP Accept-Language header. + // // function getGroupedEmails($group_id, [$exclude_email]) // Return all emails grouped in the specified group ID, optionally exclude a specific email (e.g. because you only want non-current entries) // @@ -353,6 +356,30 @@ class AuthUtils { } } + function negotiateLocale($supportedLanguages) { + $nlocale = $supportedLanguages[0]; + $headers = getAllHeaders(); + $accLcomp = explode(',', $headers['Accept-Language']); + $accLang = array(); + foreach ($accLcomp as $lcomp) { + if (strlen($lcomp)) { + $ldef = explode(';', $lcomp); + $accLang[$ldef[0]] = (float)((strpos(@$ldef[1],'q=')===0)?substr($ldef[1],2):1); + } + } + if (count($accLang)) { + $pLang = ''; $pLang_q = 0; + foreach ($supportedLanguages as $wantedLang) { + if (isset($accLang[$wantedLang]) && ($accLang[$wantedLang] > $pLang_q)) { + $pLang = $wantedLang; + $pLang_q = $accLang[$wantedLang]; + } + } + if (strlen($pLang)) { $nlocale = $pLang; } + } + return $nlocale; + } + function getGroupedEmails($group_id, $exclude_email = '') { $emails = array(); if (intval($group_id)) { -- 2.35.3