From 741d1e7b9d9d22df6d9133a7cc1102c4d3bd1beb Mon Sep 17 00:00:00 2001 From: Timo Janssrn Date: Tue, 22 May 2018 13:21:54 +0200 Subject: [PATCH] Added language translation support for response --- src/Wuunder/Connector.php | 17 +++++++++-- src/Wuunder/Util/Helper.php | 54 ++++++++++++++++++++++++++-------- src/Wuunder/etc/lang/en-nl.csv | 7 +++++ tests/test.php | 1 + 4 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 src/Wuunder/etc/lang/en-nl.csv diff --git a/src/Wuunder/Connector.php b/src/Wuunder/Connector.php index 143a56f..3a816c5 100644 --- a/src/Wuunder/Connector.php +++ b/src/Wuunder/Connector.php @@ -16,11 +16,13 @@ class Connector private $apiKey; private $apiEnvironment; + private $helper; public function __construct($apiKey, $isStaging = true) { $this->apiKey = new Key($apiKey); $this->apiEnvironment = new Environment($isStaging ? "staging" : "production"); + $this->helper = Helper::getInstance(); } /** @@ -59,14 +61,23 @@ public function getParcelshopById() { * */ public function setLogger($loggerClass, $loggerFunc) { - $helper = Helper::getInstance(); + if(empty($loggerClass)) { - $helper->setLogger($loggerFunc); + $this->helper->setLogger($loggerFunc); } else { - $helper->setLogger(array($loggerClass, $loggerFunc)); + $this->helper->setLogger(array($loggerClass, $loggerFunc)); } } + /** + * Sets user language, for translations + * + * @param $lang + */ + public function setLanguage($lang) { + $this->helper->setTranslationLang($lang); + } + /** * Logs the input parameter * diff --git a/src/Wuunder/Util/Helper.php b/src/Wuunder/Util/Helper.php index a3e3c6a..3693b33 100644 --- a/src/Wuunder/Util/Helper.php +++ b/src/Wuunder/Util/Helper.php @@ -6,12 +6,13 @@ class Helper { private $logger; + private $translationData; /** - * Creates a new instance of the Helper - * - * @return inst - */ + * Creates a new instance of the Helper + * + * @return Helper + */ public static function getInstance() { static $inst = null; @@ -22,23 +23,23 @@ public static function getInstance() } /** - * Sets the native logger - * - * @param $logger Possible array with first element class reference and second element function name. - */ + * Sets the native logger + * + * @param $logger callback, possible array with first element class reference and second element function name. + */ public function setLogger($logger) { $this->logger = $logger; } /** - * Calls the log function. - * - * @param $logText - */ + * Calls the log function. + * + * @param $logText + */ public function log($logText) { - if(isset($this->logger)) { + if (isset($this->logger)) { call_user_func_array($this->logger, array($logText)); } } @@ -47,4 +48,31 @@ private function __construct() { } + + /** + * Sets data used for translations + * + * @param $lang + */ + public function setTranslationLang($lang) + { + $translationData = array(); + $file = realpath(dirname(dirname(__FILE__)) . "/etc/lang/en-" . strtolower($lang) . ".csv"); + if (file_exists($file)) { + if (($handle = fopen($file, "r")) !== FALSE) { + while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { + if (count($data) == 2) { + $translationData[$data[0]] = $data[1]; + } + } + fclose($handle); + } + + } + } + + public function translate($val) + { +// if + } } diff --git a/src/Wuunder/etc/lang/en-nl.csv b/src/Wuunder/etc/lang/en-nl.csv new file mode 100644 index 0000000..b943e6e --- /dev/null +++ b/src/Wuunder/etc/lang/en-nl.csv @@ -0,0 +1,7 @@ +monday,maandag +tuesday,dinsdag +wednesday,woensdag +thursday,donderdag +friday,vrijdag +saturday,zaterdag +sunday,zondag \ No newline at end of file diff --git a/tests/test.php b/tests/test.php index e2212f2..815d1b5 100644 --- a/tests/test.php +++ b/tests/test.php @@ -16,6 +16,7 @@ include("../src/Wuunder/Connector.php"); $connector = new Wuunder\Connector("YVc7rKdM6e6Q_HQK81NCt7SM0LT0TtQB"); +$connector->setLanguage("NL");exit; $parcelshopRequest = $connector->getParcelshopById();