diff options
author | Matthieu Aubry <matt@piwik.org> | 2015-02-10 02:45:45 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2015-02-10 02:45:45 +0300 |
commit | 36d87593bc0dfbad4126ef1ed4845be3f30e54dc (patch) | |
tree | f799641c9cce9b077d0a2429fccba63f4b5084e6 | |
parent | 25409d6d66524f71cc1379c409d73cefbd72ae59 (diff) | |
parent | 3bf35892dd9c42d7765c3e79906abc06cc6f350b (diff) |
Merge pull request #7156 from piwik/translation-id-language
Added a new "Development" language that shows the raw translation IDs
-rw-r--r-- | config/environment/dev.php | 6 | ||||
-rw-r--r-- | core/Translation/Loader/DevelopmentLoader.php | 72 | ||||
-rw-r--r-- | lang/dev.json | 6 | ||||
-rw-r--r-- | plugins/LanguagesManager/API.php | 14 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/Translation/Loader/DevelopmentLoaderTest.php | 59 |
5 files changed, 155 insertions, 2 deletions
diff --git a/config/environment/dev.php b/config/environment/dev.php index c695c23890..ab095a7aa5 100644 --- a/config/environment/dev.php +++ b/config/environment/dev.php @@ -2,7 +2,9 @@ return array( - // Disable translation cache - 'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\JsonFileLoader'), + 'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\LoaderCache') + ->constructor(DI\link('Piwik\Translation\Loader\DevelopmentLoader')), + 'Piwik\Translation\Loader\DevelopmentLoader' => DI\object() + ->constructor(DI\link('Piwik\Translation\Loader\JsonFileLoader')), ); diff --git a/core/Translation/Loader/DevelopmentLoader.php b/core/Translation/Loader/DevelopmentLoader.php new file mode 100644 index 0000000000..a75af4bd2e --- /dev/null +++ b/core/Translation/Loader/DevelopmentLoader.php @@ -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); + } +} diff --git a/lang/dev.json b/lang/dev.json new file mode 100644 index 0000000000..a7346e864a --- /dev/null +++ b/lang/dev.json @@ -0,0 +1,6 @@ +{ + "General": { + "OriginalLanguageName": "Development", + "EnglishLanguageName": "Development" + } +} diff --git a/plugins/LanguagesManager/API.php b/plugins/LanguagesManager/API.php index f3f50ad57f..ca491892ed 100644 --- a/plugins/LanguagesManager/API.php +++ b/plugins/LanguagesManager/API.php @@ -10,10 +10,12 @@ namespace Piwik\Plugins\LanguagesManager; use Piwik\Db; +use Piwik\Development; use Piwik\Filesystem; use Piwik\Piwik; use Piwik\Cache as PiwikCache; use Piwik\Plugin\Manager as PluginManager; +use Piwik\Translation\Loader\DevelopmentLoader; /** * The LanguagesManager API lets you access existing Piwik translations, and change Users languages preferences. @@ -66,6 +68,8 @@ class API extends \Piwik\Plugin\API } } + $this->enableDevelopmentLanguageInDevEnvironment($languages); + /** * Hook called after loading available language files. * @@ -295,4 +299,14 @@ class API extends \Piwik\Plugin\API $this->availableLanguageNames = $languagesInfo; } + + private function enableDevelopmentLanguageInDevEnvironment(&$languages) + { + if (!Development::isEnabled()) { + $key = array_search(DevelopmentLoader::LANGUAGE_ID, $languages); + if ($key) { + unset($languages[$key]); + } + } + } } diff --git a/tests/PHPUnit/Unit/Translation/Loader/DevelopmentLoaderTest.php b/tests/PHPUnit/Unit/Translation/Loader/DevelopmentLoaderTest.php new file mode 100644 index 0000000000..bc36a93f03 --- /dev/null +++ b/tests/PHPUnit/Unit/Translation/Loader/DevelopmentLoaderTest.php @@ -0,0 +1,59 @@ +<?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\Tests\Unit\Translation\Loader; + +use Piwik\Translation\Loader\DevelopmentLoader; + +/** + * @group Translation + */ +class DevelopmentLoaderTest extends \PHPUnit_Framework_TestCase +{ + private $translations = array( + 'General' => array( + 'translationId' => 'Hello', + ), + ); + + public function test_shouldReturnTranslationIds_ifDevelopmentLanguage() + { + $wrappedLoader = $this->getMockForAbstractClass('Piwik\Translation\Loader\LoaderInterface'); + $loader = new DevelopmentLoader($wrappedLoader); + + $wrappedLoader->expects($this->once()) + ->method('load') + ->with('en', array('directory')) + ->willReturn($this->translations); + + $translations = $loader->load(DevelopmentLoader::LANGUAGE_ID, array('directory')); + + $expected = array( + 'General' => array( + 'translationId' => 'General_translationId', + ), + ); + + $this->assertEquals($expected, $translations); + } + + public function test_shouldUseDecoratedLoader_ifNotDevelopmentLanguage() + { + $wrappedLoader = $this->getMockForAbstractClass('Piwik\Translation\Loader\LoaderInterface'); + $loader = new DevelopmentLoader($wrappedLoader); + + $wrappedLoader->expects($this->once()) + ->method('load') + ->with('fr', array('directory')) + ->willReturn($this->translations); + + $translations = $loader->load('fr', array('directory')); + + $this->assertEquals($this->translations, $translations); + } +} |