From 427315195bdd379a0207fc9b2aaf69a5b5b86c79 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 28 Aug 2018 22:23:59 +0200 Subject: [PATCH] Moved translation/internationalization to Helpers\Translator class --- config/app.php | 2 + includes/engelsystem.php | 6 - includes/helper/email_helper.php | 8 +- .../helper/internationalization_helper.php | 80 ------------- includes/includes.php | 1 - includes/sys_form.php | 5 +- includes/sys_menu.php | 26 ++++- src/Helpers/TranslationServiceProvider.php | 58 ++++++++++ src/Helpers/Translator.php | 105 ++++++++++++++++++ src/Middleware/SetLocale.php | 49 ++++++++ src/helpers.php | 35 ++++++ .../TranslationServiceProviderTest.php | 86 ++++++++++++++ tests/Unit/Helpers/TranslatorTest.php | 69 ++++++++++++ tests/Unit/HelpersTest.php | 26 ++++- tests/Unit/Middleware/SetLocaleTest.php | 71 ++++++++++++ 15 files changed, 534 insertions(+), 93 deletions(-) delete mode 100644 includes/helper/internationalization_helper.php create mode 100644 src/Helpers/TranslationServiceProvider.php create mode 100644 src/Helpers/Translator.php create mode 100644 src/Middleware/SetLocale.php create mode 100644 tests/Unit/Helpers/TranslationServiceProviderTest.php create mode 100644 tests/Unit/Helpers/TranslatorTest.php create mode 100644 tests/Unit/Middleware/SetLocaleTest.php diff --git a/config/app.php b/config/app.php index 13d4d22b..ea394b8e 100644 --- a/config/app.php +++ b/config/app.php @@ -14,6 +14,7 @@ return [ \Engelsystem\Database\DatabaseServiceProvider::class, \Engelsystem\Http\RequestServiceProvider::class, \Engelsystem\Http\SessionServiceProvider::class, + \Engelsystem\Helpers\TranslationServiceProvider::class, \Engelsystem\Http\ResponseServiceProvider::class, \Engelsystem\Http\Psr7ServiceProvider::class, \Engelsystem\Middleware\RouteDispatcherServiceProvider::class, @@ -24,6 +25,7 @@ return [ 'middleware' => [ \Engelsystem\Middleware\SendResponseHandler::class, \Engelsystem\Middleware\ExceptionHandler::class, + \Engelsystem\Middleware\SetLocale::class, \Engelsystem\Middleware\RouteDispatcher::class, \Engelsystem\Middleware\RequestHandler::class, ], diff --git a/includes/engelsystem.php b/includes/engelsystem.php index eb5b220a..4c096b43 100644 --- a/includes/engelsystem.php +++ b/includes/engelsystem.php @@ -21,12 +21,6 @@ if ($app->get('config')->get('maintenance')) { } -/** - * Init translations - */ -gettext_init(); - - /** * Init authorization */ diff --git a/includes/helper/email_helper.php b/includes/helper/email_helper.php index c223d026..8d1cb794 100644 --- a/includes/helper/email_helper.php +++ b/includes/helper/email_helper.php @@ -15,14 +15,16 @@ function engelsystem_email_to_user($recipient_user, $title, $message, $not_if_it return true; } - gettext_locale($recipient_user['Sprache']); + /** @var \Engelsystem\Helpers\Translator $translator */ + $translator = app()->get('translator'); + $locale = $translator->getLocale(); + $translator->setLocale($recipient_user['Sprache']); $message = sprintf(_('Hi %s,'), $recipient_user['Nick']) . "\n\n" . _('here is a message for you from the engelsystem:') . "\n\n" . $message . "\n\n" . _('This email is autogenerated and has not been signed. You got this email because you are registered in the engelsystem.'); - - gettext_locale(); + $translator->setLocale($locale); return engelsystem_email($recipient_user['email'], $title, $message); } diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php deleted file mode 100644 index bb6d0abd..00000000 --- a/includes/helper/internationalization_helper.php +++ /dev/null @@ -1,80 +0,0 @@ -get('locale'); -} - -/** - * Returns two letter language code from currently active locale - * - * @return string - */ -function locale_short() -{ - return substr(locale(), 0, 2); -} - -/** - * Initializes gettext for internationalization and updates the sessions locale to use for translation. - */ -function gettext_init() -{ - $locales = config('locales'); - $request = request(); - $session = session(); - - if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) { - $session->set('locale', $request->input('set_locale')); - } elseif (!$session->has('locale')) { - $session->set('locale', config('default_locale')); - } - - gettext_locale(); - bindtextdomain('default', app('path.lang')); - bind_textdomain_codeset('default', 'UTF-8'); - textdomain('default'); -} - -/** - * Swich gettext locale. - * - * @param string $locale - */ -function gettext_locale($locale = null) -{ - if (empty($locale)) { - $locale = session()->get('locale'); - } - - putenv('LC_ALL=' . $locale); - setlocale(LC_ALL, $locale); -} - -/** - * Renders language selection. - * - * @return array - */ -function make_langselect() -{ - $request = app('request'); - - $items = []; - foreach (config('locales') as $locale => $name) { - $url = url($request->getPathInfo(), ['set_locale' => $locale]); - - $items[] = toolbar_item_link( - htmlspecialchars($url), - '', - $name, - $locale == locale() - ); - } - return $items; -} diff --git a/includes/includes.php b/includes/includes.php index e316e550..7a68c3c9 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -61,7 +61,6 @@ $includeFiles = [ __DIR__ . '/../includes/controller/user_worklog_controller.php', __DIR__ . '/../includes/helper/graph_helper.php', - __DIR__ . '/../includes/helper/internationalization_helper.php', __DIR__ . '/../includes/helper/message_helper.php', __DIR__ . '/../includes/helper/error_helper.php', __DIR__ . '/../includes/helper/email_helper.php', diff --git a/includes/sys_form.php b/includes/sys_form.php index 05df4c15..9cb6f38d 100644 --- a/includes/sys_form.php +++ b/includes/sys_form.php @@ -66,6 +66,9 @@ function form_date($name, $label, $value, $start_date = '', $end_date = '') $value = is_numeric($value) ? date('Y-m-d', $value) : ''; $start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : ''; $end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : ''; + $locale = $locale = session()->get('locale'); + $shortLocale = substr($locale, 0, 2); + return form_element($label, '
' @@ -74,7 +77,7 @@ function form_date($name, $label, $value, $start_date = '', $end_date = '')