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

Added some features needed, when changing translation in production mode. #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Kdyby/Translation/CatalogueCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class CatalogueCompiler extends Nette\Object
{

const TAG_TRANSLATION = 'Kdyby\\Translation\\Translator';

/**
* @var \Nette\Caching\Cache
*/
Expand All @@ -39,11 +41,16 @@ class CatalogueCompiler extends Nette\Object
*/
private $catalogueFactory;

/**
* @var Nette\Caching\IStorage
*/
private $cacheStorage;


public function __construct(Nette\Caching\IStorage $cacheStorage, FallbackResolver $fallbackResolver,
CatalogueFactory $catalogueFactory)
{
$this->cacheStorage = $cacheStorage;
$this->cache = new Cache($cacheStorage, 'Kdyby\\Translation\\Translator');
$this->fallbackResolver = $fallbackResolver;
$this->catalogueFactory = $catalogueFactory;
Expand All @@ -60,10 +67,22 @@ public function enableDebugMode()
}


/**
* Replaces cache storage with simple memory storage (per-request).
*/
public function disableDebugMode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tohle je realne na něco potřeba?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ano, potřebuji to tam.
Když není zapnutý debug mód, tak TranslationWriter nemá načtené resources.
Tedy musím zavolat enableDebugMode, aby měl TranslatonWriter resources, do kterých může zapsat.
Jenže pak se změna neprojeví u cache, jelikož mám zapnutou MemoryStorage a pro ostatní uživatele je stále klasickáPhpArrayStorage. Takže pokud chci, aby se automaticky projevily překlady i bez debug mode, musím těsně před zápisem přehodit zpět na persistentní storage (právě metodou disableDebugMode). Pak se překlady v celé aplikaci projeví hned po změně a nemusím kvůli tomu mazat cache celé aplikace.
A práci máme vyloženě na tohle usecase, když si klient překládá sám a z formu to cpeme do neon souborů a chceme, klient má production mode a chce, aby se překlady projevily hned.

{
$this->cache = new Cache($this->cacheStorage, 'Kdyby\\Translation\\Translator');
}


public function invalidateCache()
public function invalidateCache($onlyTranslation = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tohle bych vubec nedělal, raději udělal mazani kdyz je filecache tak maznout složku, jinak maznout podle tagu... mazat všechno nechceš nikdy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proč bys to vůbec nedělal? Nechtěl jsem ti rozbít stávající chování (proto nepovinný parametr), a nevím, kde tam invaliduješ celou cache. Jestli nikdy nechceš mazat všechno, tak co tam ta metoda dělá?
A proč nemazat vždy podle tagu? Proč u FileCache sahat přímo na složku?

{
$this->cache->clean(array(Cache::ALL => TRUE));
if ($onlyTranslation) {
$this->cache->clean(array(Cache::TAGS => static::TAG_TRANSLATION));
} else {
$this->cache->clean(array(Cache::ALL => TRUE));
}
}


Expand Down Expand Up @@ -106,7 +125,7 @@ public function compile(Translator $translator, array &$availableCatalogues, $lo
}

$this->catalogueFactory->createCatalogue($translator, $availableCatalogues, $locale);
$this->cache->save($cacheKey, $availableCatalogues[$locale]->all());
$this->cache->save($cacheKey, $availableCatalogues[$locale]->all(), array(Cache::TAGS => array(static::TAG_TRANSLATION)));
return $availableCatalogues;
}

Expand All @@ -115,7 +134,8 @@ public function compile(Translator $translator, array &$availableCatalogues, $lo
$cached = $compiled = $this->cache->load($cacheKey);
if ($compiled === NULL) {
$this->catalogueFactory->createCatalogue($translator, $availableCatalogues, $locale);
$this->cache->save($cacheKey, $compiled = $this->compilePhpCache($translator, $availableCatalogues, $locale));
$this->cache->save($cacheKey, $compiled = $this->compilePhpCache($translator, $availableCatalogues, $locale),
array(Cache::TAGS => array(static::TAG_TRANSLATION)));
$cached = $this->cache->load($cacheKey);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/php.ini-win
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[PHP]
extension_dir = "./ext"
extension=php_pdo_sqlite.dll
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jak tohle souvisi s timto pullrq?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, tak, že nové nette má jako defaultní cache sqlite storage a když chci pustit testy na windows, tak se to bez tohoto neobejde. Mám to vyhodit?