Skip to content

Commit

Permalink
Added language translation support for response
Browse files Browse the repository at this point in the history
  • Loading branch information
timoj committed May 22, 2018
1 parent c01291b commit 741d1e7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
17 changes: 14 additions & 3 deletions src/Wuunder/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
54 changes: 41 additions & 13 deletions src/Wuunder/Util/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand All @@ -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
}
}
7 changes: 7 additions & 0 deletions src/Wuunder/etc/lang/en-nl.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
monday,maandag
tuesday,dinsdag
wednesday,woensdag
thursday,donderdag
friday,vrijdag
saturday,zaterdag
sunday,zondag
1 change: 1 addition & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
include("../src/Wuunder/Connector.php");

$connector = new Wuunder\Connector("YVc7rKdM6e6Q_HQK81NCt7SM0LT0TtQB");
$connector->setLanguage("NL");exit;

$parcelshopRequest = $connector->getParcelshopById();

Expand Down

0 comments on commit 741d1e7

Please sign in to comment.