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(cli): sorting issue when different locales are used on host machines #1808

Merged
merged 5 commits into from
Nov 29, 2023

Conversation

Kepro
Copy link
Contributor

@Kepro Kepro commented Nov 27, 2023

Description

localeCompare produces different sort output based on user machine locales

e.g. colleague in Denmark da-DK, me en-US
examples:

"Opening Entry".localeCompare("Opening entry"); // 1 (en-US)
"Opening Entry".localeCompare("Opening entry"); // -1 (da-DK)

this was causing different orders of messages committed by different users (we are using format-json minimal)
we have translations like examples above when difference is only in casing
Opening Entry vs Opening entry

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Copy link

vercel bot commented Nov 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 29, 2023 0:41am

@timofei-iatsenko
Copy link
Collaborator

Good catch. I also refresehd documentation of localeComapre method for myself and thinking may be it's better to implement what they say there:

When comparing large numbers of strings, such as in sorting large arrays, it is better to create an Intl.Collator object and use the function provided by its compare() method.

Catalogs could be quite large, so instead of creatiing collator inside sort(), you could create it outside of the loop.

@Kepro
Copy link
Contributor Author

Kepro commented Nov 28, 2023

@thekip should I update PR? it is okay to use en?

@timofei-iatsenko
Copy link
Collaborator

I think it's fine. Just add a comment with explanation why we hard-code the en-US and probably link to this PR, just to quicker find the reason in future.

Added a hardcoded 'en-US' locale to the Intl.Collator in the orderByMessage function within api/catalog.ts. This is to guarantee consistent sorting across different environments. This change was informed by the discussion in the pull request lingui#1808.
Copy link
Contributor

@andrii-bodnar andrii-bodnar left a comment

Choose a reason for hiding this comment

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

@Kepro thank you!

@pkasarda
Copy link

YW! but looks like your size testing has also order problems :trollface:

@andrii-bodnar
Copy link
Contributor

@pkasarda it's related to this - #1810

Copy link

github-actions bot commented Nov 29, 2023

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.83 KB (0%)
./packages/detect-locale/dist/index.mjs 721 B (0%)
./packages/react/dist/index.mjs 1.68 KB (0%)
./packages/remote-loader/dist/index.mjs 7.24 KB (0%)

Copy link

codecov bot commented Nov 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d0fbc21) 76.27% compared to head (f255603) 76.28%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1808      +/-   ##
==========================================
+ Coverage   76.27%   76.28%   +0.01%     
==========================================
  Files          81       81              
  Lines        2074     2075       +1     
  Branches      529      529              
==========================================
+ Hits         1582     1583       +1     
  Misses        380      380              
  Partials      112      112              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andrii-bodnar andrii-bodnar merged commit 4b8b2a7 into lingui:main Nov 29, 2023
13 checks passed
@Kepro Kepro deleted the patch-1 branch November 29, 2023 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants