-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Converting money to localized string fails in german locale. #30114
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
Comments
Does this mean that the |
The first transform method is called with (string)"3365" as $value, the second then with (string)"33,65". |
It looks like you need to investigate then why this is happening. The |
Have you read the description? I already know why this happens and i have explained it in the description. I even provided a possible solution. This is not a question of why it happens but how to solve it in the best way. The This is a problem with the symfony component above and needs to be fixed. Trying to play the blame-game when the program here is obviously broken is not helpful. |
I think we are talking about different things or I may be missing some detail. I was not able to reproduce what you describe so far. You can take a look at a small example I created at https://github.com/xabbuh/issue-30114/blob/master/test.php. Its output looks like this for me:
I think the best is if you can fork it and make changes that allow to reproduce the failure that you experience. |
You missed defining a divisor. Without a divisor the problematic devision does not get executed. $transformer = new MoneyToLocalizedStringTransformer(null, null, null, 2);
|
Thanks 👍 I think this should be fixed by #30126. |
…bbuh) This PR was merged into the 3.4 branch. Discussion ---------- [Form] forward valid numeric values to transform() | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30114 | License | MIT | Doc PR | Commits ------- 3be0d35 forward valid numeric values to transform()
Symfony version(s) affected: I'm using 2.8.34
(but the problematic code is also in the master branch, so all versions should be affected)
Description
In PHP, when converting a float value to string, the character used as a decimal-seperator depends on the locale. In an english locale, the full-stop "." is used ("123.45") while in german (f.e.) the comma "," is used ("123,45"). The PHP internal function "is_numeric" does not recognize the comma and would only consider the first representation ("123.45") as a number, but not the second one:
There is at least one piece of code in symfony where this behaviour leads to code breaking in certain locales and not in others. There are probably more but i have not checked yet. I have just stumbled over one such code in the MoneyToLocalizedStringTransformer on line 61 and NumberToLocalizedStringTransformer on line 113:
In an english locale, $value contains something like "123.45", which passes the is_number check.
In an german locale, $value contains something like "123,45", which fails the is_number check.
The call "parent::transform($value)" from above directly calls the code below:
This piece of code will now fail in a german locale and pass in an english one.
How to reproduce
Switch to a german locale ("de_DE.UTF-8") and use the Money transformers. I do with the Tbbc\MoneyBundle, but the problematic code is in symfony (see above) and not that bundle.
Possible Solution
I don't know if that is the best solution, but a simple "$value = str_replace(',', '.', $value);" in the MoneyToLocalizedStringTransformer directly after calculating and string-casting the $value should do the trick at least for me; I'm not sure about other locales. Maybe more research is required here for a better solution. This works for me:
The text was updated successfully, but these errors were encountered: