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

Handle en_GB and en_US locale #230

Merged
merged 12 commits into from
Feb 12, 2025
Merged

Conversation

dangillet
Copy link
Contributor

@dangillet dangillet commented Feb 2, 2025

Fixes #210
Replaces existing PR and closes #222

I tried to push that PR over the finishing line. The commits could all be squashed in a single commit. I wanted to keep track of the original author contribution.

Changes proposed in this pull request:

  • Add i18n support for en_GB, en_US and any languages starting with en_*

Copy link

codecov bot commented Feb 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.49%. Comparing base (de8b8a1) to head (35562f8).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #230      +/-   ##
==========================================
+ Coverage   99.47%   99.49%   +0.01%     
==========================================
  Files          11       11              
  Lines         760      785      +25     
==========================================
+ Hits          756      781      +25     
  Misses          4        4              
Flag Coverage Δ
macos-latest 97.70% <92.30%> (-0.19%) ⬇️
ubuntu-latest 97.70% <92.30%> (-0.19%) ⬇️
windows-latest 95.92% <80.76%> (-0.53%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@hugovk hugovk added the changelog: Added For new features label Feb 4, 2025
@hugovk
Copy link
Member

hugovk commented Feb 4, 2025

I wanted to keep track of the original author contribution.

Appreciated!

What's the reason for removing humanize.i18n.activate(None) from the test (in 36d77e8)? Do we not want to allow that?

@dangillet
Copy link
Contributor Author

The current type hint for activate is

def activate(
    locale: str, path: str | os.PathLike[str] | None = None
) -> gettext_module.NullTranslations:

So humanize.i18n.activate(None) is not allowed from a type hinting point of view.

If this is desired though, it's absolutely OK to amend the type hint. I will then also need to slightly amend the function implementation to deal with None for locale, which would be the same as choosing an English language.

I don't really see the reason for humanize.i18n.activate(None). If you don't want any translation, or because you're done with one language, I would assume the client would use instead humanize.i18n.deactivate().

I'll let you decide what you think is best.

@hugovk
Copy link
Member

hugovk commented Feb 7, 2025

Yes, these are good points.

I do see a couple of uses of humanize.i18n.activate(None if lang == "en" else lang) in the wild, which admittedly isn't much for a package downloaded 13 million times per month.

https://github.com/search?q=%2Factivate%5C%28None%2F&ref=opensearch&type=code

But we don't know how much non-GitHub code might do activate(None), and on balance I think it's probably better to allow it for compatability. It doesn't cost us much to allow None.

What do you think?

@dangillet
Copy link
Contributor Author

I fully agree. It seems like a good idea to allow activate(None). I just wanted to make sure you were aware of it.

I made the change and I moved to test inside the TestActivate class as it seems to belong there - we're testing more usage to activate.

I then noticed that the original test had a small issue. It did not use the decorator @freeze_time("2020-02-02"), which means that when it was calling humanize.naturaltime(three_seconds), the result actually was 5 years ago which is pretty surprising.

I decided to reduce this gotcha effect by making NOW a pytest fixture. Any code that would use it would have its time frozen in 2020-02-02.

@dangillet
Copy link
Contributor Author

dangillet commented Feb 8, 2025

My unit tests were failing on Windows because gettext was not installed. I amended test.yml file to install gettext on Windows and compile the translation files.

The unit tests now pass on Windows but I'm getting two failures for linting and code coverage one failure for coverage.

I don't understand the linting issue. Locally I'm not getting any linting issue. I also tried to copy-paste test.yml content in https://rhysd.github.io/actionlint/ and I'm not getting any failures. The linter uses actionlint which uses ShellCheck for run commands. It's a shell script static analysis tool, which then does not understand Windows commands. I decided to move those commands to a separate script file. I'm torn on the location. I put it under scripts, but it's only useful for the Github Action, as it's dealing with $env:GITHUB_PATH. Maybe it would be better placed within the .github folder?

For code coverage, I'm looking on Sentry, but the only lines that are not tested seem to be the docstring I added to the pytest fixture. What am I missing?

@dangillet dangillet force-pushed the patch-1 branch 2 times, most recently from 39a9308 to 97afe65 Compare February 8, 2025 11:40
@hugovk
Copy link
Member

hugovk commented Feb 11, 2025

Let's not bother with the .ps1 scripts, we only need to run it during the automated release process, which is done on Ubuntu. It's one less thing to maintain and test.

(And I've noticed it was accidentally left out when adding the automation, I've reported this in #231.)

@dangillet
Copy link
Contributor Author

Hi,

I'm happy to remove the ps1 scripts. But the unittests in the CI would not work then for Windows. One of my test uses a translation, and thus needs those files present.

Unless you want me to also change the test so we never try to translate anything in the unittests?

I noticed in other tests this pattern

    try:
        humanize.i18n.activate("ru_RU")
        assert humanize.naturaltime(three_seconds) == "3 секунды назад"
        assert humanize.ordinal(5) == "5ый"
        assert humanize.precisedelta(one_min_three_seconds) == "1 минута и 7 секунд"

    except FileNotFoundError:
        pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")

Is it how we want to handle it? No unittests should run translations on the CI?

@hugovk
Copy link
Member

hugovk commented Feb 12, 2025

Yes, let's use the try/except.

We can see skips for the Windows CI:

But they're run on the macOS and Ubuntu CI:

Because we're installing gettext and running/testing the script for those:

- name: Install Linux dependencies
if: startsWith(matrix.os, 'ubuntu')
run: |
sudo apt install gettext
- name: Install macOS dependencies
if: startsWith(matrix.os, 'macos')
run: |
brew install gettext
- name: Install uv
uses: hynek/setup-cached-uv@v2
- name: Generate translation binaries
run: |
scripts/generate-translation-binaries.sh

This means people can also test locally without generating the binaries.

2br-2b and others added 10 commits February 12, 2025 13:49
Locales starting with en_* will default to no transaltion.
This is similar to calling i18n.deactivate.
The global variable NOW came with a gotcha. When using it, it was also needed
to decorate the test with freeze_time.

NOW is now a fixture which keeps the time frozen for the duration of the test
using it.
The linter does not understand windows powershell syntax. So we move
it to the scripts folder.
Update the unittest to be skipped in case the translation binaries were
missing.
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thanks for your patience, I think we're almost there!

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thank you very much!

@hugovk hugovk enabled auto-merge February 12, 2025 21:22
@hugovk hugovk merged commit 31acb37 into python-humanize:main Feb 12, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Added For new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add en_GB and en_US
3 participants