-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Dont set the locale implicitly #115
base: 2.25.x
Are you sure you want to change the base?
Dont set the locale implicitly #115
Conversation
cebe810
to
56bf60e
Compare
We need to clarify that. Do you see a scenario in which this change could lead to problems? |
Well, its complicated?
So yes this is a change of behavior to before. I would argue that the previous behavior is a bug in need of a workaround (as I did) not a feature that someone purposefully used to somehow get the old locale after he/she set a new one via So no I cant imagine using this bug purposefully and therefore dont think its BC break, but rather a bug fix that fixes faulty behavior. Your usage may vary though - I can only speak for my projects and my imagination ;-) So Im open to your opinions on this. |
The fact that the value of the locale is stored is not documented:
This wording is used by almost all view helpers and validators. |
The translator uses the same logic: laminas-i18n/src/Translator/Translator.php Lines 252 to 264 in 80d9f5d
Also the laminas-i18n/src/Validator/IsFloat.php Lines 75 to 86 in 80d9f5d
The laminas-i18n/src/Validator/IsInt.php Lines 75 to 86 in 80d9f5d
In the laminas-i18n/src/Validator/DateTime.php Lines 72 to 87 in 80d9f5d
laminas-i18n/src/Validator/DateTime.php Lines 232 to 240 in 80d9f5d
laminas-i18n/src/Validator/PostCode.php Lines 235 to 245 in 80d9f5d
laminas-i18n/src/Validator/PostCode.php Lines 256 to 264 in 80d9f5d
A standardised behaviour would therefore be appropriate. |
I immediately assumed a BC break at the beginning and even you had labelled it as a possibility. A more in-depth analysis is therefore essential. |
Oh Ive missed those. Ill be changing them too for completeness' sake. |
@gsteel |
Fixed the unit tests and removed the The |
Psalm is ... something else. |
This view helper does it right, a locale should always be set. The factory provides the initial value, and when the helper is called, a customised locale can be defined. Manipulation from outside of the helper, validator or translator falls into the category of magic. |
What differentiates the
Then every call to Which would make using this lib a lot harder. Even if only one locale / language is used... What Laminas could do is to define a central class where every other class gets the currently active locale from. A one-stop-shop to switch the active locale in every subsystem that needs it. Every class only has a reference and uses it on the fly. Tada you're reimplementing |
Please keep in mind that there are years between these two view helpers. But changing the behaviour in this direction for all helpers means creating a new major version.
Why harder? You can set it via configuration and it is done. If you require a different locale, define it when calling the helper. |
I have never said that PHP's |
Its harder when I want to render the same view script in 5 different locales. I have to go through each and every class and look if it has the old locale set and set it to the new one. Alas I dont want to die on this hill, Ive never used the |
So that we don't misunderstand each other, please ignore the old helpers. Here is an example: final class CustomFormatHelper
{
public function __construct(private string $defaultLocale)
{}
public function __invoke(string $text, int $format, ?string $locale = null): string
{
// …
}
} Usage: $helper = new CustomFormatHelper('de_DE');
echo $helper('example', 1);
echo $helper('example', 2);
echo $helper('example', 1, 'en_US');
echo $helper('example', 2, 'en_US');
echo $helper('example', 1, 'de_CH'); No setters or getters for the locale and no magic to manipulate the helper's state from the outside. |
Am I right in assuming that this patch is specifically to prevent the state inside the view helpers and validators being populated with In this case, the whole premise is flawed - The locale should be passed to the view helpers via By explicitly providing the desired locale, where it's needed at runtime, the default locale becomes irrelevant. Edit: I added this comment before seeing @froschdesign 's more recent comments which I believe echoes what I've said there. |
@gsteel |
Yes exactly.
So each and every invocation of a view helper should have the This will increase the needed boiler plate in our view script by A LOT. Each I'd argue that this change would in need of a new major version too. But oh well.
Im not sure on how to go forward now - if there is any way now at all. What I'm getting from your two comments is, that my PR should be dismissed and all view helper reworked to be analogeous to the behavior in Stating this upfront would have saved me a lot of wasted time. |
@gsteel Locale::setDefault('de_DE');
$translator = new Laminas\I18n\Translator\Translator();
var_dump($translator->getLocale()); // string(5) "de_DE"
Locale::setDefault('en_US');
var_dump($translator->getLocale()); // string(5) "en_US" And the new Unfortunately, the differences in handling have always been there, so we are not making things worse at this point. |
I don't see the initial patch as problematic - to begin with, you were just returning The later changes to the validators and CountryDataList are too much IMO. Validators should not be kept in memory anyhow as each instance should only be used for the lifecycle of a form, and the I still haven't actually reviewed the changes yet, so I'll try and do that ASAP so that we can maybe get a middleground that solves your problems without affecting future design goals. As @froschdesign mentions, in future, all helpers would benefit from being declared something like:
And yes, this would necessitate passing in the locale explicitly at the point of use in all view scripts in cases where you wish to render the same view script in multiple different locales within the same process. I'd say it would be a big improvement for all helpers to not have access to any global state. |
Which changes do you want me to revert further? |
Please revert everything except the changes in the view helper. Sorry for the delay. |
Done. |
@MatthiasKuehneEllerhold |
d88a314
to
406500d
Compare
Squashed the commits to hide the history. |
…e::getDefault() if none is set Add tests Signed-off-by: Matthias Kühne <[email protected]>
406500d
to
c706c3f
Compare
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.
Thanks @MatthiasKuehneEllerhold
It's worth noting that this slight behaviour change should not be relied upon in the next major, even though it more closely aligns with the current documentation.
I am expecting that the setLocale
and getLocale
methods will be dropped from all the helpers, along with internal calls to Locale::getDefault
, and instead, to deviate from the configured default locale, it would be incumbent on the caller to pass the desired locale via __invoke
.
Allowing external state to dictate behaviour is not something I personally want to see perpetuated.
It seems I struck a nerve with this PR. I wanted to make the classes in this lib more to my liking, more usable for us. I guess I thought others felt the same. So you're saying that I can not trust this implementation to last more than until the next major version. This means 3.0 right? This might be weeks or months away, so its hard to judge how long this change will last then. @gstell If you want I can drop this PR and be done with it. Going back to the old implementation proved to be much less hassle than arguing with you for a month straigth. |
No nerves have been struck @MatthiasKuehneEllerhold - Purity is definitely a goal for me - this kind of global state can be especially problematic in environments like Swoole and RoadRunner. I can't speak for all the members of the TSC mind, perhaps @Ocramius or @weierophinney might offer another opinion. Your patch is appreciated - it has highlighted potential issues with the design of the lib that we might want to address in the next major, which is probably a way off, and thanks to your contributions, we have a clearer idea of what others want from this library. |
I did not read up to everything above, but the diff is indeed a behaviour BC break. I'm for it, but it can only happen in a new major release. That said, I'm similarly a friend of referential transparency, and detecting the locale inside a getter method is something I would not have added to the codebase within the last decade: it's just inherited behaviour that we have to live with, for now. |
Description
On a multi-locale project there may be some use cases where the locale is not fixed but has to be changed for certain actions. e. g. rendering a PDF in multiple languages, pre-rendering a view-script in multiple languages, rendering an email in a background job where no locale is inherently set.
Ideadlly I want to have a single source of truth:
\Locale::getDefault()
. This is used in php-internal functions like theDateFormatter
and so on, so I can switch the output locale on the fly.Sadly the view helpers of this module "cache" the locale in their state so I have to go through each and every one that may do this and set the locale explicitly.
This is hard to debug because the first usage of a specific view helper always uses the correct locale (because
$this->locale
is stillnull
), but the second usage of the view helper takes the old$locale
and not the new.