|
| 1 | +import datetime |
| 2 | +import locale |
| 3 | +import gettext |
| 4 | + |
| 5 | +# Set up gettext (only English & French, but French translations missing some keys) |
| 6 | +locales = { |
| 7 | + "en": gettext.translation("messages", localedir="locales", languages=["en"], fallback=True), |
| 8 | + "fr": gettext.translation("messages", localedir="locales", languages=["fr"], fallback=True), |
| 9 | +} |
| 10 | + |
| 11 | +current_locale = "en" |
| 12 | +_ = locales[current_locale].gettext |
| 13 | + |
| 14 | +orders = [ |
| 15 | + {"id": 1, "customer": "Alice", "amount": 1234.56, "date": datetime.date.today()}, |
| 16 | + {"id": 2, "customer": "Bob", "amount": 98765.43, "date": datetime.date.today()}, |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def switch_language(lang): |
| 21 | + global _, current_locale |
| 22 | + if lang in locales: |
| 23 | + current_locale = lang |
| 24 | + _ = locales[lang].gettext |
| 25 | + else: |
| 26 | + print(f"Language {lang} not supported, falling back to English") |
| 27 | + current_locale = "en" |
| 28 | + _ = locales["en"].gettext |
| 29 | + |
| 30 | + |
| 31 | +def add_order(customer, amount): |
| 32 | + today = datetime.date.today() |
| 33 | + |
| 34 | + # ❌ BAD: Hardcoded English + concatenation |
| 35 | + print("Order for " + customer + " created on " + str(today)) |
| 36 | + |
| 37 | + # ✅ GOOD: Proper i18n message |
| 38 | + print(_("Order for {customer} created on {date}").format(customer=customer, date=today)) |
| 39 | + |
| 40 | + orders.append({"id": len(orders) + 1, "customer": customer, "amount": amount, "date": today}) |
| 41 | + |
| 42 | + |
| 43 | +def list_orders(): |
| 44 | + print(_("Order List")) |
| 45 | + print("------------") |
| 46 | + for o in orders: |
| 47 | + # ❌ BAD: Hardcoded date format |
| 48 | + print(f"{o['customer']} | {o['date'].strftime('%m/%d/%Y')} | ${o['amount']:.2f}") |
| 49 | + |
| 50 | + # ✅ GOOD: Locale-aware formatting |
| 51 | + locale.setlocale(locale.LC_ALL, current_locale) |
| 52 | + formatted_date = o['date'].strftime(locale.nl_langinfo(locale.D_FMT)) |
| 53 | + formatted_amount = locale.currency(o['amount'], grouping=True) |
| 54 | + print(f"{o['customer']} | {formatted_date} | {formatted_amount}") |
| 55 | + |
| 56 | + |
| 57 | +def summary(): |
| 58 | + total = sum(o["amount"] for o in orders) |
| 59 | + |
| 60 | + # ❌ BAD: Hardcoded string |
| 61 | + print("Total Orders: " + str(len(orders))) |
| 62 | + print("Total Revenue: $" + str(total)) |
| 63 | + |
| 64 | + # ✅ GOOD: Localized message |
| 65 | + print(_("Total Orders: {count}").format(count=len(orders))) |
| 66 | + print(_("Total Revenue: {revenue}").format(revenue=locale.currency(total, grouping=True))) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + print(_("Welcome to Order Management System")) |
| 71 | + |
| 72 | + list_orders() |
| 73 | + |
| 74 | + add_order("Charlie", 555.75) |
| 75 | + |
| 76 | + print("\nAfter Adding Order:") |
| 77 | + list_orders() |
| 78 | + |
| 79 | + summary() |
| 80 | + |
| 81 | + print("\nSwitching to French (missing translations -> fallback):") |
| 82 | + switch_language("fr") |
| 83 | + list_orders() |
0 commit comments