1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?php
namespace Gettext\Generators;
use Gettext\Translations;
class Jed extends Generator implements GeneratorInterface { public static $options = [ 'json' => 0, ];
/** * {@parentDoc}. */ public static function toString(Translations $translations, array $options = []) { $domain = $translations->getDomain() ?: 'messages'; $options += static::$options;
return json_encode([ $domain => [ '' => [ 'domain' => $domain, 'lang' => $translations->getLanguage() ?: 'en', 'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);', ], ] + self::buildMessages($translations), ], $options['json']); }
/** * Generates an array with all translations. * * @param Translations $translations * * @return array */ private static function buildMessages(Translations $translations) { $pluralForm = $translations->getPluralForms(); $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; $messages = []; $context_glue = '\u0004';
foreach ($translations as $translation) { $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '') .$translation->getOriginal();
if ($translation->hasPluralTranslations(true)) { $message = $translation->getPluralTranslations($pluralSize); array_unshift($message, $translation->getTranslation()); } else { $message = [$translation->getTranslation()]; }
$messages[$key] = $message; }
return $messages; } }
|