This repository has been archived by the owner on Jun 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fist development version of LocoBundle
- Loading branch information
0 parents
commit 55d7612
Showing
7 changed files
with
218 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class DownloadCommand extends ContainerAwareCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName("translation:loco:download") | ||
->setDescription("Download laatste loco translation files"); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->getContainer()->get("jcid_loco.downloader")->download(); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* This is the class that validates and merges configuration from your app/config files | ||
* | ||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root("jcid_loco"); | ||
|
||
$treeBuilder->root("jcid_loco") | ||
->children() | ||
|
||
// Config the locales to download | ||
->arrayNode("locales") | ||
->isRequired() | ||
->requiresAtLeastOneElement() | ||
->useAttributeAsKey("name") | ||
->prototype("scalar")->end() | ||
->end() | ||
|
||
// Config the locales to download | ||
->arrayNode("domains") | ||
->isRequired() | ||
->requiresAtLeastOneElement() | ||
->prototype("scalar")->end() | ||
->end() | ||
|
||
// Target dir | ||
->scalarNode("target") | ||
->defaultValue("%kernel.root_dir%/Resources/translations") | ||
->end() | ||
|
||
// API Key | ||
->scalarNode("key") | ||
->isRequired() | ||
->end() | ||
|
||
->end() | ||
->end(); | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
class JcidLocoExtension extends Extension | ||
{ | ||
/** | ||
* Handles the knp_menu configuration. | ||
* | ||
* @param array $configs The configurations being loaded | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = $this->getConfiguration($configs, $container); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
foreach ($config as $key => $value) { | ||
$container->setParameter("jcid_loco.configuration.".$key, $value); | ||
} | ||
|
||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__."/../Resources/config")); | ||
$loader->load("services.xml"); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class JcidLocoBundle extends Bundle | ||
{ | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle\Loco; | ||
|
||
use Guzzle\Http\Client; | ||
|
||
class Downloader | ||
{ | ||
private $locales; | ||
private $domains; | ||
private $target; | ||
private $client; | ||
|
||
public function __construct($locales, $domains, $target, $key) | ||
{ | ||
$this->locales = $locales; | ||
$this->domains = $domains; | ||
$this->target = $target; | ||
|
||
$this->client = new Client("https://localise.biz/api/"); | ||
$this->client->setDefaultOption("query/key", $key); | ||
} | ||
|
||
public function download() | ||
{ | ||
foreach ($this->locales as $localeKey => $localeValue) { | ||
foreach ($this->domains as $domain) { | ||
$response = $this->client->get(sprintf("export/locale/%s.phps?format=symfony&index=id&filter=%s", $localeValue, $domain), array(), array( | ||
"save_to" => sprintf("%s/%s.%s.phps", $this->target, $domain, $localeKey), | ||
))->send(); | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
|
||
<parameter key="jcid_loco.downloader.class">Jcid\Bundle\LocoBundle\Loco\Downloader</parameter> | ||
<parameter key="jcid_loco.translation.phps.class">Jcid\Bundle\LocoBundle\Translation\PhpsFileLoader</parameter> | ||
|
||
</parameters> | ||
|
||
<services> | ||
|
||
<service id="jcid_loco.downloader" class="%jcid_loco.downloader.class%"> | ||
<argument>%jcid_loco.configuration.locales%</argument> | ||
<argument>%jcid_loco.configuration.domains%</argument> | ||
<argument>%jcid_loco.configuration.target%</argument> | ||
<argument>%jcid_loco.configuration.key%</argument> | ||
</service> | ||
|
||
<service id="jcid_loco.translation.phps" class="%jcid_loco.translation.phps.class%"> | ||
<tag name="translation.loader" alias="phps" /> | ||
</service> | ||
|
||
</services> | ||
|
||
</container> |
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,35 @@ | ||
<?php | ||
|
||
namespace Jcid\Bundle\LocoBundle\Translation; | ||
|
||
use Symfony\Component\Translation\Loader\ArrayLoader; | ||
use Symfony\Component\Translation\Loader\LoaderInterface; | ||
use Symfony\Component\Translation\Exception\InvalidResourceException; | ||
use Symfony\Component\Translation\Exception\NotFoundResourceException; | ||
use Symfony\Component\Config\Resource\FileResource; | ||
|
||
class PhpsFileLoader extends ArrayLoader implements LoaderInterface | ||
{ | ||
public function load($resource, $locale, $domain = "messages") | ||
{ | ||
if (!stream_is_local($resource)) { | ||
throw new InvalidResourceException(sprintf("This is not a local file '%s'.", $resource)); | ||
} | ||
|
||
if (!file_exists($resource)) { | ||
throw new NotFoundResourceException(sprintf("File '%s' not found.", $resource)); | ||
} | ||
|
||
$messages = require($resource); | ||
|
||
$messages = array_filter($messages); | ||
array_walk($messages, function(&$param) { | ||
$param = stripslashes(str_replace(array("\\|", "\\\\|"), "|", $param)); | ||
}); | ||
|
||
$catalogue = parent::load($messages, $locale, $domain); | ||
$catalogue->addResource(new FileResource($resource)); | ||
|
||
return $catalogue; | ||
} | ||
} |