-
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
7 changed files
with
280 additions
and
13 deletions.
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
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
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
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,27 @@ | ||
<?php | ||
|
||
/** | ||
* Melis Technology (http://www.melistechnology.com) | ||
* | ||
* @copyright Copyright (c) 2018 Melis Technology (http://www.melistechnology.com) | ||
* | ||
*/ | ||
|
||
namespace MelisFront\Service\Factory; | ||
|
||
use MelisFront\Service\MelisTranslationService; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Zend\ServiceManager\FactoryInterface; | ||
|
||
class MelisTranslationServiceFactory implements FactoryInterface | ||
{ | ||
public function createService(ServiceLocatorInterface $sl) | ||
{ | ||
$moduleSvc = $sl->get('MelisAssetManagerModulesService'); | ||
$melisTranslationService = new MelisTranslationService($moduleSvc); | ||
$melisTranslationService->setServiceLocator($sl); | ||
|
||
return $melisTranslationService; | ||
} | ||
|
||
} |
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,154 @@ | ||
<?php | ||
|
||
/** | ||
* Melis Technology (http://www.melistechnology.com) | ||
* | ||
* @copyright Copyright (c) 2020 Melis Technology (http://www.melistechnology.com) | ||
* | ||
*/ | ||
|
||
namespace MelisFront\Service; | ||
|
||
use MelisAssetManager\Service\MelisModulesService; | ||
use MelisEngine\Service\MelisEngineGeneralService; | ||
use Zend\Session\Container; | ||
|
||
class MelisTranslationService extends MelisEngineGeneralService | ||
{ | ||
/** @var MelisModulesService */ | ||
private $moduleSvc; | ||
|
||
public function __construct(MelisModulesService $moduleService) | ||
{ | ||
$this->moduleSvc = $moduleService; | ||
} | ||
|
||
/** | ||
* | ||
* get all module translations by locale | ||
* | ||
* @param string $locale | ||
* @return array | ||
*/ | ||
public function getTranslationsByLocale($locale = "en_EN") | ||
{ | ||
// Event parameters prepare | ||
$arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); | ||
// Sending service start event | ||
$arrayParameters = $this->sendEvent('melis_translation_get_trans_by_locale_start', $arrayParameters); | ||
$transMessages = []; | ||
$tmpTrans = []; | ||
$modules = $this->moduleSvc->getAllModules(); | ||
$locale = $arrayParameters['locale']; | ||
$moduleFolders = []; | ||
// get modules path | ||
foreach ($modules as $module) | ||
{ | ||
array_push($moduleFolders, $this->moduleSvc->getModulePath($module)); | ||
} | ||
|
||
$transFiles = array( | ||
$locale.'.interface.php', | ||
$locale.'.forms.php', | ||
); | ||
$insideDirTrans = []; | ||
set_time_limit(0); | ||
foreach($moduleFolders as $module) { | ||
if(file_exists($module.'/language')) { | ||
foreach($transFiles as $file) { | ||
if(file_exists($module.'/language/'.$file)) { | ||
$tmpTrans[] = include($module.'/language/'.$file); | ||
} | ||
} | ||
// get the directory | ||
$iterator = new \RecursiveDirectoryIterator($module . "/language", \RecursiveDirectoryIterator::SKIP_DOTS); | ||
$files = new \RecursiveIteratorIterator($iterator,\RecursiveIteratorIterator::CHILD_FIRST); | ||
/** @var \SplFileInfo $file */ | ||
// get the files under the directory | ||
foreach($files as $file) { | ||
if (stristr($file->getBasename(),$locale)){ | ||
// get the translation based on locale | ||
$tmpTrans[]= include $file->getFileInfo()->getPathname(); | ||
} else if (stristr($file->getBasename(),"en_EN")){ | ||
// fall back locale | ||
$tmpTrans[] = include $file->getFileInfo()->getPathname(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
if($tmpTrans) { | ||
foreach($tmpTrans as $tmpIdx => $transKey) { | ||
foreach($transKey as $key => $value) { | ||
$transMessages[$key] = $value; | ||
} | ||
} | ||
} | ||
// results | ||
$arrayParameters['results'] = $transMessages; | ||
// send event | ||
$arrayParameters = $this->sendEvent('melis_translation_get_trans_by_locale_end', $arrayParameters); | ||
|
||
return $arrayParameters['results']; | ||
|
||
} | ||
|
||
/**+ | ||
* | ||
* | ||
* get translations by key and locale | ||
* | ||
* @param $translationKey | ||
* @param string $locale | ||
* @return mixed | ||
*/ | ||
public function translateByLocale($translationKey, $locale = "en_EN") | ||
{ | ||
// Event parameters prepare | ||
$arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); | ||
// Sending service start event | ||
$arrayParameters = $this->sendEvent('trans_by_locale_start', $arrayParameters); | ||
$text = $translationKey; | ||
// check translation key in the translations | ||
$translations = $this->getTranslationsByLocale($locale); | ||
if (array_key_exists($translationKey, $translations)) { | ||
$text = $translations[$translationKey]; | ||
} | ||
|
||
// results | ||
$arrayParameters['results'] = $text; | ||
// send event | ||
$arrayParameters = $this->sendEvent('trans_by_locale_end', $arrayParameters); | ||
|
||
return $arrayParameters['results']; | ||
} | ||
|
||
/** | ||
* translate translationkey based from Back-office locale | ||
* @param $translationKey | ||
* @return mixed | ||
*/ | ||
public function boTranslate($translationKey) | ||
{ | ||
// Event parameters prepare | ||
$arrayParameters = $this->makeArrayFromParameters(__METHOD__, func_get_args()); | ||
// Sending service start event | ||
$arrayParameters = $this->sendEvent('bo_translate_start', $arrayParameters); | ||
$text = $translationKey; | ||
// get bo locale | ||
$melisBoContainer = new Container('meliscore'); | ||
// check translation key in the translations | ||
$translations = $this->getTranslationsByLocale($melisBoContainer['melis-lang-locale']); | ||
if (array_key_exists($translationKey, $translations)) { | ||
$text = $translations[$translationKey]; | ||
} | ||
|
||
// results | ||
$arrayParameters['results'] = $text; | ||
// send event | ||
$arrayParameters = $this->sendEvent('bo_translate_end', $arrayParameters); | ||
|
||
return $arrayParameters['results']; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
/** | ||
* Melis Technology (http://www.melistechnology.com) | ||
* | ||
* @copyright Copyright (c) 2019 Melis Technology (http://www.melistechnology.com) | ||
* | ||
*/ | ||
|
||
namespace MelisFront\View\Helper\Factory; | ||
|
||
use MelisFront\View\Helper\MelisTranslationHelper; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Zend\ServiceManager\FactoryInterface; | ||
|
||
class MelisTranslationHelperFactory implements FactoryInterface | ||
{ | ||
/** | ||
* @param ServiceLocatorInterface $sl | ||
* @return MelisTranslationHelper|mixed | ||
*/ | ||
public function createService(ServiceLocatorInterface $sl) | ||
{ | ||
return new MelisTranslationHelper($sl->getServiceLocator()); | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace MelisFront\View\Helper; | ||
|
||
use MelisEngine\Service\MelisPageService; | ||
use MelisFront\Service\MelisTranslationService; | ||
use Zend\Session\Container; | ||
use Zend\View\Helper\AbstractHelper; | ||
|
||
class MelisTranslationHelper extends AbstractHelper | ||
{ | ||
public $serviceManager; | ||
|
||
public function __construct($sm) | ||
{ | ||
$this->serviceManager = $sm; | ||
} | ||
|
||
public function __invoke($translationKey , $locale = null) | ||
{ | ||
$text = ""; | ||
// melis translation view helper | ||
/** @var MelisTranslationService $melisTrans */ | ||
$melisTrans = $this->serviceManager->get('MelisTranslationService'); | ||
if (! empty($locale)) { | ||
$text = $melisTrans->translateByLocale($translationKey,$locale); | ||
} else { | ||
// get melis back office locale | ||
$melisCoreLang = new Container('meliscore'); | ||
// get translation | ||
$text = $melisTrans->translateByLocale($translationKey,$melisCoreLang['melis-lang-locale']); | ||
} | ||
|
||
return $text; | ||
} | ||
|
||
} |