Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Aug 18, 2015
0 parents commit 1eb7409
Show file tree
Hide file tree
Showing 431 changed files with 9,830 additions and 0 deletions.
50 changes: 50 additions & 0 deletions FileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace Wild\Localize;
class FileReader {
var $_pos;
var $_fd;
var $_length;
function __construct($filename){
if(file_exists($filename)){
$this->_length=filesize($filename);
$this->_pos = 0;
$this->_fd = fopen($filename,'rb');
if(!$this->_fd){
$this->error = 3;
return false;
}
}
else{
$this->error = 2;
return false;
}
}
function read($bytes){
if ($bytes) {
fseek($this->_fd, $this->_pos);
$data = '';
while($bytes > 0){
$chunk = fread($this->_fd, $bytes);
$data .= $chunk;
$bytes -= strlen($chunk);
}
$this->_pos = ftell($this->_fd);
return $data;
}
else
return '';
}
function seekto($pos) {
fseek($this->_fd, $pos);
$this->_pos = ftell($this->_fd);
return $this->_pos;
}
function currentpos() {
return $this->_pos;
}
function length() {
return $this->_length;
}
function close() {
fclose($this->_fd);
}
}
89 changes: 89 additions & 0 deletions Gettext/Extractors/Extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Exception;
use InvalidArgumentException;
use Wild\Localize\Gettext\Translations;

abstract class Extractor
{
/**
* Extract the translations from a file
*
* @param array|string $file A path of a file or files
* @param null|Translations $translations The translations instance to append the new translations.
*
* @return Translations
*/
public static function fromFile($file, Translations $translations = null)
{
if ($translations === null) {
$translations = new Translations();
}

foreach (self::getFiles($file) as $file) {
static::fromString(self::readFile($file), $translations, $file);
}

return $translations;
}

/**
* Checks and returns all files
*
* @param string|array $file The file/s
*
* @return array The file paths
*/
protected static function getFiles($file)
{
if (empty($file)) {
throw new InvalidArgumentException('There is not any file defined');
}

if (is_string($file)) {
if (!is_file($file)) {
throw new InvalidArgumentException("'$file' is not a valid file");
}

if (!is_readable($file)) {
throw new InvalidArgumentException("'$file' is not a readable file");
}

return array($file);
}

if (is_array($file)) {
$files = array();

foreach ($file as $f) {
$files = array_merge($files, self::getFiles($f));
}

return $files;
}

throw new InvalidArgumentException('The first argumet must be string or array');
}

/**
* Reads and returns the content of a file
*
* @param string $file
*
* @return string
*/
protected static function readFile($file)
{
$length = filesize($file);

if (!($fd = fopen($file, 'rb'))) {
throw new Exception("Cannot read the file '$file', probably permissions");
}

$content = fread($fd, $length);
fclose($fd);

return $content;
}
}
28 changes: 28 additions & 0 deletions Gettext/Extractors/ExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Wild\Localize\Gettext\Translations;

interface ExtractorInterface
{
/**
* Extract the translations from a file
*
* @param array|string $file A path of a file or files
* @param null|Translations $translations The translations instance to append the new translations.
*
* @return Translations
*/
public static function fromFile($file, Translations $translations = null);

/**
* Parses a string and append the translations found in the Translations instance
*
* @param string $string
* @param Translations|null $translations
* @param string $file The file path to insert the reference
*
* @return Translations
*/
public static function fromString($string, Translations $translations = null, $file = '');
}
24 changes: 24 additions & 0 deletions Gettext/Extractors/Jed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Wild\Localize\Gettext\Translations;

/**
* Class to get gettext strings from json files
*/
class Jed extends PhpArray implements ExtractorInterface
{
/**
* {@inheritDoc}
*/
public static function fromString($string, Translations $translations = null, $file = '')
{
if ($translations === null) {
$translations = new Translations();
}

$content = json_decode($string);

return PhpArray::handleArray($content, $translations);
}
}
30 changes: 30 additions & 0 deletions Gettext/Extractors/JsCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Wild\Localize\Gettext\Translations;
use Wild\Localize\Gettext\Utils\JsFunctionsScanner;

/**
* Class to get gettext strings from javascript files
*/
class JsCode extends Extractor implements ExtractorInterface
{
public static $functions = array(
'__' => '__',
'n__' => 'n__',
'p__' => 'p__',
);

/**
* {@inheritDoc}
*/
public static function fromString($string, Translations $translations = null, $file = '')
{
if ($translations === null) {
$translations = new Translations();
}

$functions = new JsFunctionsScanner($string);
$functions->saveGettextFunctions(self::$functions, $translations, $file);
}
}
96 changes: 96 additions & 0 deletions Gettext/Extractors/Mo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Wild\Localize\Gettext\Translations;
use Wild\Localize\Gettext\Utils\StringReader;

/**
* Class to get gettext strings from .mo files
*/

class Mo extends Extractor implements ExtractorInterface
{
const MAGIC1 = -1794895138;
const MAGIC2 = -569244523;
const MAGIC3 = 2500072158;

/**
* {@inheritDoc}
*/
public static function fromString($string, Translations $translations = null, $file = '')
{
if ($translations === null) {
$translations = new Translations();
}

$stream = new StringReader($string);
$magic = self::readInt($stream, 'V');

if (($magic === self::MAGIC1) || ($magic === self::MAGIC3)) { //to make sure it works for 64-bit platforms
$byteOrder = 'V'; //low endian
} elseif ($magic === (self::MAGIC2 & 0xFFFFFFFF)) {
$byteOrder = 'N'; //big endian
} else {
throw new \Exception('Not MO file');
}

self::readInt($stream, $byteOrder);

$total = self::readInt($stream, $byteOrder); //total string count
$originals = self::readInt($stream, $byteOrder); //offset of original table
$tran = self::readInt($stream, $byteOrder); //offset of translation table

$stream->seekto($originals);
$table_originals = self::readIntArray($stream, $byteOrder, $total * 2);
$stream->seekto($tran);
$table_translations = self::readIntArray($stream, $byteOrder, $total * 2);

for ($i = 0; $i < $total; $i++) {
$stream->seekto($table_originals[$i * 2 + 2]);
$original = $stream->read($table_originals[$i * 2 + 1]);

if (empty($original)) {
continue;
}

$stream->seekto($table_translations[$i * 2 + 2]);
$original = explode("\000", $original, 2);
$translated = explode("\000", $stream->read($table_translations[$i * 2 + 1]), 2);

$plural = isset($original[1]) ? $original[1] : '';
$pluralTranslation = isset($translated[1]) ? $translated[1] : '';

$translation = $translations->insert(null, $original[0], $plural);
$translation->setTranslation($translated[0]);

if ($plural && $pluralTranslation) {
$translation->setPluralTranslation($pluralTranslation);
}
}
}

/**
* @param StringReader $stream
* @param string $byteOrder
*/
private static function readInt(StringReader $stream, $byteOrder)
{
if (($read = $stream->read(4)) === false) {
return false;
}

$read = unpack($byteOrder, $read);

return array_shift($read);
}

/**
* @param StringReader $stream
* @param string $byteOrder
* @param int $count
*/
private static function readIntArray(StringReader $stream, $byteOrder, $count)
{
return unpack($byteOrder.$count, $stream->read(4 * $count));
}
}
74 changes: 74 additions & 0 deletions Gettext/Extractors/PhpArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace Wild\Localize\Gettext\Extractors;

use Exception;
use Wild\Localize\Gettext\Translations;

/**
* Class to get gettext strings from php files returning arrays
*/
class PhpArray extends Extractor implements ExtractorInterface
{
/**
* Extract the translations from a file
*
* @param array|string $file A path of a file or files
* @param null|Translations $translations The translations instance to append the new translations.
*
* @return Translations
*/
public static function fromFile($file, Translations $translations = null)
{
if ($translations === null) {
$translations = new Translations();
}

foreach (self::getFiles($file) as $file) {
self::handleArray(include($file), $translations);
}

return $translations;
}

/**
* {@inheritDoc}
*/
public static function fromString($string, Translations $translations = null, $file = '')
{
throw new Exception("PhpArray::fromString() cannot be called. Use PhpArray::fromFile()");
}

/**
* Handle an array of translations and append to the Translations instance
*
* @param array $content
* @param Translations $translations
*/
public static function handleArray(array $content, Translations $translations)
{
$content = $content['messages'];

$translations_info = isset($content['']) ? $content[''] : null;
unset($content['']);

if (isset($translations_info['domain'])) {
$translations->setDomain($translations_info['domain']);
}

$context_glue = '\u0004';

foreach ($content as $key => $message) {
$key = explode($context_glue, $key);

$context = isset($key[1]) ? array_shift($key) : '';
$original = array_shift($key);
$plural = array_shift($message);
$translation = array_shift($message);
$plural_translation = array_shift($message);

$entry = $translations->find($context, $original, $plural) ?: $translations->insert($context, $original, $plural);
$entry->setTranslation($translation);
$entry->setPluralTranslation($plural_translation);
}
}
}
Loading

0 comments on commit 1eb7409

Please sign in to comment.