Add complete translation of PHP code and more forecast data (#73)

* Add translations for Weather items

* Fix errors

* Revert function to static

Signed-off-by: e-alfred

* Fix errors and code formatting

* Re-add container

* Fix formatting

* Fix formatting

* Increase number of forecasts to maximum and add weekdays
master
e-alfred 2019-05-19 10:30:28 +02:00 committed by Loïc Blot
parent 71f6a7acd5
commit 20f9afdcb9
3 changed files with 82 additions and 21 deletions

View File

@ -33,13 +33,18 @@ class Application extends App {
* Core * Core
*/ */
$container->registerService('UserId', function(IContainer $c) { $container->registerService('UserId', function(IContainer $c) {
return \OC::$server->getUserSession()->getUser()->getUID(); $user = $c->getServer()->getUserSession()->getUser();
}); return $user ? $user->getUID() : null;
});
$container->registerService('Config', function($c) { $container->registerService('Config', function($c) {
return $c->query('ServerContainer')->getConfig(); return $c->query('ServerContainer')->getConfig();
}); });
$container->registerService('L10N', function($c) {
return $c->query('ServerContainer')->getL10N($c->query('AppName'));
});
/** /**
* Database Layer * Database Layer
*/ */
@ -83,7 +88,8 @@ class Application extends App {
$c->query('Request'), $c->query('Request'),
$c->query('UserId'), $c->query('UserId'),
$c->query('CityMapper'), $c->query('CityMapper'),
$c->query('SettingsMapper') $c->query('SettingsMapper'),
$c->query('L10N')
); );
}); });
} }

View File

@ -17,6 +17,7 @@ use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Controller; use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http; use \OCP\AppFramework\Http;
use \OCP\IL10N;
use \OCA\Weather\Db\CityMapper; use \OCA\Weather\Db\CityMapper;
use \OCA\Weather\Db\SettingsMapper; use \OCA\Weather\Db\SettingsMapper;
@ -30,16 +31,18 @@ class WeatherController extends IntermediateController {
private $settingsMapper; private $settingsMapper;
private $metric; private $metric;
private $config; private $config;
private $trans;
private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q="; private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q=";
private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q="; private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q=";
public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper, IL10N $trans) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->userId = $userId; $this->userId = $userId;
$this->mapper = $mapper; $this->mapper = $mapper;
$this->settingsMapper = $settingsMapper; $this->settingsMapper = $settingsMapper;
$this->metric = $settingsMapper->getMetric($this->userId); $this->metric = $settingsMapper->getMetric($this->userId);
$this->config = $config; $this->config = $config;
$this->trans = $trans;
} }
/** /**
@ -54,25 +57,51 @@ class WeatherController extends IntermediateController {
return new JSONResponse($cityInfos); return new JSONResponse($cityInfos);
} }
public function getLanguageCode() {
return $this->trans->getLanguageCode();
}
private function getCityInformations ($name) { private function getCityInformations ($name) {
$apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key'); $apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key');
$name = preg_replace("[ ]",'%20',$name); $name = preg_replace("[ ]",'%20',$name);
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric);
$openWeatherMapLang = array("ar", "bg", "ca", "cz", "de", "el", "en", "fa", "fi", "fr", "gl", "hr", "hu", "it", "ja", "kr", "la", "lt", "mk", "nl", "pl", "pt", "ro", "ru", "se", "sk", "sl", "es", "tr", "ua", "vi");
$currentLang = \OC::$server->getL10N('core')->getLanguageCode();
if (preg_match("/_/i", $currentLang)) {
$currentLang = strstr($currentLang, '_', true);
}
if (in_array($currentLang, $openWeatherMapLang)) {
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric."&lang=".$currentLang);
}
else {
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric);
}
if ($reqContent[0] != Http::STATUS_OK) { if ($reqContent[0] != Http::STATUS_OK) {
$this->errorCode = $reqContent[0]; $this->errorCode = $reqContent[0];
return null; return null;
} }
$cityDatas = json_decode($reqContent[1], true); $cityDatas = json_decode($reqContent[1], true);
$cityDatas["forecast"] = array(); $cityDatas["forecast"] = array();
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric), true);
if (in_array($currentLang, $openWeatherMapLang)) {
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric."&lang=".$currentLang), true);
}
else {
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric), true);
}
if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) { if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) {
// Show only 8 values max // Show only 8 values max
// @TODO: setting ? // @TODO: setting ?
$maxFC = $forecast['cnt'] > 8 ? 8 : $forecast['cnt']; $maxFC = $forecast['cnt'] > 40 ? 40 : $forecast['cnt'];
for ($i = 0; $i < $maxFC; $i++) { for ($i = 0; $i < $maxFC; $i++) {
$cityDatas['forecast'][] = array( $cityDatas['forecast'][] = array(
'hour' => $forecast['list'][$i]['dt'], 'date' => $this->UnixTimeToString($forecast['list'][$i]['dt']),
'weather' => $forecast['list'][$i]['weather'][0]['description'], 'weather' => $forecast['list'][$i]['weather'][0]['description'],
'temperature' => $forecast['list'][$i]['main']['temp'], 'temperature' => $forecast['list'][$i]['main']['temp'],
'pressure' => $forecast['list'][$i]['main']['pressure'], 'pressure' => $forecast['list'][$i]['main']['pressure'],
@ -84,36 +113,62 @@ class WeatherController extends IntermediateController {
} }
} }
return $cityDatas; return $cityDatas;
} }
private static function windDegToString($deg) { private function windDegToString($deg) {
if ($deg > 0 && $deg < 23 || if ($deg > 0 && $deg < 23 ||
$deg > 333) { $deg > 333) {
return "North"; return $this->trans->t('North');
} }
else if ($deg > 22 && $deg < 67) { else if ($deg > 22 && $deg < 67) {
return "North-East"; return $this->trans->t('North-East');
} }
else if ($deg > 66 && $deg < 113) { else if ($deg > 66 && $deg < 113) {
return "East"; return $this->trans->t('East');
} }
else if ($deg > 112 && $deg < 157) { else if ($deg > 112 && $deg < 157) {
return "South-East"; return $this->trans->t('South-East');
} }
else if ($deg > 156 && $deg < 201) { else if ($deg > 156 && $deg < 201) {
return "South"; return $this->trans->t('South');
} }
else if ($deg > 200 && $deg < 245) { else if ($deg > 200 && $deg < 245) {
return "South-West"; return $this->trans->t('South-West');
} }
else if ($deg > 244 && $deg < 289) { else if ($deg > 244 && $deg < 289) {
return "West"; return $this->trans->t('West');
} }
else if ($deg > 288 && $deg < 334) { else if ($deg > 288 && $deg < 334) {
return "North-West"; return $this->trans->t('North-West');
}
}
private function UnixTimeToString($unixtime) {
if (date("l", $unixtime) == "Monday") {
return $this->trans->t('Monday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Tuesday") {
return $this->trans->t('Tuesday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Wednesday") {
return $this->trans->t('Wednesday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Thursday") {
return $this->trans->t('Thursday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Friday") {
return $this->trans->t('Friday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Saturday") {
return $this->trans->t('Saturday') . " " . date("H:i",$unixtime);
}
else if (date("l", $unixtime) == "Sunday") {
return $this->trans->t('Sunday') . " " . date("H:i",$unixtime);
} }
} }
}; };
?> ?>

View File

@ -63,14 +63,14 @@
<div id="city-forecast-panel"> <div id="city-forecast-panel">
<table> <table>
<tr> <tr>
<th><?php p($l->t('Hour')); ?></th> <th><?php p($l->t('Date')); ?></th>
<th><?php p($l->t('Temperature')); ?></th> <th><?php p($l->t('Temperature')); ?></th>
<th><?php p($l->t('Weather')); ?></th> <th><?php p($l->t('Weather')); ?></th>
<th><?php p($l->t('Pressure')); ?></th> <th><?php p($l->t('Pressure')); ?></th>
<th><?php p($l->t('Wind')); ?></th> <th><?php p($l->t('Wind')); ?></th>
</tr> </tr>
<tr ng-repeat="forecast in currentCity.forecast"> <tr ng-repeat="forecast in currentCity.forecast">
<td>{{ forecast.hour * 1000 | date:'HH:mm'}}</td> <td>{{ forecast.date }}</td>
<td>{{ forecast.temperature }}{{ metricRepresentation }}</td> <td>{{ forecast.temperature }}{{ metricRepresentation }}</td>
<td>{{ forecast.weather }}</td> <td>{{ forecast.weather }}</td>
<td>{{ forecast.pressure }}</td> <td>{{ forecast.pressure }}</td>