Skip to content

Commit

Permalink
Merge pull request #223 from briedis/multiple-domain-scan
Browse files Browse the repository at this point in the history
Multiple domain scan
  • Loading branch information
oscarotero committed Sep 15, 2019
2 parents a202411 + dd5dafa commit 2c1d126
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 119 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
sudo: false
dist: trusty

env:
- COMPOSER_DISABLE_XDEBUG_WARN=1
Expand Down
28 changes: 28 additions & 0 deletions src/Extractors/ExtractorMultiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Gettext\Extractors;

use Gettext\Translations;

interface ExtractorMultiInterface
{
/**
* Parses a string and append the translations found in the Translations instance.
* Allows scanning for multiple domains at a time (each Translation has to have a different domain)
*
* @param string $string
* @param Translations[] $translations
* @param array $options
*/
public static function fromStringMultiple($string, array $translations, array $options = []);

/**
* Parses a string and append the translations found in the Translations instance.
* Allows scanning for multiple domains at a time (each Translation has to have a different domain)
*
* @param $file
* @param Translations[] $translations
* @param array $options
*/
public static function fromFileMultiple($file, array $translations, array $options = []);
}
28 changes: 25 additions & 3 deletions src/Extractors/JsCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Gettext\Extractors;

use Exception;
use Gettext\Translations;
use Gettext\Utils\JsFunctionsScanner;

/**
* Class to get gettext strings from javascript files.
*/
class JsCode extends Extractor implements ExtractorInterface
class JsCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
{
public static $options = [
'constants' => [],
Expand Down Expand Up @@ -36,14 +37,35 @@ class JsCode extends Extractor implements ExtractorInterface
];

/**
* {@inheritdoc}
* @throws \Exception
* @inheritdoc
* @throws Exception
*/
public static function fromString($string, Translations $translations, array $options = [])
{
self::fromStringMultiple($string, [$translations], $options);
}

/**
* @inheritDoc
* @throws Exception
*/
public static function fromStringMultiple($string, array $translations, array $options = [])
{
$options += static::$options;

$functions = new JsFunctionsScanner($string);
$functions->saveGettextFunctions($translations, $options);
}

/**
* @inheritDoc
* @throws Exception
*/
public static function fromFileMultiple($file, array $translations, array $options = [])
{
foreach (self::getFiles($file) as $file) {
$options['file'] = $file;
static::fromStringMultiple(self::readFile($file), $translations, $options);
}
}
}
53 changes: 41 additions & 12 deletions src/Extractors/PhpCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace Gettext\Extractors;

use Exception;
use Gettext\Translations;
use Gettext\Utils\PhpFunctionsScanner;

/**
* Class to get gettext strings from php files returning arrays.
*/
class PhpCode extends Extractor implements ExtractorInterface
class PhpCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
{
public static $options = [
// - false: to not extract comments
// - empty string: to extract all comments
// - non-empty string: to extract comments that start with that string
// - array with strings to extract comments format.
// - false: to not extract comments
// - empty string: to extract all comments
// - non-empty string: to extract comments that start with that string
// - array with strings to extract comments format.
'extractComments' => false,

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

/**
* {@inheritdoc}
* @throws Exception
*/
public static function fromString($string, Translations $translations, array $options = [])
{
self::fromStringMultiple($string, [$translations], $options);
}

/**
* @inheritDoc
* @throws Exception
*/
public static function fromStringMultiple($string, array $translations, array $options = [])
{
$options += static::$options;

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

/**
* @inheritDoc
*/
public static function fromFileMultiple($file, array $translations, array $options = [])
{
foreach (self::getFiles($file) as $file) {
$options['file'] = $file;
static::fromStringMultiple(self::readFile($file), $translations, $options);
}
}


/**
* Decodes a T_CONSTANT_ENCAPSED_STRING string.
*
Expand Down Expand Up @@ -110,7 +133,11 @@ function ($match) {
);
}

//http://php.net/manual/en/function.chr.php#118804
/**
* @param $dec
* @return string|null
* @see http://php.net/manual/en/function.chr.php#118804
*/
private static function unicodeChar($dec)
{
if ($dec < 0x80) {
Expand All @@ -119,20 +146,22 @@ private static function unicodeChar($dec)

if ($dec < 0x0800) {
return chr(0xC0 + ($dec >> 6))
.chr(0x80 + ($dec & 0x3f));
. chr(0x80 + ($dec & 0x3f));
}

if ($dec < 0x010000) {
return chr(0xE0 + ($dec >> 12))
.chr(0x80 + (($dec >> 6) & 0x3f))
.chr(0x80 + ($dec & 0x3f));
. chr(0x80 + (($dec >> 6) & 0x3f))
. chr(0x80 + ($dec & 0x3f));
}

if ($dec < 0x200000) {
return chr(0xF0 + ($dec >> 18))
.chr(0x80 + (($dec >> 12) & 0x3f))
.chr(0x80 + (($dec >> 6) & 0x3f))
.chr(0x80 + ($dec & 0x3f));
. chr(0x80 + (($dec >> 12) & 0x3f))
. chr(0x80 + (($dec >> 6) & 0x3f))
. chr(0x80 + ($dec & 0x3f));
}

return null;
}
}

0 comments on commit 2c1d126

Please sign in to comment.