PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,72 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Translation\Loader;
/**
* Loads a pseudo-language for developers where translation are equal to translation ids.
*/
class DevelopmentLoader implements LoaderInterface
{
const LANGUAGE_ID = 'dev';
/**
* Decorated loader.
*
* @var LoaderInterface
*/
private $loader;
/**
* @var string
*/
private $fallbackLanguage = 'en';
/**
* @param LoaderInterface $loader Decorate another loader to add the pseudo-language.
*/
public function __construct(LoaderInterface $loader)
{
$this->loader = $loader;
}
/**
* {@inheritdoc}
*/
public function load($language, array $directories)
{
if ($language !== self::LANGUAGE_ID) {
return $this->loader->load($language, $directories);
}
return $this->getDevelopmentTranslations($directories);
}
private function getDevelopmentTranslations(array $directories)
{
$fallbackTranslations = $this->loader->load($this->fallbackLanguage, $directories);
$translations = array();
foreach ($fallbackTranslations as $section => $sectionFallbackTranslations) {
$translationIds = array_keys($sectionFallbackTranslations);
$sectionTranslations = $this->prefixTranslationsWithSection($section, $translationIds);
$translations[$section] = array_combine($translationIds, $sectionTranslations);
}
return $translations;
}
private function prefixTranslationsWithSection($section, $translationIds)
{
return array_map(function ($translation) use ($section) {
return $section . '_' . $translation;
}, $translationIds);
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Translation\Loader;
use Piwik\Common;
/**
* Loads translations from JSON files.
*/
class JsonFileLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load($language, array $directories)
{
if (empty($language)) {
return array();
}
$translations = array();
foreach ($directories as $directory) {
$filename = $directory . '/' . $language . '.json';
if (! file_exists($filename)) {
continue;
}
$translations = array_replace_recursive(
$translations,
$this->loadFile($filename)
);
}
return $translations;
}
private function loadFile($filename)
{
$data = file_get_contents($filename);
$translations = json_decode($data, true);
if (is_null($translations) && Common::hasJsonErrorOccurred()) {
throw new \Exception(sprintf(
'Not able to load translation file %s: %s',
$filename,
Common::getLastJsonError()
));
}
if (!is_array($translations)) {
return array();
}
return $translations;
}
}

View File

@ -0,0 +1,65 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Translation\Loader;
use Piwik\Cache;
/**
* Caches the translations loaded by another loader.
*/
class LoaderCache implements LoaderInterface
{
/**
* @var LoaderInterface
*/
private $loader;
/**
* @var Cache\Lazy
*/
private $cache;
public function __construct(LoaderInterface $loader, Cache\Lazy $cache)
{
$this->loader = $loader;
$this->cache = $cache;
}
/**
* {@inheritdoc}
*/
public function load($language, array $directories)
{
if (empty($language)) {
return array();
}
$cacheKey = $this->getCacheKey($language, $directories);
$translations = $this->cache->fetch($cacheKey);
if (empty($translations) || !is_array($translations)) {
$translations = $this->loader->load($language, $directories);
$this->cache->save($cacheKey, $translations, 43200); // ttl=12hours
}
return $translations;
}
private function getCacheKey($language, array $directories)
{
$cacheKey = 'Translations-' . $language . '-';
// in case loaded plugins change (ie Tests vs Tracker vs UI etc)
$cacheKey .= sha1(implode('', $directories));
return $cacheKey;
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Translation\Loader;
/**
* Loads translations.
*/
interface LoaderInterface
{
/**
* @param string $language
* @param mixed[] $directories Directories containing translation files.
* @throws \Exception The translation file was not found
* @return string[] Translations.
*/
public function load($language, array $directories);
}