-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] make date formats and number formats configurable #13554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
this implementation is wrong. the Twig_Environment should not be configured through the TwigEngine, as this means that the |
I see your point, but do we have a better place where we can call methods of the Twig extensions? |
@xabbuh Well, there is 2 solutions here:
|
->arrayNode('number_format') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->scalarNode('decimals')->defaultValue(0)->end() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be an integer node
@stof Thanks for pointing me towards service configurators. |
@@ -165,5 +166,14 @@ | |||
<argument type="service" id="http_kernel" /> | |||
<argument>%twig.exception_listener.controller%</argument> | |||
</service> | |||
|
|||
<service id="twig.configurator.environment" class="Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, we cannot make the service private because it then gets inlined and the XML dumper fails here: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php#L182
The error message then is:
ContextErrorException: Warning: DOMElement::setAttribute() expects parameter 2 to be string, object given in /var/www/symfony/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php line 182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting the second argument to a string should fix the issue properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it won't. The issue is that the XML format cannot accept inline services for the configurator, and so cannot handle the case of private configurator service definitions getting inlined in place where they are used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho the same would be true for other formats as well since the definition does not contain the service id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh the PHP dumper already supports this case. And the YamlDumper is irrelevant, because it is already unusable on a compiled container (the YAML config format does not support inline service definitions in arguments for instance)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have a look at #13557 for a proposal about making it possible to use inlined configurator services in the XML format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that factories and configurators can now be inlined, this is not an issue anymore and I made the service private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you need to update the depdency to require the DependencyInjection component version where this has been fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we really need to do this. It would mean to add a conflict rule to the Composer config. The issue has been solved in the latest patch version of all maintained Symfony versions though. How did we deal with things like this in the past?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't pay enough attention to such things so far. But to be correct it would be required.
ping @symfony/deciders |
->children() | ||
->scalarNode('format')->defaultNull()->end() | ||
->scalarNode('interval_format')->defaultNull()->end() | ||
->scalarNode('timezone')->defaultNull()->end() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be documented that null means date_default_timezone_get
c7716eb
to
82b249b
Compare
I addressed @Tobion's last comments and added an entry to the changelog. |
@stof what do you think about #13554 (comment) |
By the way, why is the DependencyInjection component not a mandatory requirement? Does this make sense at all? |
ping @symfony/deciders Would be nice if we could get this into 2.7. Bumping the DependencyInjection version imho could be discussed afterwards. |
👍 |
1 similar comment
👍 |
$container->setParameter('twig.number_format.decimal_point', $config['number_format']['decimal_point']); | ||
$container->setParameter('twig.number_format.decimals', $config['number_format']['decimals']); | ||
$container->setParameter('twig.number_format.thousands_separator', $config['number_format']['thousands_separator']); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All those parameters should be removed and instead, you should inject the value in the service directly. That's what we are doing now as much as possible to avoid polluting the parameters with information you don't need to access.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
<argument /> | ||
<argument /> | ||
<argument /> | ||
<argument /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe to not confuse developers looking where those arguments are set add comment like:
<!-- Above arguments are set in TwigExtension -->
This adds new Twig configuration options that make it possible to configure the format of both numbers and dates as well as timezones without the need to write custom code. For example, using the new configuration options can look like this: ```yaml twig: date: format: d.m.Y, H:i:s interval_format: %%d days timezone: Europe/Berlin number_format: decimals: 2 decimal_point: , thousands_separator: . ```
Thank you @xabbuh. |
…igurable (xabbuh) This PR was merged into the 2.7 branch. Discussion ---------- [TwigBundle] make date formats and number formats configurable | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13552 | License | MIT | Doc PR | TODO This adds new Twig configuration options that make it possible to configure the format of both numbers and dates as well as timezones without the need to write custom code. For example, using the new configuration options can look like this: ```yaml twig: date: format: d.m.Y, H:i:s interval_format: %%d days timezone: Europe/Berlin number_format: decimals: 2 decimal_point: , thousands_separator: . ``` Commits ------- e9bc23b make date formats and number formats configurable
This is fine. As long as you dont have multiple locales. Wouldn't it make more sense to configure it (additionally?) via translation files? |
I made that same argument here: #13552 (comment) and got no love. I agree there needed to be a better overall way to generally make changing formats easier, however, I see it the same way as @King2500 . Date, number and even currency formats should be part of ( are defined by?) the end user's locale. If you only need one format, then you most likely only have your application working with users who live in one locale (if there are other reasons for changing the formatting in an application globally, please let me know, as I don't understand the reasoning/ use cases). If you have different users with different locales using the application, then you have to use different locale formats. How does this improvement work with those other locale formats? Also, if you actually do need to use different formatting schemes than the locales offer (and again, I'd love to understand why this might be necessary in a global perspective), then the locale definition is what should be overridable (or possibly make custom locale definitions? Though, that makes even less sense...hmmmm.). Or actually, this extension http://twig.sensiolabs.org/doc/extensions/intl.html has the right logic and I think should actually be part of the core twig package (of an internationally usable templating system). With it in the core, then the locales should be configurable as to which locales are used in the application (defined by each user) and/ or which are overridden or new or customized and how. So that way, when you create templates with variables needing formatting, Twig already takes the user's locale (or the special overridden formatting) into consideration and outputs the formatting accordingly (yes, I'd even leave out the filters and only use a filter, if overriding in the template is actually necessary). If the addition of the extension to the core causes "too much overhead" and why others that don't really need it can avoid using it (and why it is an extension?), then I'd venture to say, the whole formatting system in Twig needs to be revisited. It wasn't created with an "international templating system" in mind, which any template system should be first and foremost. Scott |
…igs (xabbuh) This PR was merged into the 2.7 branch. Discussion ---------- [TwigBundle] allow to configure custom formats in XML configs | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13554 | License | MIT | Doc PR | Commits ------- 5a3a24b allow to configure custom formats in XML configs
This adds new Twig configuration options that make it possible to
configure the format of both numbers and dates as well as timezones
without the need to write custom code.
For example, using the new configuration options can look like this: