-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
561 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# nette-localization | ||
# Smartsupp/Localization | ||
|
||
## Installation | ||
|
||
```sh | ||
$ composer require smartsupp/localization | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.