From cac7c090975350e3b7cf4f818f4d5384ea8e138d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 11 Feb 2015 09:58:51 -0500
Subject: [PATCH] Made translation logic smarter about missing intl extension.

---
 module/VuFind/src/VuFind/Bootstrapper.php     | 16 ++++++++++----
 module/VuFind/src/VuFind/Service/Factory.php  | 10 ++++++++-
 .../Helper/Root/DisplayLanguageOption.php     | 21 +++++++++++++------
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index d5b6f4e78d0..f43f8fe6fb3 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -322,10 +322,18 @@ class Bootstrapper
             }
 
             $sm = $event->getApplication()->getServiceManager();
-            $sm->get('VuFind\Translator')
-                ->addTranslationFile('ExtendedIni', null, 'default', $language)
-                ->setLocale($language);
-
+            try {
+                $sm->get('VuFind\Translator')
+                    ->addTranslationFile('ExtendedIni', null, 'default', $language)
+                    ->setLocale($language);
+            } catch (\Zend\Mvc\Exception\BadMethodCallException $e) {
+                if (!extension_loaded('intl')) {
+                    throw new \Exception(
+                        'Translation broken due to missing PHP intl extension.'
+                        . ' Please disable translation or install the extension.'
+                    );
+                }
+            }
             // Send key values to view:
             $viewModel = $sm->get('viewmanager')->getViewModel();
             $viewModel->setVariable('userLang', $language);
diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php
index 1982865785b..72ca05e285e 100644
--- a/module/VuFind/src/VuFind/Service/Factory.php
+++ b/module/VuFind/src/VuFind/Service/Factory.php
@@ -739,7 +739,15 @@ class Factory
         $fallbackLocales = $config->Site->language == 'en'
             ? 'en'
             : array($config->Site->language, 'en');
-        $translator->getPluginManager()->setService(
+        try {
+            $pm = $translator->getPluginManager();
+        } catch (\Zend\Mvc\Exception\BadMethodCallException $ex) {
+            // If getPluginManager is missing, this means that the user has
+            // disabled translation in module.config.php or PHP's intl extension
+            // is missing. We can do no further configuration of the object.
+            return $translator;
+        }
+        $pm->setService(
             'extendedini',
             new \VuFind\I18n\Translator\Loader\ExtendedIni(
                 $pathStack, $fallbackLocales
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
index b552f55c062..adea7d7d75f 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/DisplayLanguageOption.php
@@ -56,12 +56,21 @@ class DisplayLanguageOption extends \Zend\View\Helper\AbstractHelper
         // Clone the translator; we need to switch language for the purposes
         // of this plugin, but we don't want that change to happen globally.
         $this->translator = clone($translator);
-        $this->translator->addTranslationFile(
-            'ExtendedIni',
-            APPLICATION_PATH  . '/languages/native.ini',
-            'default', 'native'
-        );
-        $this->translator->setLocale('native');
+        try {
+            $this->translator->addTranslationFile(
+                'ExtendedIni',
+                APPLICATION_PATH  . '/languages/native.ini',
+                'default', 'native'
+            );
+            $this->translator->setLocale('native');
+        } catch (\Zend\Mvc\Exception\BadMethodCallException $e) {
+            if (!extension_loaded('intl')) {
+                throw new \Exception(
+                    'Translation broken due to missing PHP intl extension.'
+                    . ' Please disable translation or install the extension.'
+                );
+            }
+        }
     }
 
     /**
-- 
GitLab