Skip to content

Commit 2c1d126

Browse files
authored
Merge pull request #223 from briedis/multiple-domain-scan
Multiple domain scan
2 parents a202411 + dd5dafa commit 2c1d126

18 files changed

Lines changed: 524 additions & 119 deletions

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: php
22
sudo: false
3+
dist: trusty
34

45
env:
56
- COMPOSER_DISABLE_XDEBUG_WARN=1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Gettext\Extractors;
4+
5+
use Gettext\Translations;
6+
7+
interface ExtractorMultiInterface
8+
{
9+
/**
10+
* Parses a string and append the translations found in the Translations instance.
11+
* Allows scanning for multiple domains at a time (each Translation has to have a different domain)
12+
*
13+
* @param string $string
14+
* @param Translations[] $translations
15+
* @param array $options
16+
*/
17+
public static function fromStringMultiple($string, array $translations, array $options = []);
18+
19+
/**
20+
* Parses a string and append the translations found in the Translations instance.
21+
* Allows scanning for multiple domains at a time (each Translation has to have a different domain)
22+
*
23+
* @param $file
24+
* @param Translations[] $translations
25+
* @param array $options
26+
*/
27+
public static function fromFileMultiple($file, array $translations, array $options = []);
28+
}

src/Extractors/JsCode.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Gettext\Extractors;
44

5+
use Exception;
56
use Gettext\Translations;
67
use Gettext\Utils\JsFunctionsScanner;
78

89
/**
910
* Class to get gettext strings from javascript files.
1011
*/
11-
class JsCode extends Extractor implements ExtractorInterface
12+
class JsCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
1213
{
1314
public static $options = [
1415
'constants' => [],
@@ -36,14 +37,35 @@ class JsCode extends Extractor implements ExtractorInterface
3637
];
3738

3839
/**
39-
* {@inheritdoc}
40-
* @throws \Exception
40+
* @inheritdoc
41+
* @throws Exception
4142
*/
4243
public static function fromString($string, Translations $translations, array $options = [])
44+
{
45+
self::fromStringMultiple($string, [$translations], $options);
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
* @throws Exception
51+
*/
52+
public static function fromStringMultiple($string, array $translations, array $options = [])
4353
{
4454
$options += static::$options;
4555

4656
$functions = new JsFunctionsScanner($string);
4757
$functions->saveGettextFunctions($translations, $options);
4858
}
59+
60+
/**
61+
* @inheritDoc
62+
* @throws Exception
63+
*/
64+
public static function fromFileMultiple($file, array $translations, array $options = [])
65+
{
66+
foreach (self::getFiles($file) as $file) {
67+
$options['file'] = $file;
68+
static::fromStringMultiple(self::readFile($file), $translations, $options);
69+
}
70+
}
4971
}

src/Extractors/PhpCode.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace Gettext\Extractors;
44

5+
use Exception;
56
use Gettext\Translations;
67
use Gettext\Utils\PhpFunctionsScanner;
78

89
/**
910
* Class to get gettext strings from php files returning arrays.
1011
*/
11-
class PhpCode extends Extractor implements ExtractorInterface
12+
class PhpCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
1213
{
1314
public static $options = [
14-
// - false: to not extract comments
15-
// - empty string: to extract all comments
16-
// - non-empty string: to extract comments that start with that string
17-
// - array with strings to extract comments format.
15+
// - false: to not extract comments
16+
// - empty string: to extract all comments
17+
// - non-empty string: to extract comments that start with that string
18+
// - array with strings to extract comments format.
1819
'extractComments' => false,
1920

2021
'constants' => [],
@@ -43,8 +44,18 @@ class PhpCode extends Extractor implements ExtractorInterface
4344

4445
/**
4546
* {@inheritdoc}
47+
* @throws Exception
4648
*/
4749
public static function fromString($string, Translations $translations, array $options = [])
50+
{
51+
self::fromStringMultiple($string, [$translations], $options);
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
* @throws Exception
57+
*/
58+
public static function fromStringMultiple($string, array $translations, array $options = [])
4859
{
4960
$options += static::$options;
5061

@@ -57,6 +68,18 @@ public static function fromString($string, Translations $translations, array $op
5768
$functions->saveGettextFunctions($translations, $options);
5869
}
5970

71+
/**
72+
* @inheritDoc
73+
*/
74+
public static function fromFileMultiple($file, array $translations, array $options = [])
75+
{
76+
foreach (self::getFiles($file) as $file) {
77+
$options['file'] = $file;
78+
static::fromStringMultiple(self::readFile($file), $translations, $options);
79+
}
80+
}
81+
82+
6083
/**
6184
* Decodes a T_CONSTANT_ENCAPSED_STRING string.
6285
*
@@ -110,7 +133,11 @@ function ($match) {
110133
);
111134
}
112135

113-
//http://php.net/manual/en/function.chr.php#118804
136+
/**
137+
* @param $dec
138+
* @return string|null
139+
* @see http://php.net/manual/en/function.chr.php#118804
140+
*/
114141
private static function unicodeChar($dec)
115142
{
116143
if ($dec < 0x80) {
@@ -119,20 +146,22 @@ private static function unicodeChar($dec)
119146

120147
if ($dec < 0x0800) {
121148
return chr(0xC0 + ($dec >> 6))
122-
.chr(0x80 + ($dec & 0x3f));
149+
. chr(0x80 + ($dec & 0x3f));
123150
}
124151

125152
if ($dec < 0x010000) {
126153
return chr(0xE0 + ($dec >> 12))
127-
.chr(0x80 + (($dec >> 6) & 0x3f))
128-
.chr(0x80 + ($dec & 0x3f));
154+
. chr(0x80 + (($dec >> 6) & 0x3f))
155+
. chr(0x80 + ($dec & 0x3f));
129156
}
130157

131158
if ($dec < 0x200000) {
132159
return chr(0xF0 + ($dec >> 18))
133-
.chr(0x80 + (($dec >> 12) & 0x3f))
134-
.chr(0x80 + (($dec >> 6) & 0x3f))
135-
.chr(0x80 + ($dec & 0x3f));
160+
. chr(0x80 + (($dec >> 12) & 0x3f))
161+
. chr(0x80 + (($dec >> 6) & 0x3f))
162+
. chr(0x80 + ($dec & 0x3f));
136163
}
164+
165+
return null;
137166
}
138167
}

0 commit comments

Comments
 (0)