Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
Fist development version of LocoBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
cmodijk committed Sep 19, 2013
0 parents commit 55d7612
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Command/DownloadCommand.php
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();
}
}
56 changes: 56 additions & 0 deletions DependencyInjection/Configuration.php
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;
}
}
30 changes: 30 additions & 0 deletions DependencyInjection/JcidLocoExtension.php
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");
}
}
9 changes: 9 additions & 0 deletions JcidLocoBundle.php
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
{
}
34 changes: 34 additions & 0 deletions Loco/Downloader.php
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();
}
}
}
}
29 changes: 29 additions & 0 deletions Resources/config/services.xml
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>
35 changes: 35 additions & 0 deletions Translation/PhpsFileLoader.php
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;
}
}

0 comments on commit 55d7612

Please sign in to comment.