-
Notifications
You must be signed in to change notification settings - Fork 0
Bump to 1.17.0 and some new i18n patterns #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 1.16.4 | ||
| 1.17.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -432,3 +432,109 @@ rules: | |
| impact: MEDIUM | ||
| confidence: LOW | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.python.i18n.no-hardcoded-print-concat | ||
| severity: WARNING | ||
| languages: | ||
| - python | ||
| pattern-either: | ||
| - pattern: print("..." + ...) | ||
| - pattern: print(... + "...") | ||
| message: >- | ||
| Avoid hardcoded or concatenated strings in print. Use an i18n translation function (e.g., _("key")) with .format() or f-strings. | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags hardcoded string literals concatenated in print calls to enforce localization | ||
| technology: | ||
| - python | ||
| impact: MEDIUM | ||
| confidence: LOW | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.python.i18n.no-hardcoded-strftime | ||
| severity: WARNING | ||
| languages: | ||
| - python | ||
| pattern: $X.strftime("...") | ||
| message: >- | ||
| Avoid hardcoded date format strings in strftime. Use locale.nl_langinfo(locale.D_FMT) or similar locale-aware formatting. | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags hardcoded date format strings passed to strftime to enforce locale-aware date formatting | ||
| technology: | ||
| - python | ||
| impact: MEDIUM | ||
| confidence: HIGH | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.python.i18n.no-hardcoded-number-format | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ LOW RISK Suggestion: The rationale for flagging hardcoded number formatting is that decimal separators (period vs. comma) are locale-dependent. Using |
||
| severity: WARNING | ||
| languages: | ||
| - python | ||
| pattern-regex: ":\\.[0-9]+f" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK The global regex |
||
| message: >- | ||
| Avoid using :.Nf format specifiers for user-visible number formatting. Use locale.currency() or locale.format_string() for locale-aware formatting. | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags :.Nf format specifiers in f-strings used for user-visible numbers instead of locale-aware formatting | ||
| technology: | ||
| - python | ||
| impact: MEDIUM | ||
| confidence: LOW | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.cpp.i18n.no-hardcoded-cout | ||
| severity: WARNING | ||
| languages: | ||
| - cpp | ||
| pattern: std::cout << "$MSG" | ||
| message: >- | ||
| Avoid hardcoded strings in std::cout. Use a localization function or resource bundle for user-facing output. | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags hardcoded string literals streamed directly to std::cout to enforce localization | ||
| technology: | ||
| - cpp | ||
| impact: MEDIUM | ||
| confidence: LOW | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.cpp.i18n.no-hardcoded-strftime | ||
| severity: WARNING | ||
| languages: | ||
| - cpp | ||
| pattern-either: | ||
| - pattern: std::strftime($BUF, $SIZE, "$FMT", $TIME); | ||
| - pattern: strftime($BUF, $SIZE, "$FMT", $TIME); | ||
| message: >- | ||
| Avoid hardcoded date format strings in strftime. Use locale-aware date formatting (e.g., std::put_time with a locale-imbued stream). | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags hardcoded date format strings passed to strftime to enforce locale-aware date formatting | ||
| technology: | ||
| - cpp | ||
| impact: MEDIUM | ||
| confidence: HIGH | ||
| likelihood: HIGH | ||
|
|
||
| - id: codacy.cpp.i18n.no-hardcoded-number-format | ||
| severity: WARNING | ||
| languages: | ||
| - cpp | ||
| pattern: std::setprecision($N) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Suggestion: This rule will flag technical formatting that doesn't require localization (e.g., logging or data serialization). Narrow the |
||
| message: >- | ||
| Avoid using std::setprecision for user-visible number formatting. Imbue the stream with a locale and use std::use_facet<std::numpunct> for locale-aware output. | ||
| metadata: | ||
| category: codestyle | ||
| subcategory: i18n | ||
| description: Flags std::setprecision used for user-visible number formatting instead of locale-aware alternatives | ||
| technology: | ||
| - cpp | ||
| impact: MEDIUM | ||
| confidence: LOW | ||
| likelihood: HIGH | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import datetime | ||
| import locale | ||
| import gettext | ||
|
|
||
| # Set up gettext (only English & French, but French translations missing some keys) | ||
| locales = { | ||
| "en": gettext.translation("messages", localedir="locales", languages=["en"], fallback=True), | ||
| "fr": gettext.translation("messages", localedir="locales", languages=["fr"], fallback=True), | ||
| } | ||
|
|
||
| current_locale = "en" | ||
| _ = locales[current_locale].gettext | ||
|
|
||
| orders = [ | ||
| {"id": 1, "customer": "Alice", "amount": 1234.56, "date": datetime.date.today()}, | ||
| {"id": 2, "customer": "Bob", "amount": 98765.43, "date": datetime.date.today()}, | ||
| ] | ||
|
|
||
|
|
||
| def switch_language(lang): | ||
| global _, current_locale | ||
| if lang in locales: | ||
| current_locale = lang | ||
| _ = locales[lang].gettext | ||
| else: | ||
| print(f"Language {lang} not supported, falling back to English") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Suggestion: This fallback error message is hardcoded and won't be translated, which is inconsistent with the i18n practices being established. Since the new 'no-hardcoded-print-concat' rule only flags concatenation, this f-string is missed. Use a translation wrapper: |
||
| current_locale = "en" | ||
| _ = locales["en"].gettext | ||
|
|
||
|
|
||
| def add_order(customer, amount): | ||
| today = datetime.date.today() | ||
|
|
||
| # ❌ BAD: Hardcoded English + concatenation | ||
| print("Order for " + customer + " created on " + str(today)) | ||
|
|
||
| # ✅ GOOD: Proper i18n message | ||
| print(_("Order for {customer} created on {date}").format(customer=customer, date=today)) | ||
|
|
||
| orders.append({"id": len(orders) + 1, "customer": customer, "amount": amount, "date": today}) | ||
|
|
||
|
|
||
| def list_orders(): | ||
| print(_("Order List")) | ||
| print("------------") | ||
| for o in orders: | ||
| # ❌ BAD: Hardcoded date format | ||
| print(f"{o['customer']} | {o['date'].strftime('%m/%d/%Y')} | ${o['amount']:.2f}") | ||
|
|
||
| # ✅ GOOD: Locale-aware formatting | ||
| locale.setlocale(locale.LC_ALL, current_locale) | ||
| formatted_date = o['date'].strftime(locale.nl_langinfo(locale.D_FMT)) | ||
| formatted_amount = locale.currency(o['amount'], grouping=True) | ||
|
Comment on lines
+49
to
+53
|
||
| print(f"{o['customer']} | {formatted_date} | {formatted_amount}") | ||
|
|
||
|
|
||
| def summary(): | ||
| total = sum(o["amount"] for o in orders) | ||
|
|
||
| # ❌ BAD: Hardcoded string | ||
| print("Total Orders: " + str(len(orders))) | ||
| print("Total Revenue: $" + str(total)) | ||
|
|
||
| # ✅ GOOD: Localized message | ||
| print(_("Total Orders: {count}").format(count=len(orders))) | ||
| print(_("Total Revenue: {revenue}").format(revenue=locale.currency(total, grouping=True))) | ||
|
Comment on lines
+64
to
+66
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(_("Welcome to Order Management System")) | ||
|
|
||
| list_orders() | ||
|
|
||
| add_order("Charlie", 555.75) | ||
|
|
||
| print("\nAfter Adding Order:") | ||
| list_orders() | ||
|
|
||
| summary() | ||
|
|
||
| print("\nSwitching to French (missing translations -> fallback):") | ||
| switch_language("fr") | ||
| list_orders() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI/README build examples pass
--build-arg TOOL_VERSION=$(cat .tool_version), but this Dockerfile only defines/usesOPENGREP_VERSION. As a result, the build arg has no effect and.tool_versioncan drift from the opengrep binary version. Consider renaming the ARG toTOOL_VERSION(or updating CI/docs to passOPENGREP_VERSION) and wiring it through consistently.