Skip to content

Commit

Permalink
Add translations checking functionality (Cocolabs-SAS#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
humenetskyifr authored and cocolabssas committed Jan 10, 2019
1 parent e8ec167 commit b7022e0
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ parameters:
cocorico.admin: admin
#Admin Web JMS translations activation
cocorico.admin_translation: false
#To check if site texts are translatable
cocorico.check_translation: false

#Microsoft Translator key
cocorico.translator.secret.key: ~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{{ (object.getValue() / 100) | format_price(app.request.locale, 2) }}
{% elseif object.getType() == 'percent' %}
{{ object.getValue() * 100 }} %
{% elseif object.getType() == 'checkbox' %}
{{ object.getValue() ? true : false }}
{% else %}
{{ object.getValue() }}
{% endif %}
Expand Down
3 changes: 1 addition & 2 deletions src/Cocorico/CoreBundle/Admin/ListingAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ function ($num) {
array(
'translation_domain' => 'cocorico_listing_deposit',
)
)
->end();
);
}

$formMapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ services:
arguments:
- "%kernel.bundles%"
tags:
- { name: twig.extension }
- { name: twig.extension }

#Override trans filters. Don't change service name
twig.extension.trans:
class: Cocorico\CoreBundle\Twig\TranslationExtension
public: false
arguments:
- '@translator'
calls:
- [setCheckTranslation, ["%cocorico.check_translation%"]]
tags:
- { name: twig.extension }
2 changes: 1 addition & 1 deletion src/Cocorico/CoreBundle/Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jms_translation:
dirs: ["%kernel.root_dir%", "%kernel.root_dir%/../src"]
output_dir: "%kernel.root_dir%/Resources/translations"
# ignored_domains: [routes]
excluded_names: ["*TestCase.php", "*Test.php"]
excluded_names: ["*TestCase.php", "*Test.php", "TranslationExtension.php"]
excluded_dirs: [cache, data, logs, Tests]
extractors: [jms_i18n_routing, cocorico_breadcrumbs, sonata_admin ]

Expand Down
6 changes: 5 additions & 1 deletion src/Cocorico/CoreBundle/Resources/config/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ parameters:
#BookingDepositRefund : none or Cocorico\ListingDepositBundle\Entity\BookingDepositRefund (without quotes!)
cocorico_listing_deposit.booking_deposit_refund.entity_class: none

#Config
#Config. types taken into account for now are percent, price, checkbox
cocorico_config.parameters_allowed:
cocorico.fee_as_asker:
type: 'percent'
cocorico.fee_as_offerer:
type: 'percent'
cocorico.check_translation:
type: 'text'

#User
#Is there a delivery address. Should not be enabled if DeliveryBundle is enabled.
Expand Down Expand Up @@ -309,6 +311,8 @@ parameters:
cocorico.admin: ~
#Admin Web JMS translations activation
cocorico.admin_translation: ~
#To check if site texts are translatable
cocorico.check_translation: ~

#translator key
cocorico.translator.secret.key: ~
Expand Down
110 changes: 110 additions & 0 deletions src/Cocorico/CoreBundle/Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/*
* This file is part of the Cocorico package.
*
* (c) Cocolabs SAS <contact@cocolabs.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocorico\CoreBundle\Twig;


use Symfony\Bridge\Twig\Extension\TranslationExtension as BaseTranslationExtension;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class TranslationExtension
*
* Override trans filter by adding suffix to translations to know if a text has been translated through web interface
*
* @package Cocorico\CoreBundle\Twig
*/
class TranslationExtension extends BaseTranslationExtension
{
private $checkTranslation;

const TRANS_SUFFIX = '';

public function __construct(
TranslatorInterface $translator,
\Twig_NodeVisitorInterface $translationNodeVisitor = null
) {
parent::__construct($translator, $translationNodeVisitor);
}

public function setCheckTranslation($checkTranslation)
{
$this->checkTranslation = $checkTranslation;
}

/**
* {@inheritdoc}
*/
public function getFilters()
{
if ($this->checkTranslation) {
return array(
new \Twig_SimpleFilter('trans', array($this, 'transOverride')),
new \Twig_SimpleFilter('transchoice', array($this, 'transchoiceOverride')),
);
}

return parent::getFilters();
}

public function transOverride($id, array $parameters = array(), $domain = null, $locale = null)
{
if (null === $locale) {
$locale = $this->getTranslator()->getLocale();
}

if (null === $domain) {
$domain = 'messages';
}

if ('messages' !== $domain && false === $this->translationExists($id, $domain, $locale)) {
$domain = 'messages';
}

return $this->getTranslator()->trans($id, $parameters, $domain, $locale) . self::TRANS_SUFFIX;
}

public function transchoiceOverride($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
if (null === $locale) {
$locale = $this->getTranslator()->getLocale();
}

if (null === $domain) {
$domain = 'messages';
}

if ('messages' !== $domain && false === $this->translationExists($id, $domain, $locale)) {
$domain = 'messages';
}

return $this->getTranslator()->transChoice(
$id,
$number,
array_merge(array('%count%' => $number), $parameters),
$domain,
$locale
) . self::TRANS_SUFFIX;
}

protected function translationExists($id, $domain, $locale)
{
return $this->getTranslator()->getCatalogue($locale)->has((string)$id, $domain);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'translator';
}
}
4 changes: 2 additions & 2 deletions web/css/all-override.css
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ li.time-fields {
}

.time-fields .jcf-select, .nb-days .jcf-select {
width: 50px;
width: 54px;
margin-left: 3px;
}

Expand All @@ -369,7 +369,7 @@ li.time-fields {
}

.time-fields .jcf-select-text, .nb-days .jcf-select-text {
width: 20px;
width: 24px;
}

.jcf-select-no-scroll .jcf-scrollbar {
Expand Down
8 changes: 6 additions & 2 deletions web/css/all.css
Original file line number Diff line number Diff line change
Expand Up @@ -7080,7 +7080,7 @@ span.btn:focus {

.date-selection .col {
float: left;
width: 48%;
width: 48.7%;
position: relative;
}

Expand Down Expand Up @@ -8637,7 +8637,11 @@ label {
}

.nav-tabs > li {
margin-right: 6px;
margin-right: 5px;
}

.nav-tabs > li:last-child {
margin-right: 0;
}

.nav-tabs > li > a {
Expand Down

0 comments on commit b7022e0

Please sign in to comment.