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

Add sort_by_msgid_case_insensitive #326

Merged

Conversation

superruzafa
Copy link
Contributor

@superruzafa superruzafa commented Nov 15, 2022

Closes #325.

Copy link
Contributor

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

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

Nice work! Left just a comment about the option name, but I think this feature in general is welcome 💯

lib/gettext.ex Outdated
Comment on lines 531 to 532
* `:sort_by_msgid_case_insensitive` - same as `:sort_by_msgid` but it
performs a case insensitive comparation when sorting alphabetically by msgid.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* `:sort_by_msgid_case_insensitive` - same as `:sort_by_msgid` but it
performs a case insensitive comparation when sorting alphabetically by msgid.
* `:sort_by_msgid_case_insensitive` - a boolean that behaves similarly to
`:sort_by_msgid`, but that sorts with a case-insensitive comparison.

Comment on lines 321 to 343
cond do
gettext_config[:sort_by_msgid] ->
Enum.sort_by(messages, & &1.msgid)

gettext_config[:sort_by_msgid_case_insensitive] ->
Enum.sort_by(messages, fn m -> Enum.map(m.msgid, &String.downcase/1) end)

true ->
messages
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should think about this API, because as it stands in this PR you could pass sort_by_msgid: true, sort_by_msgid_case_insensitive: false and the sorting would work, even if by reading the options I wouldn't really know what to expect.

What do you think about going with two complementary options, such as the existing :sort_by_msgid and :sort_is_case_sensitive? You'd have to specify sort_by_msgid: true, sort_is_case_sensitive: false in your use case.

Copy link
Contributor

Choose a reason for hiding this comment

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

@josevalim thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's would be fine. In case you agree with that we could apply these changes in this PR.

Comment on lines 321 to 344
cond do
gettext_config[:sort_by_msgid] ->
Enum.sort_by(messages, & &1.msgid)

gettext_config[:sort_by_msgid_case_insensitive] ->
Enum.sort_by(messages, fn m -> Enum.map(m.msgid, &String.downcase/1) end)

true ->
messages
end
Copy link
Member

Choose a reason for hiding this comment

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

To make sure that the line splitting of the message has no impact on the sorting, I would recommend changing this a bit:

Suggested change
cond do
gettext_config[:sort_by_msgid] ->
Enum.sort_by(messages, & &1.msgid)
gettext_config[:sort_by_msgid_case_insensitive] ->
Enum.sort_by(messages, fn m -> Enum.map(m.msgid, &String.downcase/1) end)
true ->
messages
end
cond do
gettext_config[:sort_by_msgid] ->
Enum.sort_by(messages, &IO.chardata_to_string(&1.msgid))
gettext_config[:sort_by_msgid_case_insensitive] ->
Enum.sort_by(messages, &(&1 |> IO.chardata_to_string() |> String.downcase()))
true ->
messages
end

Copy link
Contributor

Choose a reason for hiding this comment

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

We were already sorting without IO.chardata_to_string/1. Do we know if it has ever caused issues? I think in general we should fix that in another PR and add test cases for it.

Copy link
Member

@maennchen maennchen Dec 20, 2022

Choose a reason for hiding this comment

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

@whatyouhide That’s something I should’ve probably done in #307 and spotted now when reading the changes.

I‘m not aware of issues so far but I can see how it could be an issue.

I can open a PR and add some tests if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's do that @maennchen, thanks 👍

Copy link
Member

Choose a reason for hiding this comment

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

Done: #331

This PR will need a merge after #331 is merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

Merged #331 👍

@whatyouhide whatyouhide force-pushed the sort_by_msgid_case_insensitive branch from 4172d87 to b40bd8a Compare January 4, 2023 08:57
@whatyouhide
Copy link
Contributor

@superruzafa I took the liberty of pushing a change to this branch that sets the :sort_by_msgid option to :case_sensitive | :case_insensitive | nil. @josevalim let me know if this looks good to you 🙃

@coveralls
Copy link

Pull Request Test Coverage Report for Build b40bd8a5952648b85547215fbe20e82a51e756a0-PR-326

  • 5 of 7 (71.43%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 89.372%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/gettext/extractor.ex 5 7 71.43%
Totals Coverage Status
Change from base Build 9285a824267cfd04678ef762b0ba5cdeedaa22ad: -0.2%
Covered Lines: 555
Relevant Lines: 621

💛 - Coveralls

@whatyouhide whatyouhide merged commit 0dd72d3 into elixir-gettext:main Jan 6, 2023
@superruzafa superruzafa deleted the sort_by_msgid_case_insensitive branch January 16, 2023 13:50
ravensiris pushed a commit to ravensiris/gettext that referenced this pull request Aug 3, 2023
Co-authored-by: Alfonso Ruzafa <alfonso.ruzafa@cabify.com>
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.

Sort messages alphabetically case insensitive
4 participants