Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
dkMorlok committed Jul 2, 2016
1 parent 7203315 commit 986c8d5
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# nette-localization
# Smartsupp/Localization

## Installation

```sh
$ composer require smartsupp/localization
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "smartsupp/localization",
"type": "library",
"keywords": ["localization", "nette"],
"authors": [{
"name": "Dusan Kmet",
"email": "[email protected]"
}],
"description" : "Localization",
"version": "0.1",
"license": "",
"require": {
"php": ">=5.4.0",
"nette/di": "^2.3",
"nette/utils": "^2.3",
"nette/safe-stream": "^2.3"
},
"autoload": {
"classmap": ["src/"]
}
}
74 changes: 74 additions & 0 deletions src/DirectoryStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Smartsupp\Localization;

class DirectoryStorage implements ITranslateStorage
{

private $dir;


public function __construct($dir)
{
$this->dir = $dir;
}


/**
* Get translates for lang
* @param string $section
* @param string $lang
* @return array
*/
public function getTranslates($section, $lang)
{
$path = $this->dir . '/' . $section . '/' . $lang . '.json';
if (is_file($path)) {
$translates = array();
$data = json_decode(file_get_contents($path), true);
$data = array_filter($data, function ($value) {
return $value !== '';
});
$this->expandKeys($translates, $data);
return $translates;
} else {
return array();
}
}


/**
* Get last change
* @param string $section
* @param string $lang
* @return int
*/
public function getLastChange($section, $lang)
{
$path = $this->dir . '/' . $section . '/' . $lang . '.json';
if (is_file($path)) {
return filemtime($path);
} else {
return 0;
}
}


/**
* Expand structured translates
* @param array $translates
* @param array $data
* @param string $prefix
*/
private function expandKeys(array &$translates, array $data, $prefix = null)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$this->expandKeys($translates, $value, $prefix ? "$prefix.$key" : $key);
} else {
$translates[$prefix ? "$prefix.$key" : $key] = $value;
}
}
}

}
24 changes: 24 additions & 0 deletions src/ITranslateStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Smartsupp\Localization;

interface ITranslateStorage
{

/**
* Get translates for lang
* @param string $section
* @param string $lang
* @return array
*/
function getTranslates($section, $lang);


/**
* Get last change
* @param string $section
* @param string $lang
* @return int
*/
function getLastChange($section, $lang);
}
12 changes: 12 additions & 0 deletions src/ITranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Smartsupp\Localization;

interface ITranslator extends \Nette\Localization\ITranslator
{

function setTranslates(array $dictionary);

function getTranslates();

}
49 changes: 49 additions & 0 deletions src/LocalizationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Smartsupp\Localization;

use Nette\DI\CompilerExtension;

class LocalizationExtension extends CompilerExtension
{

public $defaults = array(
'translatesDir' => null,
'debugMode' => null,
'alias' => null,
'sections' => null
);


public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

$debugMode = $config['debugMode'] === null ? $container->parameters['debugMode'] : $config['debugMode'];

$container->addDefinition($this->prefix('translatesStorage'))
->setClass('Smartsupp\Localization\DirectoryStorage')
->setArguments(array($config['translatesDir']));

$container->addDefinition($this->prefix('translatesLoader'))
->setClass('Smartsupp\Localization\TranslatesLoader')
->addSetup('$debugMode', array($debugMode))
->addSetup('setTempDir', array('%tempDir%/cache/_Wimo.Localization'));

$translatorFactory = $container->addDefinition($this->prefix('translatorFactory'))
->setClass('Smartsupp\Localization\TranslatorFactory')
->addSetup('$debugMode', array($debugMode));

if ($config['alias']) {
foreach ($config['alias'] as $from => $to) {
$translatorFactory->addSetup('setAlias', array($from, $to));
}
}

if ($config['sections']) {
$translatorFactory->addSetup('$defaultSections', array($config['sections']));
}
}

}
92 changes: 92 additions & 0 deletions src/TranslatesCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Smartsupp\Localization;

use Nette\Utils\SafeStream;

/**
* TranslatesCache
*/
class TranslatesCache
{
/** @var string */
private $tempDir;

/** @var string */
private $filename;


/**
* Set temp dir
* @param string $tempDir
*/
public function setTempDir($tempDir)
{
$this->tempDir = $tempDir;
}


/**
* Set filename of cached file
* @param string $filename
*/
public function setFilename($filename)
{
$this->filename = $filename;
}


/**
* Check if cache file exists
* @return bool
*/
public function isCached()
{
return is_file($this->getCacheFile());
}


/**
* Get cache file path
* @return string
*/
public function getCacheFile()
{
return $this->tempDir . '/' . $this->filename;
}


/**
* Load cached translates
* @return array
*/
public function load()
{
if ($this->isCached()) {
return include $this->getCacheFile();
} else {
return null;
}
}


/**
* Store translates
* @param string $translates
*/
public function save($translates)
{
if (preg_match('/^([^\-]+\-[^\-]+\-).*$/', $this->filename, $matches)) {
foreach (scandir($this->tempDir) as $f) { // clean old
if ($f[0] != '.') {
if (strpos($f, $matches[1]) === 0) {
unlink($this->tempDir . '/' . $f);
}
}
}
}
SafeStream::register();
file_put_contents('nette.safe://' . $this->getCacheFile(), $translates);
}

}
Loading

0 comments on commit 986c8d5

Please sign in to comment.