Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching database storage and untranslated strings #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
62 changes: 47 additions & 15 deletions LiveTranslator/Storage/NetteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ class NetteDatabase implements \LiveTranslator\ITranslatorStorage
/** @var string */
private $translationTable;

/** @var \Nette\Caching\Cache */
private $cache;


/**
* @param string $defaultTableName name of table with original texts
* @param string $translationTableName name of table with translated texts
* @param \Nette\Database\Connection $db
* @param \Nette\Database\Context|NULL $context
* @param \Nette\Caching\IStorage $cacheStorage
*/
public function __construct($defaultTableName, $translationTableName, \Nette\Database\Connection $db, \Nette\Database\Context $context = NULL)
public function __construct($defaultTableName, $translationTableName, \Nette\Database\Connection $db, \Nette\Database\Context $context = NULL, \Nette\Caching\IStorage $cacheStorage)
{
$this->db = $context ?: $db; // Context is part of newer Nette version
if ($defaultTableName[0] !== '`') {
Expand All @@ -34,29 +37,50 @@ public function __construct($defaultTableName, $translationTableName, \Nette\Dat
}
$this->defaultTable = $defaultTableName;
$this->translationTable = $translationTableName;

$this->cache = new \Nette\Caching\Cache($cacheStorage, 'VladaHejda.LiveTranslator');
}



public function cacheDisable($disable = TRUE)
{
if ($disable) {
$this->cache = NULL;
}
}



public function getTranslation($original, $lang, $variant = 0, $namespace = NULL)
{
$arg = array();
$translation = isset($this->cache) ? $this->cache->load($original.$lang.$variant.$namespace) : NULL;

$arg[0] = "SELECT t.`translation` FROM {$this->defaultTable} d
JOIN {$this->translationTable} t ON d.`id` = t.`text_id`
WHERE ";
if ($translation === NULL) {
$arg = array();

if ($namespace){
$arg[0] .= 'd.`ns` = ? AND ';
$arg[] = $namespace;
}
$arg[0] = "SELECT t.`translation` FROM {$this->defaultTable} d
JOIN {$this->translationTable} t ON d.`id` = t.`text_id`
WHERE ";

$arg[0] .= 'd.`text` = ? AND t.`lang` = ? AND t.`variant` <= ? ORDER BY t.`variant` DESC';
$arg[] = $original;
$arg[] = $lang;
$arg[] = $variant;
if ($namespace){
$arg[0] .= 'd.`ns` = ? AND ';
$arg[] = $namespace;
}

$arg[0] .= 'BINARY d.`text` = ? AND t.`lang` = ? AND t.`variant` <= ? ORDER BY t.`variant` DESC';
$arg[] = $original;
$arg[] = $lang;
$arg[] = $variant;

$translation = $this->fetchField($arg);
$translation = $this->fetchField($arg);

if (isset($this->cache)) {
$this->cache->save($original.$lang.$variant.$namespace, $translation, array(
\Nette\Caching\Cache::TAGS => array($original.$lang.$namespace)
));
}
}

return $translation ?: NULL;
}
Expand Down Expand Up @@ -95,6 +119,10 @@ public function getAllTranslations($lang, $namespace = NULL)

public function setTranslation($original, $translated, $lang, $variant = 0, $namespace = NULL)
{
if (isset($this->cache)) {
$this->cache->remove($original.$lang.$variant.$namespace);
}

$arg = array();

$arg[0] = "SELECT `id` FROM {$this->defaultTable} WHERE ";
Expand All @@ -104,7 +132,7 @@ public function setTranslation($original, $translated, $lang, $variant = 0, $nam
$arg[] = $namespace;
}

$arg[0] .= "`text` = ?";
$arg[0] .= "BINARY `text` = ?";
$arg[] = $original;

$textId = $this->fetchField($arg);
Expand Down Expand Up @@ -140,6 +168,10 @@ public function setTranslation($original, $translated, $lang, $variant = 0, $nam

public function removeTranslation($original, $lang, $namespace = NULL)
{
if (isset($this->cache)) {
$this->cache->clean(array(\Nette\Caching\Cache::TAGS => $original.$lang.$namespace));
}

$arg = array();

$arg[0] = "SELECT d.`id` FROM {$this->defaultTable} d
Expand Down
83 changes: 68 additions & 15 deletions LiveTranslator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,34 @@ class Translator extends Nette\Object implements Nette\Localization\ITranslator
/** @var Nette\Application\Application */
private $application;

/** @var \Nette\Caching\Cache */
private $cache;


/**
* @param string $defaultLang
* @param ITranslatorStorage $translatorStorage
* @param Nette\Http\Session $session
* @param Nette\Application\Application $application
* @param \Nette\Caching\IStorage $cacheStorage
*/
public function __construct($defaultLang, ITranslatorStorage $translatorStorage, Nette\Http\Session $session, Nette\Application\Application $application)
public function __construct($defaultLang, ITranslatorStorage $translatorStorage, Nette\Http\Session $session, Nette\Application\Application $application, Nette\Caching\IStorage $cacheStorage)
{
$this->setDefaultLang($defaultLang);
$this->translatorStorage = $translatorStorage;
$session->start();
$this->session = $session;
$this->application = $application;

$this->cache = new Nette\Caching\Cache($cacheStorage, 'VladaHejda.LiveTranslator');
}


public function cacheDisable($disable = TRUE)
{
if ($disable) {
$this->cache = NULL;
}
}


Expand Down Expand Up @@ -279,7 +293,7 @@ public function setPresenterLanguageParam($paramName)
* @return string
* @throws TranslatorException
*/
public function translate($string, $count = 1)
public function translate($string, $count = NULL)
{
$hasVariants = FALSE;
if (is_array($string)) {
Expand All @@ -297,6 +311,9 @@ public function translate($string, $count = 1)
unset($args[0]);
$args = array_values($args);

} elseif ($count === NULL) {
$args = NULL;

} else {
$args = array($count);
}
Expand Down Expand Up @@ -355,8 +372,7 @@ public function translate($string, $count = 1)
}

if (!$translated) {
$newStrings = &$this->getNewStrings();
$newStrings[$string] = FALSE;
$this->setNewStringUntranslated($string);

if ($hasVariants) {
if (isset($stringVariants[$plural])) {
Expand All @@ -370,7 +386,7 @@ public function translate($string, $count = 1)
}
}

if (FALSE !== strpos($translated, '%')) {
if ($args !== NULL AND FALSE !== strpos($translated, '%')) {
$tmp = str_replace(array('%label', '%name', '%value'), array('#label', '#name', '#value'), $translated);
if (FALSE !== strpos($tmp, '%')) {
$translated = vsprintf($tmp, $args);
Expand Down Expand Up @@ -413,9 +429,8 @@ public function setTranslation($original, $translated)
}
$original = trim($original);
if ($translated === FALSE) {
$newStrings = &$this->getNewStrings();
$this->translatorStorage->removeTranslation($original, $lang, $this->namespace);
$newStrings[$original] = FALSE;
$this->setNewStringUntranslated($original);
return;
}

Expand All @@ -426,8 +441,7 @@ public function setTranslation($original, $translated)
foreach ($translated as $variant => $string) {
$this->translatorStorage->setTranslation($original, $string, $lang, $variant, $this->namespace);
}
$newStrings = &$this->getNewStrings();
unset($newStrings[$original]);
$this->setNewStringTranslated($original);
}


Expand All @@ -440,14 +454,53 @@ protected function getSessionSection()



protected function &getNewStrings()
protected function saveNewStrings(array $newStrings)
{
if (isset($this->cache)) {
$ns = $this->namespace ?: 'default';
$this->cache->save("newStrings-$ns", $newStrings);

} else {
$section = $this->getSessionSection();
$section->strings = $newStrings;
}
}



protected function setNewStringTranslated($original)
{
// todo mohlo by to mít jednu section a ns by byly jednotlivý property zde
$section = $this->getSessionSection();
if (!isset($section->strings)) {
$section->strings = array();
$newStrings = $this->getNewStrings();
unset($newStrings[$original]);
$this->saveNewStrings($newStrings);
}



protected function setNewStringUntranslated($original)
{
$newStrings = $this->getNewStrings();
$newStrings[$original] = FALSE;
$this->saveNewStrings($newStrings);
}



protected function getNewStrings()
{
if (isset($this->cache)) {
$ns = $this->namespace ?: 'default';
$strings = (array) $this->cache->load("newStrings-$ns");

} else {
// todo mohlo by to mít jednu section a ns by byly jednotlivý property zde
$section = $this->getSessionSection();
if (!isset($section->strings)) {
$section->strings = array();
}
$strings = $section->strings;
}
$strings = &$section->strings;

return $strings;
}

Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ services:
translatorStorage: LiveTranslator\Storage\NetteDatabase(localization_text, localization)
```

*You can rename tables in SQL script (use the same names in config file).*
*You can rename tables in SQL script (use the same names in config file).*
In default `LiveTranslator\Storage\NetteDatabase` uses `Nette\Caching`. For disabling this function use `cacheDisable()` method:
```
services:
translatorStorage:
class: LiveTranslator\Storage\NetteDatabase(localization_text, localization)
setup:
- cacheDisable()
```

- **I am using [Dibi](http://dibiphp.com/)**

Expand Down Expand Up @@ -95,6 +103,16 @@ services:
translatorPanel: LiveTranslator\Panel
```

In default `LiveTranslator\Translator` uses `Nette\Caching` for storage untranslated strings. For disabling caching use `cacheDisable()` method. Then will be use SEESIONS:
```
services:
translator:
class: LiveTranslator\Translator(en)
setup:
- cacheDisable()
...
```


### 3. set up your BasePresenter

Expand Down
2 changes: 1 addition & 1 deletion tests/Panel.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $container = require __DIR__ . '/bootstrap.application.php';

require __DIR__.'/storage/simple.php';

$trans = new \LiveTranslator\Translator('en', new SimpleStorage, $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('en', new SimpleStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));
$panel = new \LiveTranslator\Panel($trans, $container->getService('httpRequest'));

Assert::type('LiveTranslator\Translator', $panel->getTranslator());
Expand Down
8 changes: 4 additions & 4 deletions tests/Translator.exceptions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ $container = require __DIR__ . '/bootstrap.application.php';
require __DIR__.'/storage/dummy.php';

Assert::exception(function() use($container){
new \LiveTranslator\Translator('', new DummyStorage, $container->getService('session'), $container->getService('application'));
new \LiveTranslator\Translator('', new DummyStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));
}, 'LiveTranslator\TranslatorException');

$trans = new \LiveTranslator\Translator('en', new DummyStorage, $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('en', new DummyStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setAvailableLanguages(array(
'en', 'cz'
Expand All @@ -23,15 +23,15 @@ Assert::exception(function() use($trans){
$trans->setDefaultLang('de');
}, 'LiveTranslator\TranslatorException');

$trans = new \LiveTranslator\Translator('de', new DummyStorage(), $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('de', new DummyStorage(), $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

Assert::exception(function() use($trans){
$trans->setAvailableLanguages(array(
'en', 'cz'
));
}, 'LiveTranslator\TranslatorException');

$trans = new \LiveTranslator\Translator('en', new DummyStorage(), $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('en', new DummyStorage(), $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setCurrentLang('de');

Expand Down
2 changes: 1 addition & 1 deletion tests/Translator.getAll.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $container = require __DIR__ . '/bootstrap.application.php';
require __DIR__.'/storage/simple.php';

use \LiveTranslator\Translator as Tr;
$trans = new Tr('en', new SimpleStorage, $container->getService('session'), $container->getService('application'));
$trans = new Tr('en', new SimpleStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setCurrentLang('cz');

Expand Down
2 changes: 1 addition & 1 deletion tests/Translator.langSwitch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $container = require __DIR__ . '/bootstrap.application.php';
require __DIR__.'/storage/language.php';

use \LiveTranslator\Translator as Tr;
$trans = new Tr('en', new LanguageStorage, $container->getService('session'), $container->getService('application'));
$trans = new Tr('en', new LanguageStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setCurrentLang('cz');

Expand Down
2 changes: 1 addition & 1 deletion tests/Translator.minimum.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ $container = require __DIR__ . '/bootstrap.application.php';

require __DIR__.'/storage/dummy.php';

$trans = new \LiveTranslator\Translator('en', new DummyStorage, $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('en', new DummyStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

Assert::equal('hello', $trans->translate('hello'));
2 changes: 1 addition & 1 deletion tests/Translator.namespaces.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $container = require __DIR__ . '/bootstrap.application.php';
require __DIR__.'/storage/namespace.php';

use \LiveTranslator\Translator as Tr;
$trans = new Tr('en', new NamespaceStorage, $container->getService('session'), $container->getService('application'));
$trans = new Tr('en', new NamespaceStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setCurrentLang('cz')
->setNamespace('first');
Expand Down
5 changes: 4 additions & 1 deletion tests/Translator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $container = require __DIR__ . '/bootstrap.application.php';

require __DIR__.'/storage/simple.php';

$trans = new \LiveTranslator\Translator('en', new SimpleStorage, $container->getService('session'), $container->getService('application'));
$trans = new \LiveTranslator\Translator('en', new SimpleStorage, $container->getService('session'), $container->getService('application'), $container->getService('cache.storage'));

$trans->setCurrentLang('cz')
->setAvailableLanguages(array(
Expand All @@ -31,6 +31,9 @@ Assert::equal('jména Johnny, George', $trans->translate(array('name %s'), 'Joh
Assert::equal('Woohoo křičí 1 muž.', $trans->translate(array('%2$d man screams %1$s.'), 'Woohoo', 1));
Assert::equal('2 muži křičí "Woohoo!".', $trans->translate(array('%2$d man screams %1$s.'), 'Woohoo', 2));
Assert::equal('Woohoo křičí 5 mužů.', $trans->translate(array('%2$d man screams %1$s.'), 'Woohoo', 5));
Assert::equal('Tax is 21%', $trans->translate('Tax is 21%'));
Assert::equal('Tax is 21% or 15%', $trans->translate('Tax is 21% or 15%'));
Assert::equal('50%done', $trans->translate('50%done'));

$trans->setCurrentLang('en');

Expand Down
Loading