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
6 changes: 5 additions & 1 deletion spp_programs/models/cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ def _compute_total_amount(self):
def _compute_total_amount_in_words(self):
for record in self:
if record.total_amount and record.currency_id:
amount_in_words = num2words(record.total_amount, lang="en").title()
lang_code = (self.env.context.get("lang") or self.env.user.lang or "en_US").split("_")[0]
try:
amount_in_words = num2words(record.total_amount, lang=lang_code).title()
except NotImplementedError:
amount_in_words = num2words(record.total_amount, lang="en").title()
record.total_amount_in_words = f"{amount_in_words} {record.currency_id.name}"
Comment on lines +263 to 268

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

By immediately splitting the language code on _ (e.g., converting pt_BR to pt), regional dialects supported by num2words (such as Brazilian Portuguese pt_BR or British English en_GB) are lost, falling back to the generic base language.

A more robust approach is to attempt translation using the full language code first, then fall back to the base language code (split by _), and finally fall back to English (en) if neither is supported.

Suggested change
lang_code = (self.env.context.get("lang") or self.env.user.lang or "en_US").split("_")[0]
try:
amount_in_words = num2words(record.total_amount, lang=lang_code).title()
except NotImplementedError:
amount_in_words = num2words(record.total_amount, lang="en").title()
record.total_amount_in_words = f"{amount_in_words} {record.currency_id.name}"
lang_code = self.env.context.get("lang") or self.env.user.lang or "en_US"
for lang in (lang_code, lang_code.split("_")[0], "en"):
try:
amount_in_words = num2words(record.total_amount, lang=lang).title()
break
except NotImplementedError:
continue
record.total_amount_in_words = f"{amount_in_words} {record.currency_id.name}"

else:
record.total_amount_in_words = ""
Expand Down
4 changes: 4 additions & 0 deletions spp_programs/tests/test_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ def test_total_amount_in_words(self, mock_today):
cycle._compute_total_amount_in_words()
self.assertIn("Five Hundred", cycle.total_amount_in_words)

cycle_fr = cycle.with_context(lang="fr_FR")
cycle_fr._compute_total_amount_in_words()
self.assertIn("cinq cents", cycle_fr.total_amount_in_words.lower())

def test_approval_state_mapping(self):
"""_compute_approval_state maps cycle state to the approval mixin state correctly."""
cycle = self._make_cycle(name="Approval State Mapping Cycle [CYCLE TEST]")
Expand Down