From 3294d2928b2f279ddcce64161c3918581b454451 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 24 Sep 2019 14:15:50 -0400
Subject: [PATCH] Refactor language initialization to trait.

---
 module/VuFind/src/VuFind/Bootstrapper.php     |  38 +------
 .../Translator/LanguageInitializerTrait.php   | 101 ++++++++++++++++++
 2 files changed, 105 insertions(+), 34 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/I18n/Translator/LanguageInitializerTrait.php

diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index ff13b3e683e..ce3bea3275c 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -42,6 +42,8 @@ use Zend\Router\Http\RouteMatch;
  */
 class Bootstrapper
 {
+    use \VuFind\I18n\Translator\LanguageInitializerTrait;
+
     /**
      * Main VuFind configuration
      *
@@ -293,30 +295,6 @@ class Bootstrapper
         return false;
     }
 
-    /**
-     * Support method for initLanguage() -- look up all text domains.
-     *
-     * @return array
-     */
-    protected function getTextDomains()
-    {
-        $base = APPLICATION_PATH;
-        $local = LOCAL_OVERRIDE_DIR;
-        $languagePathParts = ["$base/languages"];
-        if (!empty($local)) {
-            $languagePathParts[] = "$local/languages";
-        }
-        $languagePathParts[] = "$base/themes/*/languages";
-
-        $domains = [];
-        foreach ($languagePathParts as $current) {
-            $places = glob($current . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
-            $domains = array_merge($domains, array_map('basename', $places));
-        }
-
-        return array_unique($domains);
-    }
-
     /**
      * Set up language handling.
      *
@@ -355,16 +333,8 @@ class Bootstrapper
             }
             try {
                 $translator = $sm->get(\Zend\Mvc\I18n\Translator::class);
-                $translator->setLocale($language)
-                    ->addTranslationFile('ExtendedIni', null, 'default', $language);
-                foreach ($this->getTextDomains() as $domain) {
-                    // Set up text domains using the domain name as the filename;
-                    // this will help the ExtendedIni loader dynamically locate
-                    // the appropriate files.
-                    $translator->addTranslationFile(
-                        'ExtendedIni', $domain, $domain, $language
-                    );
-                }
+                $translator->setLocale($language);
+                $this->addLanguageToTranslator($translator, $language);
             } catch (\Zend\Mvc\I18n\Exception\BadMethodCallException $e) {
                 if (!extension_loaded('intl')) {
                     throw new \Exception(
diff --git a/module/VuFind/src/VuFind/I18n/Translator/LanguageInitializerTrait.php b/module/VuFind/src/VuFind/I18n/Translator/LanguageInitializerTrait.php
new file mode 100644
index 00000000000..75126bcaf75
--- /dev/null
+++ b/module/VuFind/src/VuFind/I18n/Translator/LanguageInitializerTrait.php
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Logic for initializing a language within a translator used by VuFind.
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2019.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * @category VuFind
+ * @package  Translator
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace VuFind\I18n\Translator;
+
+use Zend\I18n\Translator\TranslatorInterface;
+
+/**
+ * Logic for initializing a language within a translator used by VuFind.
+ *
+ * @category VuFind
+ * @package  Translator
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+trait LanguageInitializerTrait
+{
+    /**
+     * Array of flags to indicate which languages have already been initialized.
+     *
+     * @var array
+     */
+    protected $initializedI18nLanguages = [];
+
+    /**
+     * Look up all text domains.
+     *
+     * @return array
+     */
+    protected function getTextDomains()
+    {
+        $base = APPLICATION_PATH;
+        $local = LOCAL_OVERRIDE_DIR;
+        $languagePathParts = ["$base/languages"];
+        if (!empty($local)) {
+            $languagePathParts[] = "$local/languages";
+        }
+        $languagePathParts[] = "$base/themes/*/languages";
+
+        $domains = [];
+        foreach ($languagePathParts as $current) {
+            $places = glob($current . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
+            $domains = array_merge($domains, array_map('basename', $places));
+        }
+
+        return array_unique($domains);
+    }
+
+    /**
+     * Configure a translator to support the requested language.
+     *
+     * @param \Zend\Mvc\I18n\Translator $translator Translator
+     * @param string                    $language   Language to set up
+     *
+     * @return void
+     */
+    protected function addLanguageToTranslator($translator, $language)
+    {
+        // Don't double-initialize languages:
+        if (isset($this->initializedI18nLanguages[$language])) {
+            return;
+        }
+        $this->initializedI18nLanguages[$language] = true;
+
+        // If we got this far, we need to set everything up:
+        $translator->addTranslationFile('ExtendedIni', null, 'default', $language);
+        foreach ($this->getTextDomains() as $domain) {
+            // Set up text domains using the domain name as the filename;
+            // this will help the ExtendedIni loader dynamically locate
+            // the appropriate files.
+            $translator->addTranslationFile(
+                'ExtendedIni', $domain, $domain, $language
+            );
+        }
+    }
+}
-- 
GitLab