Skip to content
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

Fix Inconsistent Behavior When Editing Products to mg Units in Hungarian Locale #12826

Conversation

chahmedejaz
Copy link
Collaborator

What? Why?

What should we test?

  • This needs to be validated with the Hungarian language (Magyar)
  • Visit the products page
  • Update product unit scale to different values
  • It's better to validate it with all the available unit scales in the application.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

@@ -26,9 +26,15 @@ def system
def self.variant_unit_options
available_units_sorted.flat_map do |measurement, measurement_info|
measurement_info.filter_map do |scale, unit_info|
# Our code is based upon English based number formatting
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref:

describe "#variant_unit_options" do
let(:available_units) { "mg,g,kg,T,mL,cL,dL,L,kL,lb,oz,gal" }
subject { WeightsAndMeasures.variant_unit_options }
before do
allow(Spree::Config).to receive(:available_units).and_return(available_units)
end
it "returns options for each unit" do
expected_array = [
["Weight (mg)", "weight_0.001"],
["Weight (g)", "weight_1"],
["Weight (oz)", "weight_28.35"],
["Weight (lb)", "weight_453.6"],
["Weight (kg)", "weight_1000"],
["Weight (T)", "weight_1000000"],
["Volume (mL)", "volume_0.001"],
["Volume (cL)", "volume_0.01"],
["Volume (dL)", "volume_0.1"],
["Volume (L)", "volume_1"],
["Volume (gal)", "volume_4.54609"],
["Volume (kL)", "volume_1000"],
["Items", "items"],
]
expect(subject).to match_array expected_array # diff each element
expect(subject).to eq expected_array # test ordering also
end

# Some language locales like +hu+ uses a comma(,) for decimal separator
# While in English, decimal separator is represented by a period.
# e.g. en: 0.001, hu: 0,001
# Hence the results become "weight_0,001" for hu while or code recognizes "weight_0.001"
scale_clean =
ActiveSupport::NumberHelper.number_to_rounded(scale, precision: nil, significant: false,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this helper should only be used to round numbers when we want to display the output to the end user as it is highly influenced by the locale being used.

Upon further investigation (from old UI), I think it's being an alternative to Javascript's parseFloat method which behaves just like this helper. And here Rails being Rails, it just does so much under the hood that it feels like magic and these odd behaviors can be missed easily 😓

@chahmedejaz
Copy link
Collaborator Author

chahmedejaz commented Aug 28, 2024

FYI: The failing specs are unrelated. (also failing in master).

scale_clean =
ActiveSupport::NumberHelper.number_to_rounded(scale, precision: nil, significant: false,
strip_insignificant_zeros: true)
strip_insignificant_zeros: true,
locale: :en)
[
"#{I18n.t(measurement)} (#{unit_info['name']})", # Label (eg "Weight (g)")
"#{measurement}_#{scale_clean}", # Scale ID (eg "weight_1")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly, we can tweak the keys of the UNITS hash such that we don't need to use the helper to round off-scale values here. i.e. already round off keys here. But I'm reluctant to do so because it's being used in other places and not sure if it's a good idea to do it this quickly.
https://github.com/chahmedejaz/openfoodnetwork/blob/a50be52cded204db8ba67e6dee4bdb4888d2d4a1/app/services/weights_and_measures.rb#L65-L86

The current solution works just like we would have expected in the old UI. So, maybe we can go with it for now. 🤷

@chahmedejaz chahmedejaz added the bug-s2 The bug is affecting any of the non-critical features described in S1 and there is no workaround. label Aug 28, 2024
@dacook dacook self-requested a review August 28, 2024 23:31
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for looking into this Ahmed. This looks like the perfect quick fix.
As it is dealing with backend codes that are not visible to the user, it's appropriate to force the locale here. I like that the comment explains why in weights_and_measures, so suggest we also have that in products.rb

app/models/spree/product.rb Show resolved Hide resolved
@dacook
Copy link
Member

dacook commented Aug 28, 2024

I think the weights and measures code in the system is pretty complicated and would like to see it simplified somehow..
But I don't have a suggestion for that right now!

@chahmedejaz
Copy link
Collaborator Author

I like that the comment explains why in weights_and_measures, so suggest we also have that in products.rb

Thanks, David 👍

I think the weights and measures code in the system is pretty complicated and would like to see it simplified somehow..

Yeah, agreed 😅

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I wonder why this didn't fail in Germany and France before. They use a comma, too, right?

I think that the underlying problem is that we compute identifiers for the units. We should have a fixed set for identifiers. Then we don't need to convert anything which could be affected by a locale.

@chahmedejaz, would you like to work on a follow-up contribution? Now that you fixed the problem, it's just tech debt.

@chahmedejaz
Copy link
Collaborator Author

I think that the underlying problem is that we compute identifiers for the units. We should have a fixed set for identifiers. Then we don't need to convert anything which could be affected by a locale.

Exactly, @mkllnk agreed. Sure would love to work on this tech debt. Thanks. :)

@chahmedejaz
Copy link
Collaborator Author

Interesting. I wonder why this didn't fail in Germany and France before. They use a comma, too, right?

I'll look into these and get back to you whether the locales for these actually use commas in Rails app. Thanks

@filipefurtad0 filipefurtad0 self-assigned this Aug 29, 2024
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Aug 29, 2024
@filipefurtad0
Copy link
Contributor

Hey @chahmedejaz ,

Thanks so much for this blazing fast fix.

First I've followed the steps described here, to enable the HU locale on staging-UK. Then I've reproduced the steps leading to the bug, and could now observe that the units are changed consistently:

From mL:
image

To mg:
image

Edited the name:
image

I've also made some smoke tests around the EN locale, with other units like Kg, and changing these to L, or items. Looks all good.
Since it looks like the bug relates to how commas and dots are used as separators, I've changed this setting in the server and repeated the tests above.

image

All good!
Merging.

@filipefurtad0 filipefurtad0 merged commit 98eabc9 into openfoodfoundation:master Aug 29, 2024
51 of 53 checks passed
@chahmedejaz
Copy link
Collaborator Author

chahmedejaz commented Aug 29, 2024

Interesting. I wonder why this didn't fail in Germany and France before. They use a comma, too, right?

I'll look into these and get back to you whether the locales for these actually use commas in Rails app. Thanks

@mkllnk - I just investigated this, and the issue is reproducible using the French locale however, on the German one it's working fine.
I believe the gem that provides these locales-related formatting data in the app (rails-i18n) has a comma as a decimal separator for the French locale and a period for the German.

@mkllnk mkllnk added user facing changes Thes pull requests affect the user experience and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-s2 The bug is affecting any of the non-critical features described in S1 and there is no workaround. user facing changes Thes pull requests affect the user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Editing products with mL as units leads to inconsistent behavior with Hungarian locale
4 participants