diff --git a/composer.json b/composer.json index 99ec7264ad..ecfdedc3b4 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,9 @@ "symfony/twig-bundle": "^4.3", "symfony/validator": "^4.3", "twig/extensions": "^1.5", - "twig/twig": "^2.10" + "twig/extra-bundle": "^3.0", + "twig/intl-extra": "^3.0", + "twig/twig": "^2.12" }, "conflict": { "doctrine/doctrine-bundle": ">=3", @@ -66,7 +68,6 @@ "require-dev": { "jms/translation-bundle": "^1.4", "matthiasnoback/symfony-dependency-injection-test": "^4.1", - "sonata-project/intl-bundle": "^2.4", "symfony/browser-kit": "^4.3", "symfony/css-selector": "^4.3", "symfony/filesystem": "^4.3", @@ -76,9 +77,7 @@ }, "suggest": { "jms/translation-bundle": "Extract message keys from Admins", - "kunicmarko/sonata-auto-configure-bundle": "Auto configures Admin classes", - "sensio/generator-bundle": "Add sonata:admin:generate command", - "sonata-project/intl-bundle": "Add localized date and number into the list" + "kunicmarko/sonata-auto-configure-bundle": "Auto configures Admin classes" }, "config": { "sort-packages": true diff --git a/docs/index.rst b/docs/index.rst index fd4f8007f4..73bbc8e2b5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -52,6 +52,7 @@ The demo website can be found at http://demo.sonata-project.org. reference/console reference/troubleshooting reference/breadcrumbs + reference/internationalization .. toctree:: :caption: Advanced Options diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 8cda513b34..b44496e631 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -248,3 +248,6 @@ Full Configuration Options global_search: show_empty_boxes: show case_sensitive: true + + # show localized information + use_intl_templates: false diff --git a/docs/reference/internationalization.rst b/docs/reference/internationalization.rst new file mode 100644 index 0000000000..630496f4d4 --- /dev/null +++ b/docs/reference/internationalization.rst @@ -0,0 +1,18 @@ +Internationalization (I18N) +=========================== + +The admin comes with the ``use_intl_templates`` option that will load a set of templates that will +display localized information. + + +Configuration +------------- + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/sonata_admin.yaml + + sonata_admin: + use_intl_templates: true diff --git a/src/DependencyInjection/AbstractSonataAdminExtension.php b/src/DependencyInjection/AbstractSonataAdminExtension.php index 66b53e2b78..198b4da47b 100644 --- a/src/DependencyInjection/AbstractSonataAdminExtension.php +++ b/src/DependencyInjection/AbstractSonataAdminExtension.php @@ -78,29 +78,28 @@ protected function fixTemplatesConfiguration( ], ]; - // let's add some magic, only overwrite template if the SonataIntlBundle is enabled - $bundles = $container->getParameter('kernel.bundles'); - if (isset($bundles['SonataIntlBundle'])) { + $useIntlTemplates = $container->getParameter('sonata.admin.configuration.use_intl_templates'); + if ($useIntlTemplates) { $defaultConfig['templates']['types']['list'] = array_merge($defaultConfig['templates']['types']['list'], [ - 'date' => '@SonataIntl/CRUD/list_date.html.twig', - 'datetime' => '@SonataIntl/CRUD/list_datetime.html.twig', - 'smallint' => '@SonataIntl/CRUD/list_decimal.html.twig', - 'bigint' => '@SonataIntl/CRUD/list_decimal.html.twig', - 'integer' => '@SonataIntl/CRUD/list_decimal.html.twig', - 'decimal' => '@SonataIntl/CRUD/list_decimal.html.twig', - 'currency' => '@SonataIntl/CRUD/list_currency.html.twig', - 'percent' => '@SonataIntl/CRUD/list_percent.html.twig', + 'date' => '@SonataAdmin/CRUD/Intl/list_date.html.twig', + 'datetime' => '@SonataAdmin/CRUD/Intl/list_datetime.html.twig', + 'smallint' => '@SonataAdmin/CRUD/Intl/list_decimal.html.twig', + 'bigint' => '@SonataAdmin/CRUD/Intl/list_decimal.html.twig', + 'integer' => '@SonataAdmin/CRUD/Intl/list_decimal.html.twig', + 'decimal' => '@SonataAdmin/CRUD/Intl/list_decimal.html.twig', + 'currency' => '@SonataAdmin/CRUD/Intl/list_currency.html.twig', + 'percent' => '@SonataAdmin/CRUD/Intl/list_percent.html.twig', ]); $defaultConfig['templates']['types']['show'] = array_merge($defaultConfig['templates']['types']['show'], [ - 'date' => '@SonataIntl/CRUD/show_date.html.twig', - 'datetime' => '@SonataIntl/CRUD/show_datetime.html.twig', - 'smallint' => '@SonataIntl/CRUD/show_decimal.html.twig', - 'bigint' => '@SonataIntl/CRUD/show_decimal.html.twig', - 'integer' => '@SonataIntl/CRUD/show_decimal.html.twig', - 'decimal' => '@SonataIntl/CRUD/show_decimal.html.twig', - 'currency' => '@SonataIntl/CRUD/show_currency.html.twig', - 'percent' => '@SonataIntl/CRUD/show_percent.html.twig', + 'date' => '@SonataAdmin/CRUD/Intl/show_date.html.twig', + 'datetime' => '@SonataAdmin/CRUD/Intl/show_datetime.html.twig', + 'smallint' => '@SonataAdmin/CRUD/Intl/show_decimal.html.twig', + 'bigint' => '@SonataAdmin/CRUD/Intl/show_decimal.html.twig', + 'integer' => '@SonataAdmin/CRUD/Intl/show_decimal.html.twig', + 'decimal' => '@SonataAdmin/CRUD/Intl/show_decimal.html.twig', + 'currency' => '@SonataAdmin/CRUD/Intl/show_currency.html.twig', + 'percent' => '@SonataAdmin/CRUD/Intl/show_percent.html.twig', ]); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f9bc4bfd23..b47099faf0 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -514,6 +514,11 @@ public function getConfigTreeBuilder() ->info('Translate group label') ->end() + ->booleanNode('use_intl_templates') + ->defaultFalse() + ->info('Whether localized information should be shown, it will replace some default templates') + ->end() + ->end() ->end(); diff --git a/src/DependencyInjection/SonataAdminExtension.php b/src/DependencyInjection/SonataAdminExtension.php index 486c71f6d0..f7d7f6c5cb 100644 --- a/src/DependencyInjection/SonataAdminExtension.php +++ b/src/DependencyInjection/SonataAdminExtension.php @@ -49,15 +49,6 @@ public function load(array $configs, ContainerBuilder $container) ]); } - if (isset($bundles['SonataIntlBundle'])) { - // integrate the SonataUserBundle if the bundle exists - array_unshift($configs, [ - 'templates' => [ - 'history_revision_timestamp' => '@SonataIntl/CRUD/history_revision_timestamp.html.twig', - ], - ]); - } - $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('twig.xml'); $loader->load('core.xml'); @@ -95,6 +86,16 @@ public function load(array $configs, ContainerBuilder $container) $container->removeDefinition('sonata.admin.lock.extension'); } + $useIntlTemplates = $config['use_intl_templates'] || isset($bundles['SonataIntlBundle']); + + $container->setParameter('sonata.admin.configuration.use_intl_templates', $useIntlTemplates); + + if ($useIntlTemplates) { + if ('@SonataAdmin/CRUD/history_revision_timestamp.html.twig' === $config['templates']['history_revision_timestamp']) { + $config['templates']['history_revision_timestamp'] = '@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig'; + } + } + $container->setParameter('sonata.admin.configuration.global_search.empty_boxes', $config['global_search']['empty_boxes']); $container->setParameter('sonata.admin.configuration.global_search.case_sensitive', $config['global_search']['case_sensitive']); $container->setParameter('sonata.admin.configuration.templates', $config['templates']); diff --git a/src/Resources/views/CRUD/Intl/display_currency.html.twig b/src/Resources/views/CRUD/Intl/display_currency.html.twig new file mode 100644 index 0000000000..400972c224 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/display_currency.html.twig @@ -0,0 +1,22 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {%- if value is null -%} +   + {%- else -%} + {% set currency = field_description.options.currency %} + {% set attributes = field_description.options.attributes|default({}) %} + {% set locale = field_description.options.locale|default(null) %} + + {{ value|format_currency(currency, attributes, locale) }} + {%- endif -%} +{% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_date.html.twig b/src/Resources/views/CRUD/Intl/display_date.html.twig new file mode 100644 index 0000000000..f638256ca2 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/display_date.html.twig @@ -0,0 +1,26 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {%- if value is empty -%} +   + {%- else -%} + {% set pattern = field_description.options.pattern|default('') %} + {% set calendar = field_description.options.calendar|default('') %} + {% set locale = field_description.options.locale|default(null) %} + {% set timezone = field_description.options.timezone|default(null) %} + {% set dateType = field_description.options.dateType|default(null) %} + + + {%- endif -%} +{% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_datetime.html.twig b/src/Resources/views/CRUD/Intl/display_datetime.html.twig new file mode 100644 index 0000000000..dd42a183fc --- /dev/null +++ b/src/Resources/views/CRUD/Intl/display_datetime.html.twig @@ -0,0 +1,27 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {%- if value is empty -%} +   + {%- else -%} + {% set pattern = field_description.options.pattern|default('') %} + {% set calendar = field_description.options.calendar|default('') %} + {% set locale = field_description.options.locale|default(null) %} + {% set timezone = field_description.options.timezone|default(null) %} + {% set dateType = field_description.options.dateType|default(null) %} + {% set timeType = field_description.options.timeType|default(null) %} + + + {%- endif -%} +{% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_decimal.html.twig b/src/Resources/views/CRUD/Intl/display_decimal.html.twig new file mode 100644 index 0000000000..f294cc9bc9 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/display_decimal.html.twig @@ -0,0 +1,22 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {%- if value is null -%} +   + {%- else -%} + {% set attributes = field_description.options.attributes|default({}) %} + {% set locale = field_description.options.locale|default(null) %} + {% set type = field_description.options.type|default('default') %} + + {{ value | format_number(attributes, 'decimal', type, locale) }} + {%- endif -%} +{% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/display_percent.html.twig b/src/Resources/views/CRUD/Intl/display_percent.html.twig new file mode 100644 index 0000000000..43808f9e10 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/display_percent.html.twig @@ -0,0 +1,22 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {%- if value is null -%} +   + {%- else -%} + {% set attributes = field_description.options.attributes|default({}) %} + {% set locale = field_description.options.locale|default(null) %} + {% set type = field_description.options.type|default('default') %} + + {{ value | format_number(attributes, 'percent', type, locale) }} + {%- endif -%} +{% endapply -%} diff --git a/src/Resources/views/CRUD/Intl/history_revision_timestamp.html.twig b/src/Resources/views/CRUD/Intl/history_revision_timestamp.html.twig new file mode 100644 index 0000000000..84aabd21e6 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/history_revision_timestamp.html.twig @@ -0,0 +1,12 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- include '@SonataAdmin/CRUD/Intl/display_datetime.html.twig' with { value: revision.timestamp } -%} diff --git a/src/Resources/views/CRUD/Intl/list_currency.html.twig b/src/Resources/views/CRUD/Intl/list_currency.html.twig new file mode 100644 index 0000000000..4e5ef68e43 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/list_currency.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends admin.getTemplate('base_list_field') %} + +{% block field%} + {%- include '@SonataAdmin/CRUD/Intl/display_currency.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/list_date.html.twig b/src/Resources/views/CRUD/Intl/list_date.html.twig new file mode 100644 index 0000000000..c0fe0f159d --- /dev/null +++ b/src/Resources/views/CRUD/Intl/list_date.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends admin.getTemplate('base_list_field') %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_date.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/list_datetime.html.twig b/src/Resources/views/CRUD/Intl/list_datetime.html.twig new file mode 100644 index 0000000000..93ef441581 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/list_datetime.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends admin.getTemplate('base_list_field') %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_datetime.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/list_decimal.html.twig b/src/Resources/views/CRUD/Intl/list_decimal.html.twig new file mode 100644 index 0000000000..cbe1749a09 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/list_decimal.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends admin.getTemplate('base_list_field') %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_decimal.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/list_percent.html.twig b/src/Resources/views/CRUD/Intl/list_percent.html.twig new file mode 100644 index 0000000000..d8248c22ae --- /dev/null +++ b/src/Resources/views/CRUD/Intl/list_percent.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends admin.getTemplate('base_list_field') %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_percent.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/show_currency.html.twig b/src/Resources/views/CRUD/Intl/show_currency.html.twig new file mode 100644 index 0000000000..7c499d77a2 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/show_currency.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_currency.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/show_date.html.twig b/src/Resources/views/CRUD/Intl/show_date.html.twig new file mode 100644 index 0000000000..47127757d8 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/show_date.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_date.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/show_datetime.html.twig b/src/Resources/views/CRUD/Intl/show_datetime.html.twig new file mode 100644 index 0000000000..aab91e8f74 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/show_datetime.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_datetime.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/show_decimal.html.twig b/src/Resources/views/CRUD/Intl/show_decimal.html.twig new file mode 100644 index 0000000000..ba339b0f3d --- /dev/null +++ b/src/Resources/views/CRUD/Intl/show_decimal.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_decimal.html.twig' -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/Intl/show_percent.html.twig b/src/Resources/views/CRUD/Intl/show_percent.html.twig new file mode 100644 index 0000000000..0d953ead54 --- /dev/null +++ b/src/Resources/views/CRUD/Intl/show_percent.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/Intl/display_percent.html.twig' -%} +{% endblock %} diff --git a/tests/DependencyInjection/SonataAdminExtensionTest.php b/tests/DependencyInjection/SonataAdminExtensionTest.php index b70a97179a..5b201e868e 100644 --- a/tests/DependencyInjection/SonataAdminExtensionTest.php +++ b/tests/DependencyInjection/SonataAdminExtensionTest.php @@ -309,11 +309,11 @@ public function testDefaultTemplates(): void public function testLoadIntlTemplate(): void { - $bundlesWithSonataIntlBundle = array_merge($this->container->getParameter('kernel.bundles'), ['SonataIntlBundle' => true]); - $this->container->setParameter('kernel.bundles', $bundlesWithSonataIntlBundle); - $this->load(); + $this->load([ + 'use_intl_templates' => true, + ]); $templates = $this->container->getParameter('sonata.admin.configuration.templates'); - $this->assertSame('@SonataIntl/CRUD/history_revision_timestamp.html.twig', $templates['history_revision_timestamp']); + $this->assertSame('@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig', $templates['history_revision_timestamp']); } protected function getContainerExtensions(): array