File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments