Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Nov 29, 2019
2 parents 7020c75 + a7e351b commit d5fcb34
Show file tree
Hide file tree
Showing 39 changed files with 1,002 additions and 596 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ FileETag None
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy no-referrer
Header set Content-Security-Policy "default-src 'self'; object-src 'none'; script-src 'none'; img-src http:"
Header set Content-Security-Policy "default-src 'self'; object-src 'none'; script-src 'none'; style-src 'self' 'unsafe-inline'; img-src http:"
</ifmodule>
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module.exports = function (grunt) {
'i18n/es_ES/LC_MESSAGES/Alltube.mo': 'i18n/es_ES/LC_MESSAGES/Alltube.po',
'i18n/de_DE/LC_MESSAGES/Alltube.mo': 'i18n/de_DE/LC_MESSAGES/Alltube.po',
'i18n/pt_BR/LC_MESSAGES/Alltube.mo': 'i18n/pt_BR/LC_MESSAGES/Alltube.po',
'i18n/ar_001/LC_MESSAGES/Alltube.mo': 'i18n/ar_001/LC_MESSAGES/Alltube.po'
'i18n/ar/LC_MESSAGES/Alltube.mo': 'i18n/ar/LC_MESSAGES/Alltube.po'
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"url": "https://github.com/piotras/heroku-buildpack-gettext.git"
},
{
"url": "heroku/nodejs"
"url": "heroku/php"
},
{
"url": "heroku/python"
"url": "heroku/nodejs"
},
{
"url": "heroku/php"
"url": "heroku/python"
}
],
"env": {
Expand Down
19 changes: 14 additions & 5 deletions classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Exception;
use Symfony\Component\Yaml\Yaml;
use Jawira\CaseConverter\Convert;

/**
* Manage config parameters.
Expand Down Expand Up @@ -134,6 +135,13 @@ class Config
*/
public $genericFormats = [];

/**
* Enable debug mode.
*
* @var bool
*/
public $debug = false;

/**
* Config constructor.
*
Expand Down Expand Up @@ -222,17 +230,18 @@ private function applyOptions(array $options)

/**
* Override options from environement variables.
* Supported environment variables: CONVERT, PYTHON, AUDIO_BITRATE.
* Environment variables should use screaming snake case: CONVERT, PYTHON, AUDIO_BITRATE, etc.
* If the value is an array, you should use the YAML format: "CONVERT_ADVANCED_FORMATS='[foo, bar]'"
*
* @return void
*/
private function getEnv()
{
foreach (['CONVERT', 'PYTHON', 'AUDIO_BITRATE', 'STREAM'] as $var) {
$env = getenv($var);
foreach (get_object_vars($this) as $prop => $value) {
$convert = new Convert($prop);
$env = getenv($convert->toSnake(true));
if ($env) {
$prop = lcfirst(str_replace('_', '', ucwords(strtolower($var), '_')));
$this->$prop = $env;
$this->$prop = Yaml::parse($env);
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions classes/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function __construct($locale)
{
$parse = AcceptLanguage::parse($locale);
$this->language = $parse[1]['language'];
$this->region = $parse[1]['region'];
if (!empty($parse[1]['region'])) {
$this->region = $parse[1]['region'];
}
}

/**
Expand Down Expand Up @@ -68,7 +70,11 @@ public function getFullName()
*/
public function getIso15897()
{
return $this->language . '_' . $this->region;
if (isset($this->region)) {
return $this->language . '_' . $this->region;
} else {
return $this->language;
}
}

/**
Expand All @@ -78,7 +84,11 @@ public function getIso15897()
*/
public function getBcp47()
{
return $this->language . '-' . $this->region;
if (isset($this->region)) {
return $this->language . '-' . $this->region;
} else {
return $this->language;
}
}

/**
Expand All @@ -98,6 +108,8 @@ public function getIso3166()
*/
public function getCountry()
{
return country($this->getIso3166());
if (isset($this->region)) {
return country($this->getIso3166());
}
}
}
108 changes: 93 additions & 15 deletions classes/LocaleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Aura\Session\Segment;
use Symfony\Component\Process\Process;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\MoFileLoader;

/**
* Class used to manage locales.
Expand All @@ -19,7 +21,7 @@ class LocaleManager
*
* @var array
*/
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN', 'es_ES', 'pt_BR', 'de_DE', 'ar_001'];
private $supportedLocales = ['en_US', 'fr_FR', 'zh_CN', 'es_ES', 'pt_BR', 'de_DE', 'ar'];

/**
* Current locale.
Expand All @@ -35,19 +37,49 @@ class LocaleManager
*/
private $sessionSegment;

/**
* Default locale.
*
* @var string
*/
private const DEFAULT_LOCALE = 'en';

/**
* Symfony Translator instance.
*
* @var Translator
*/
private $translator;

/**
* Singleton instance.
*
* @var LocaleManager|null
*/
private static $instance;

/**
* LocaleManager constructor.
*/
public function __construct()
private function __construct()
{
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale');

$this->translator = new Translator(self::DEFAULT_LOCALE);
if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale));
}
bindtextdomain('Alltube', __DIR__ . '/../i18n/');
textdomain('Alltube');

$this->translator->addLoader('gettext', new MoFileLoader());
foreach ($this->getSupportedLocales() as $locale) {
$this->translator->addResource(
'gettext',
__DIR__ . '/../i18n/' . $locale->getIso15897() . '/LC_MESSAGES/Alltube.mo',
$locale->getIso15897()
);
}
}

/**
Expand All @@ -58,16 +90,9 @@ public function __construct()
public function getSupportedLocales()
{
$return = [];
$process = new Process(['locale', '-a']);
$process->run();
$installedLocales = explode(PHP_EOL, trim($process->getOutput()));

foreach ($this->supportedLocales as $supportedLocale) {
if (
in_array($supportedLocale, $installedLocales)
|| in_array($supportedLocale . '.utf8', $installedLocales)
) {
$return[] = new Locale($supportedLocale);
}
$return[] = new Locale($supportedLocale);
}

return $return;
Expand All @@ -90,8 +115,7 @@ public function getLocale()
*/
public function setLocale(Locale $locale)
{
putenv('LANG=' . $locale);
setlocale(LC_ALL, [$locale . '.utf8', $locale]);
$this->translator->setLocale($locale->getIso15897());
$this->curLocale = $locale;
$this->sessionSegment->set('locale', $locale);
}
Expand All @@ -101,7 +125,61 @@ public function setLocale(Locale $locale)
*/
public function unsetLocale()
{
$this->translator->setLocale(self::DEFAULT_LOCALE);
$this->curLocale = null;
$this->sessionSegment->clear();
}

/**
* Smarty "t" block.
*
* @param array $params Block parameters
* @param string $text Block content
*
* @return string Translated string
*/
public function smartyTranslate(array $params, $text)
{
if (isset($params['params'])) {
return $this->t($text, $params['params']);
} else {
return $this->t($text);
}
}

/**
* Translate a string.
*
* @param string $string String to translate
*
* @return string Translated string
*/
public function t($string, array $params = [])
{
return $this->translator->trans($string, $params);
}

/**
* Get LocaleManager singleton instance.
*
* @return LocaleManager
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Destroy singleton instance.
*
* @return void
*/
public static function destroyInstance()
{
self::$instance = null;
}
}
Loading

0 comments on commit d5fcb34

Please sign in to comment.