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
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>
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