Skip to content

Commit 6baa57d

Browse files
committed
cs fixes, updated contributors in readme
1 parent 1d1d036 commit 6baa57d

7 files changed

Lines changed: 43 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,5 @@ Note, if the second argument is not defined, the default is `self::MERGE_ADD | s
264264
* [leom](https://github.com/leom) (Jed fixes)
265265
* [eusonlito](https://github.com/eusonlito) (Blade extractor)
266266
* [vvh-empora](https://github.com/vvh-empora) (fixes)
267+
* [mlocati](https://github.com/mlocati) (Mo generator/extractor)
268+
* [and many more...](https://github.com/oscarotero/Gettext/graphs/contributors)

src/Extractors/Mo.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static function fromString($string, Translations $translations = null, $f
5050
$original = $stream->read($table_originals[$i * 2 + 1]);
5151
$stream->seekto($table_translations[$i * 2 + 2]);
5252
$translated = $stream->read($table_translations[$i * 2 + 1]);
53+
5354
if ($original === '') {
5455
// Headers
5556
foreach (explode("\n", $translated) as $headerLine) {
@@ -60,20 +61,25 @@ public static function fromString($string, Translations $translations = null, $f
6061
}
6162
} else {
6263
$chunks = explode("\x04", $original, 2);
64+
6365
if (isset($chunks[1])) {
6466
$context = $chunks[0];
6567
$original = $chunks[1];
6668
} else {
6769
$context = '';
6870
}
71+
6972
$chunks = explode("\x00", $original, 2);
73+
7074
if (isset($chunks[1])) {
7175
$original = $chunks[0];
7276
$plural = $chunks[1];
7377
} else {
7478
$plural = '';
7579
}
80+
7681
$translation = $translations->insert($context, $original, $plural);
82+
7783
if ($translated !== '') {
7884
if ($plural === '') {
7985
$translation->setTranslation($translated);

src/Generators/Mo.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,86 +12,112 @@ public static function toString(Translations $translations)
1212
{
1313
$array = array();
1414
$headers = '';
15+
1516
foreach ($translations->getHeaders() as $headerName => $headerValue) {
1617
$headers .= "$headerName: $headerValue\n";
1718
}
19+
1820
if (strlen($headers)) {
1921
$array[''] = $headers;
2022
}
23+
2124
foreach ($translations as $translation) {
2225
if ($translation->hasContext()) {
2326
$originalString = $translation->getContext()."\x04".$translation->getOriginal();
2427
} else {
2528
$originalString = $translation->getOriginal();
2629
}
30+
2731
if ($translation->hasPlural()) {
2832
$originalString .= "\x00".$translation->getPlural();
2933
}
34+
3035
$array[$originalString] = $translation;
3136
}
37+
3238
ksort($array);
3339
$numEntries = count($array);
3440
$originalsTable = '';
3541
$translationsTable = '';
3642
$originalsIndex = array();
3743
$translationsIndex = array();
44+
3845
foreach ($array as $originalString => $translation) {
3946
if (is_string($translation)) {
4047
// Headers
4148
$translationString = $translation;
4249
} else {
4350
/* @var $translation \Gettext\Translation */
4451
$translationString = $translation->getTranslation();
52+
4553
if ($translation->hasPluralTranslation()) {
4654
$translationString .= "\x00".implode("\x00", $translation->getPluralTranslation());
4755
}
4856
}
57+
4958
$originalsIndex[] = array('relativeOffset' => strlen($originalsTable), 'length' => strlen($originalString));
5059
$originalsTable .= $originalString."\x00";
5160
$translationsIndex[] = array('relativeOffset' => strlen($translationsTable), 'length' => strlen($translationString));
5261
$translationsTable .= $translationString."\x00";
5362
}
63+
5464
// Offset of table with the original strings index: right after the header (which is 7 words)
5565
$originalsIndexOffset = 7 * 4;
66+
5667
// Size of table with the original strings index
5768
$originalsIndexSize = $numEntries * (4 + 4);
69+
5870
// Offset of table with the translation strings index: right after the original strings index table
5971
$translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize;
72+
6073
// Size of table with the translation strings index
6174
$translationsIndexSize = $numEntries * (4 + 4);
75+
6276
// Hashing table starts after the header and after the index table
6377
$originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize;
78+
6479
// Translations start after the keys
6580
$translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable);
6681

6782
// Let's generate the .mo file binary data
6883
$mo = '';
84+
6985
// Magic number
7086
$mo .= pack('L', 0x950412de);
87+
7188
// File format revision
7289
$mo .= pack('L', 0);
90+
7391
// Number of strings
7492
$mo .= pack('L', $numEntries);
93+
7594
// Offset of table with original strings
7695
$mo .= pack('L', $originalsIndexOffset);
96+
7797
// Offset of table with translation strings
7898
$mo .= pack('L', $translationsIndexOffset);
99+
79100
// Size of hashing table: we don't use it.
80101
$mo .= pack('L', 0);
102+
81103
// Offset of hashing table: it would start right after the translations index table
82104
$mo .= pack('L', $translationsIndexOffset + $translationsIndexSize);
105+
83106
// Write the lengths & offsets of the original strings
84107
foreach ($originalsIndex as $info) {
85108
$mo .= pack('L', $info['length']);
86109
$mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']);
87110
}
111+
88112
// Write the lengths & offsets of the translated strings
89113
foreach ($translationsIndex as $info) {
90114
$mo .= pack('L', $info['length']);
91115
$mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']);
92116
}
117+
93118
// Write original strings
94119
$mo .= $originalsTable;
120+
95121
// Write translation strings
96122
$mo .= $translationsTable;
97123

src/Translator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ class Translator
1212
private $context_glue = "\004";
1313
private $plurals = array();
1414

15-
1615
/**
1716
* Set a translation instance as global, to use it with the gettext functions
18-
*
17+
*
1918
* @param Translator $translator
2019
*/
2120
public static function initGettextFunctions(Translator $translator)
@@ -25,7 +24,6 @@ public static function initGettextFunctions(Translator $translator)
2524
include_once __DIR__.'/translator_functions.php';
2625
}
2726

28-
2927
/**
3028
* Loads translation from a Translations instance, a file on an array
3129
*
@@ -50,11 +48,10 @@ public function loadTranslations($translations)
5048
return $this;
5149
}
5250

53-
5451
/**
5552
* Set new translations to the dictionary
5653
*
57-
* @param array $translations
54+
* @param array $translations
5855
*/
5956
public function addTranslations(array $translations)
6057
{

tests/PoExtractorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function testParser()
99
$translations = Gettext\Extractors\Po::fromFile(__DIR__.'/files/po.po');
1010

1111
$this->assertInstanceOf('Gettext\\Translation', $translations->find(null, '%ss must be unique for %ss %ss.'));
12-
12+
1313
$this->assertEquals($translations->find(null, 'and')->getFlags(), array('c-format'));
1414
$this->assertEquals($translations->find(null, 'Value %sr is not a valid choice.')->getExtractedComments(), array('This is a extracted comment'));
1515
}
@@ -59,14 +59,14 @@ public function testReferences()
5959
$this->assertEquals(
6060
$translations->find(null, 'This field cannot be null.')->getReferences(),
6161
array(
62-
array('C:/Users/Me/Documents/foo2.php', '1')
62+
array('C:/Users/Me/Documents/foo2.php', '1'),
6363
)
6464
);
6565

6666
$this->assertEquals(
6767
$translations->find(null, 'This field cannot be blank.')->getReferences(),
6868
array(
69-
array('C:/Users/Me/Documents/foo1.php', null)
69+
array('C:/Users/Me/Documents/foo1.php', null),
7070
)
7171
);
7272

@@ -82,7 +82,7 @@ public function testReferences()
8282
public function testMultilineTranslation()
8383
{
8484
$translations = Gettext\Extractors\Po::fromFile(__DIR__.'/files/po.po');
85-
85+
8686
$t1 = $translations->find(null, '{test1}');
8787
$this->assertInstanceOf('Gettext\\Translation', $t1);
8888

tests/TranslationsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testMergeReferences()
108108

109109
$this->assertTrue($translation2->hasReferences());
110110
$this->assertCount(1, $actualRef = $translation2->getReferences());
111-
$expectedRef2 = array($comment, $line);
111+
$expectedRef2 = array($comment, $line);
112112
$this->assertEquals($expectedRef2, current($actualRef));
113113

114114
//merge with references

tests/TranslatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function testOne()
1313
$this->assertEquals('Cijeo broj', $t->gettext('Integer'));
1414
$this->assertEquals('Ovo polje ne može biti prazno.', $t->gettext('This field cannot be blank.'));
1515
$this->assertEquals('Value %sr is not a valid choice.', $t->gettext('Value %sr is not a valid choice.'));
16-
16+
1717
//domains
1818
$this->assertEquals('single', $t->gettext('single'));
1919
$this->assertEquals('test', $t->dgettext('messages', 'single'));
@@ -26,7 +26,7 @@ public function testFunctions()
2626
$t->loadTranslations(Gettext\Translations::fromPoFile(__DIR__.'/files/po.po'));
2727

2828
Gettext\Translator::initGettextFunctions($t);
29-
29+
3030
$this->assertEquals('Cijeo broj', __('Integer'));
3131
$this->assertEquals('Ovo polje ne može biti prazno.', __('This field cannot be blank.'));
3232
$this->assertEquals('Value %sr is not a valid choice.', __('Value %sr is not a valid choice.'));

0 commit comments

Comments
 (0)