Skip to content

Conversation

@chaule97
Copy link

@chaule97 chaule97 commented Nov 4, 2024

odoo/odoo#52155

Starting from Odoo 14.0, the _() function supports named placeholders, which eliminates the need for manual string formatting like format() or %. I have written a script to convert old code to the new style.

  • convert _("It's %s") % 2024, _("It's %s") % (2024), _("It's {}").format(2024) to _("It's %s", 2024)

  • convert _("User %(name)s has %(items)s items") % {"name":"Dev", "items": 5}, _("User {name} has {items} items").format(name="Dev", items=5) to _("User %(name)s has %(items)s items", name="Dev", items=5)

  • show warning if we use unnamed placeholders in translated string, example: _("%s are only valid until %s", "User", 2024)

Copy link
Member

@StefanRijnhart StefanRijnhart left a comment

Choose a reason for hiding this comment

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

Really nice! Maybe you can improve the commit message (+ pr title) a little bit? Something like [IMP] rewrite translatable strings to use named placeholders.

Refactor translatable strings to use named placeholders instead of positional ones.
This improves readability, reduces the risk of translation errors, and makes
translations easier to maintain and reorder across languages.
@chaule97 chaule97 changed the title [IMP] _() improvements [IMP] rewrite translatable strings to use named placeholders Dec 26, 2025
@chaule97
Copy link
Author

Hallo @StefanRijnhart , I updated commit message

Copy link
Member

@StefanRijnhart StefanRijnhart left a comment

Choose a reason for hiding this comment

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

Perfect, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants