Skip to content

Commit

Permalink
Merge pull request #2 from experius/js-translation-feature
Browse files Browse the repository at this point in the history
[FEATURE] Js translation feature added and cronjobs are now configurable
  • Loading branch information
lewisvoncken authored Feb 26, 2018
2 parents 4b25640 + f7aad8e commit 2679adf
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 204 deletions.
23 changes: 21 additions & 2 deletions Controller/Adminhtml/Translation/InlineEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,37 @@
*/
class InlineEdit extends \Magento\Backend\App\Action
{

/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
protected $jsonFactory;

/**
* @var \Experius\MissingTranslations\Model\TranslationFactory
*/
protected $translationFactory;

/**
* @var \Experius\MissingTranslations\Helper\Data
*/
protected $helper;
/**
* InlineEdit constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
* @param \Experius\MissingTranslations\Model\TranslationFactory $translationFactory
* @param \Experius\MissingTranslations\Helper\Data $helper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
\Experius\MissingTranslations\Model\TranslationFactory $translationFactory
\Experius\MissingTranslations\Model\TranslationFactory $translationFactory,
\Experius\MissingTranslations\Helper\Data $helper
) {
parent::__construct($context);
$this->jsonFactory = $jsonFactory;
$this->translationFactory = $translationFactory;
$this->helper = $helper;
}

/**
Expand Down Expand Up @@ -75,6 +91,9 @@ public function execute()
try {
$model->setData(array_merge($data, $postItems[$modelId]));
$model->save();

$this->helper->updateJsTranslationJsonFiles($data['locale']);

} catch (\Exception $e) {
$messages[] = "[Translation ID: {$modelId}] {$e->getMessage()}";
$error = true;
Expand Down
19 changes: 18 additions & 1 deletion Controller/Adminhtml/Translation/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,38 @@ class Save extends \Magento\Backend\App\Action
*/
protected $phrases;

/**
* @var \Magento\Translation\Model\Inline\CacheManager
*/
protected $cacheManager;

/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $localeResolver;

/**
* Save constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
* @param \Experius\MissingTranslations\Model\TranslationFactory $translationFactory
* @param \Experius\MissingTranslations\Helper\Data $helper
* @param \Magento\Backend\Model\Auth\Session $authSession
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
\Experius\MissingTranslations\Model\TranslationFactory $translationFactory,
\Experius\MissingTranslations\Helper\Data $helper,
\Magento\Backend\Model\Auth\Session $authSession
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Framework\Locale\ResolverInterface $localeResolver
) {
$this->dataPersistor = $dataPersistor;
$this->translationFactory = $translationFactory;
$this->helper = $helper;
$this->authSession = $authSession;
$this->localeResolver = $localeResolver;

parent::__construct($context);
}
Expand Down Expand Up @@ -129,6 +144,8 @@ public function execute()
$this->messageManager->addSuccess(__('You saved the Translation.'));
$this->dataPersistor->clear('experius_missingtranslations_translation');

$this->helper->updateJsTranslationJsonFiles($data['locale']);

if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['key_id' => $model->getId()]);
}
Expand Down
184 changes: 101 additions & 83 deletions Cron/Collect.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,102 @@
<?php
/**
* A Magento 2 module named Experius/MissingTranslations
* Copyright (C) 2018 Experius
*
* This file is part of Experius/MissingTranslations.
*
* Experius/MissingTranslations is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Experius\MissingTranslations\Cron;

/**
* Class Collect
* @package Experius\MissingTranslations\Cron
*/
class Collect
{
/**
* @var \Experius\MissingTranslations\Model\Config\Source\Locale
*/
protected $localeSourceModel;

/**
* @var \Experius\MissingTranslations\Model\TranslationCollector
*/
protected $translationCollector;

public function __construct(
\Experius\MissingTranslations\Model\Config\Source\Locale $locale,
\Experius\MissingTranslations\Model\TranslationCollector $translationCollector
) {
$this->localeSourceModel = $locale;
$this->translationCollector = $translationCollector;
}

/**
* Executes collect cron, inserting all translations for active locales into the database on global scope
*/
public function existingTranslations()
{
/** Global scope only for now */
$storeId = 0;
$locales = $this->localeSourceModel->getLocaleMapping();

foreach ($locales as $locale => $localeOptionArray) {
$this->translationCollector->updateTranslationDatabase(
$storeId,
$locale,
\Experius\MissingTranslations\Model\TranslationCollector::TRANSLATION_TYPE_EXISTING
);
}
}

/**
* Insert all missing translations found in missing translation files
* for active locales into the database on global scope
*/
public function missingTranslations()
{
/** Global scope only for now */
$storeId = 0;
$locales = $this->localeSourceModel->getLocaleMapping();

foreach ($locales as $locale => $localeOptionArray) {
$this->translationCollector->updateTranslationDatabase(
$storeId,
$locale,
\Experius\MissingTranslations\Model\TranslationCollector::TRANSLATION_TYPE_MISSING
);
}
}
<?php
/**
* A Magento 2 module named Experius/MissingTranslations
* Copyright (C) 2018 Experius
*
* This file is part of Experius/MissingTranslations.
*
* Experius/MissingTranslations is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Experius\MissingTranslations\Cron;

/**
* Class Collect
* @package Experius\MissingTranslations\Cron
*/
class Collect
{
const XML_PATH_EXISTING_TRANSLATIONS_CRON = 'general/locale/cron_existing_translations';
const XML_PATH_MISSING_TRANSLATIONS_CRON = 'general/locale/cron_missing_translations';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var \Experius\MissingTranslations\Model\Config\Source\Locale
*/
protected $localeSourceModel;

/**
* @var \Experius\MissingTranslations\Model\TranslationCollector
*/
protected $translationCollector;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Experius\MissingTranslations\Model\Config\Source\Locale $locale,
\Experius\MissingTranslations\Model\TranslationCollector $translationCollector
) {
$this->scopeConfig = $scopeConfig;
$this->localeSourceModel = $locale;
$this->translationCollector = $translationCollector;
}

/**
* Executes collect cron, inserting all translations for active locales into the database on global scope
*/
public function existingTranslations()
{
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_EXISTING_TRANSLATIONS_CRON)) {
return;
}

/** Global scope only for now */
$storeId = 0;
$locales = $this->localeSourceModel->getLocaleMapping();

foreach ($locales as $locale => $localeOptionArray) {
$this->translationCollector->updateTranslationDatabase(
$storeId,
$locale,
\Experius\MissingTranslations\Model\TranslationCollector::TRANSLATION_TYPE_EXISTING
);
}
}

/**
* Insert all missing translations found in missing translation files
* for active locales into the database on global scope
*/
public function missingTranslations()
{
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_MISSING_TRANSLATIONS_CRON)) {
return;
}

/** Global scope only for now */
$storeId = 0;
$locales = $this->localeSourceModel->getLocaleMapping();

foreach ($locales as $locale => $localeOptionArray) {
$this->translationCollector->updateTranslationDatabase(
$storeId,
$locale,
\Experius\MissingTranslations\Model\TranslationCollector::TRANSLATION_TYPE_MISSING
);
}
}
}
Loading

0 comments on commit 2679adf

Please sign in to comment.