Skip to content

Commit 47b66b5

Browse files
committed
added JsonDictionary extractor
1 parent 35d2db4 commit 47b66b5

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/Extractors/JsonDictionary.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace Gettext\Extractors;
3+
4+
use Gettext\Translations;
5+
6+
/**
7+
* Class to get gettext strings from plain json
8+
*/
9+
class JsonDictionary extends Extractor implements ExtractorInterface
10+
{
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public static function fromString($string, Translations $translations = null, $file = '')
15+
{
16+
if ($translations === null) {
17+
$translations = new Translations();
18+
}
19+
20+
if (($entries = json_decode($string, true))) {
21+
foreach ($entries as $original => $translation) {
22+
$translations->insert(null, $original)->setTranslation($translation);
23+
}
24+
}
25+
26+
return $translations;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
include_once dirname(__DIR__).'/src/autoloader.php';
3+
4+
class JsonDictionaryExtractorTest extends PHPUnit_Framework_TestCase
5+
{
6+
public function testGeneration()
7+
{
8+
$string = '{"apple": "maza", "water": "auga"}';
9+
10+
//Extract translations
11+
$translations = Gettext\Translations::fromJsonDictionaryString($string);
12+
13+
$this->assertInstanceOf('Gettext\\Translations', $translations);
14+
$this->assertEquals(2, count($translations));
15+
16+
$translation = $translations->find('', 'water');
17+
18+
$this->assertInstanceOf('Gettext\\Translation', $translation);
19+
$this->assertEquals('auga', $translation->getTranslation());
20+
}
21+
}

0 commit comments

Comments
 (0)