This thread contains a patchset. You're looking at the original emails,
but you may wish to use the patch review UI.
Review patch
5
3
[PATCH hello-hut 0/3] Add --lang for localized greetings
This series adds localized greetings, as discussed on the tracker.
The translation table lives in its own module, so translators never
need to touch the CLI code:
$ hello-hut --lang fr Ada
Bonjour, Ada!
Included: ar, de, en, es, fr, ja, pt, sv and tr. Corrections from
native speakers are very welcome.
Noor Haddad (3):
greetings: add a translation table
cli: add --lang to pick the greeting language
docs: document --lang
README.md | 5 ++++-
docs/configuration.md | 2 ++
src/hello_hut/cli.py | 6 +++++-
src/hello_hut/greetings.py | 6 ++++--
src/hello_hut/translations.py | 17 +++++++++++++++++
tests/test_cli.py | 4 ++++
tests/test_greetings.py | 4 ++++
7 files changed, 40 insertions(+), 4 deletions(-)
create mode 100644 src/hello_hut/translations.py
base-commit: c72c9ea1de40d170d9c119bbc9a70b2ad7f21b65
--
2.51.0
[PATCH hello-hut 1/3] greetings: add a translation table
---
src/hello_hut/greetings.py | 6 ++++ --
src/hello_hut/translations.py | 17 +++++++++++++++++
2 files changed, 21 insertions(+), 2 deletions(-)
create mode 100644 src/hello_hut/translations.py
diff --git a/src/hello_hut/greetings.py b/src/hello_hut/greetings.py
index 3b776fc..dc3fda6 100644
--- a/src/hello_hut/greetings.py
+++ b/src/hello_hut/greetings.py
@@ -2,6 +2,8 @@
import datetime
+ from .translations import TRANSLATIONS
+
DEFAULT_NAME = "friend"
@@ -15,9 +17,9 @@ def time_of_day_greeting(now=None):
return "Good evening"
- def greet(name=DEFAULT_NAME, shout=False, salutation=None):
+ def greet(name=DEFAULT_NAME, shout=False, salutation=None, lang="en"):
"""Build a greeting such as "Hello, Ada!"."""
if salutation is None:
- salutation = "Hello"
+ salutation = TRANSLATIONS.get(lang, TRANSLATIONS["en"])
message = f"{salutation}, {name}!"
return message.upper() if shout else message
diff --git a/src/hello_hut/translations.py b/src/hello_hut/translations.py
new file mode 100644
index 0000000..b44b115
--- /dev/null
+++ b/src/hello_hut/translations.py
@@ -0,0 +1,17 @@
+ """Salutations by language code.
+
+ Keep the table sorted by code. Corrections from native speakers are very
+ welcome; please send them to the devel list.
+ """
+
+ TRANSLATIONS = {
+ "ar": "مرحبا",
+ "de": "Hallo",
+ "en": "Hello",
+ "es": "Hola",
+ "fr": "Bonjour",
+ "ja": "こんにちは",
+ "pt": "Olá",
+ "sv": "Hej",
+ "tr": "Merhaba",
+ }
--
2.51.0
[PATCH hello-hut 2/3] cli: add --lang to pick the greeting language
---
src/hello_hut/cli.py | 6 +++++ -
tests/test_cli.py | 4 ++++
tests/test_greetings.py | 4 ++++
3 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/src/hello_hut/cli.py b/src/hello_hut/cli.py
index 5247b66..7676641 100644
--- a/src/hello_hut/cli.py
+++ b/src/hello_hut/cli.py
@@ -8,6 +8,7 @@ import sys
from . import __version__
from .config import load_config
from .greetings import DEFAULT_NAME, greet, time_of_day_greeting
+ from .translations import TRANSLATIONS
def build_parser():
@@ -26,6 +27,8 @@ def build_parser():
default="auto", help="colorize the output")
parser.add_argument("--format", choices=("text", "json"), default="text",
help="output format (default: text)")
+ parser.add_argument("--lang", choices=sorted(TRANSLATIONS), default=None,
+ help="greeting language (default: en)")
parser.add_argument("--version", action="version",
version=f"%(prog)s {__version__}")
return parser
@@ -45,12 +48,13 @@ def main(argv=None):
config = load_config()
name = args.name or config.get("name", DEFAULT_NAME)
shout = args.shout or bool(config.get("shout", False))
+ lang = args.lang or config.get("lang", "en")
if args.count < 1:
print("hello-hut: --count must be at least 1", file=sys.stderr)
return 2
salutation = time_of_day_greeting() if args.time_of_day else None
- message = greet(name, shout=shout, salutation=salutation)
+ message = greet(name, shout=shout, salutation=salutation, lang=lang)
for _ in range(args.count):
if args.format == "json":
print(json.dumps({"greeting": message, "name": name}))
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 7896e27..6982c64 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -28,3 +28,7 @@ class CliTest(unittest.TestCase):
code, out = run("Ada", "--format", "json")
self.assertEqual(code, 0)
self.assertEqual(json.loads(out), {"greeting": "Hello, Ada!", "name": "Ada"})
+
+ def test_lang(self):
+ code, out = run("Ada", "--lang", "es")
+ self.assertEqual(out.strip(), "Hola, Ada!")
diff --git a/tests/test_greetings.py b/tests/test_greetings.py
index 60cdf01..723e4f4 100644
--- a/tests/test_greetings.py
+++ b/tests/test_greetings.py
@@ -25,6 +25,10 @@ class GreetTest(unittest.TestCase):
self.assertEqual(time_of_day_greeting(at(13)), "Good afternoon")
self.assertEqual(time_of_day_greeting(at(21)), "Good evening")
+ def test_lang(self):
+ self.assertEqual(greet("Ada", lang="fr"), "Bonjour, Ada!")
+ self.assertEqual(greet("Ada", lang="xx"), "Hello, Ada!")
+
if __name__ == "__main__":
unittest.main()
--
2.51.0
[PATCH hello-hut 3/3] docs: document --lang
---
README.md | 5 ++++ -
docs/configuration.md | 2 ++
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 3e2a4f0..989533a 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,8 @@ $ hello-hut Ada --shout
HELLO, ADA!
$ hello-hut --time-of-day Grace
Good evening, Grace!
+ $ hello-hut --lang fr Ada
+ Bonjour, Ada!
```
## Options
@@ -37,6 +39,7 @@ Good evening, Grace!
| `--count N` | Repeat the greeting `N` times | `1` |
| `--color WHEN` | `auto`, `always` or `never`; `NO_COLOR` is respected | `auto` |
| `--format FORMAT` | `text` or `json` | `text` |
+ | `--lang CODE` | Greeting language: `ar`, `de`, `en`, `es`, `fr`, `ja`, `pt`, `sv`, `tr` | `en` |
## Configuration
@@ -56,7 +59,7 @@ Every key is described in [docs/configuration.md](docs/configuration.md).
- [x] Configuration file
- [x] `--count`, `--color` and `--format json`
- [x] Bash and zsh completions
- - [ ] Localized greetings with `--lang`
+ - [x] Localized greetings with `--lang` (on the `next` branch)
- [ ] A man page
- [ ] ~~Rhyming greetings~~ (out of scope, see the tracker)
diff --git a/docs/configuration.md b/docs/configuration.md
index fc43450..946a04e 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -9,6 +9,7 @@ win over the file.
| --- | --- | --- |
| `name` | string | Who to greet when no `NAME` is given |
| `shout` | boolean | Shout by default |
+ | `lang` | string | Default greeting language, e.g. `"fr"` |
Example:
@@ -16,6 +17,7 @@ Example:
# ~/.config/hello-hut/config.toml
name = "Ada"
shout = false
+ lang = "fr"
```
## Notes
--
2.51.0
Re: [PATCH hello-hut 2/3] cli: add --lang to pick the greeting language
On Mon, Oct 6, 2026 at 10:05, Noor Haddad wrote:
> + parser.add_argument("--lang", choices=sorted(TRANSLATIONS), default=None,
> + help="greeting language (default: en)")
Nice touch using choices= so typos get a helpful error. Should the
config file's "lang" be validated the same way? Not a blocker.
Reviewed-by: Mira Okafor <mira@example.org >
Noor Haddad <noor@example.org > writes:
> This series adds localized greetings, as discussed on the tracker.
Applied to next, thanks Noor! I'll fold Mira's suggestion about
validating the config value into the 0.3.0 work.
Dana