Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ How to change locale at runtime:
'3 seconds ago'
```

Number formatting also uses the locale's configured numeric separators:

```pycon
>>> _t = humanize.i18n.activate("tr_TR")
>>> humanize.intcomma(1_234_567.89)
'1.234.567,89'
>>> humanize.intword(1_200_000)
'1,2 milyon'
>>> humanize.i18n.deactivate()
```

You can pass additional parameter `path` to `activate` to specify a path to search
locales in.

Expand Down
2 changes: 2 additions & 0 deletions src/humanize/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"pt_BR": ".",
"hu_HU": " ",
"lv": " ",
"tr_TR": ".",
}

# Mapping of locale to decimal separator
Expand All @@ -36,6 +37,7 @@
"pt_BR": ",",
"hu_HU": ",",
"lv": ",",
"tr_TR": ",",
}


Expand Down
9 changes: 9 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def test_intcomma() -> None:
humanize.i18n.activate("pt_BR")
assert humanize.intcomma(number) == "10.000.000"

humanize.i18n.activate("tr_TR")
assert humanize.intcomma(number) == "10.000.000"
assert humanize.intcomma(1_234_567.89) == "1.234.567,89"
assert humanize.intcomma("1234567,89") == "1.234.567,89"
assert humanize.intcomma("1.234.567,89") == "1.234.567,89"

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

Expand Down Expand Up @@ -159,6 +165,9 @@ def test_naturaldelta() -> None:
("it_IT", 1_200_000, "1,2 milioni"),
("it_IT", 1_000_000_000, "1,0 miliardo"),
("it_IT", 3_500_000_000, "3,5 miliardi"),
# Turkish uses comma as decimal separator
("tr_TR", 1_000_000, "1,0 milyon"),
("tr_TR", 1_200_000, "1,2 milyon"),
# Sinhala uses borrowed scale words with a dot decimal separator
("si_LK", 1_000_000, "1.0 මිලියන"),
("si_LK", 1_200_000, "1.2 මිලියන"),
Expand Down