python-humanize / humanize

Python humanize functions
https://humanize.readthedocs.io
MIT License
500 stars 61 forks source link

Add en_GB and en_US #210

Open 2br-2b opened 3 weeks ago

2br-2b commented 3 weeks ago

What did you do?

Hello! Thank you so much for making this library!!!

I'm using this to bot to translate some strings for my python discord bot. To do that, I am mapping discord locales to Humanize strings, like so:

HUMANIZE_LOCALE_STRINGS = {
    Locale.taiwan_chinese : "zh_HK",
    Locale.german : "de_DE",
    Locale.latin_american_spanish : "es_ES",
    etc.
]

I wish I could also map British English and American English to en_US and en_GB instead of setting them to None, then doing a None check whenever I get the locale.

Especially since en_GB is listed as an example locale at the bottom of the Readme, I wish this was an option


Please include code that reproduces the issue.

>>> import humanize
>>> humanize.i18n.activate("en_US")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/homer/StoryBot-Testing/.venv/lib/python3.10/site-packages/humanize/i18n.py", line 76, in activate
    translation = gettext_module.translation("humanize", path, [locale])
  File "/usr/lib/python3.10/gettext.py", line 607, in translation
    raise FileNotFoundError(ENOENT,
FileNotFoundError: [Errno 2] No translation file found for domain: 'humanize'
hugovk commented 3 weeks ago

Would something like this work for you?

diff --git a/src/humanize/i18n.py b/src/humanize/i18n.py
index 42447b0..c4ca274 100644
--- a/src/humanize/i18n.py
+++ b/src/humanize/i18n.py
@@ -71,6 +71,9 @@ def activate(
     Raises:
         Exception: If humanize cannot find the locale folder.
     """
+    if locale and locale.startswith("en"):
+        locale = None
+
     if path is None:
         path = _get_default_locale_path()
2br-2b commented 3 weeks ago

Yes, this seems to fix the issue for me! Thank you so much!

hugovk commented 3 weeks ago

Great, would you like to put together a PR?

2br-2b commented 3 weeks ago

Sure thing! I'll have to do that after the weekend though - I've been working on this while procrastinating on my Ludum Dare game 😅

hugovk commented 3 weeks ago

No rush at all :) Have fun with Ludum Dare!