Translating a variable
Bad :
def say(text):
print(_(text)) # <- not extracted but maybe translated here
say("Hello")
Good :
def say(text):
print(text)
say(_("Hello")) # <- extracted and translated here
We extract translatable strings from the source code. They can only be extracted if the original strings themselves are wrapped in the translation function.
The only exception where translating a variable is okay is when we can rely on the text being extracted in another way, e.g. doctype names and field labels.
Constructing sentences
Bad :
message = _("Created a {0} Invoice!").format(_(invoice_type))
Good :
if invoice_type == "Sales":
message = _("Created a Sales Invoice!")
elif invoice_type == "Purchase":
message = _("Created a Purchase Invoice!")
It’s easiest to translated whole sentences. Translating parts of a constructed sentence correctly is close to impossible.
Examples:
- fix: message for missing mandatory fields by barredterra · Pull Request #27659 · frappe/frappe · GitHub,
- fix(Opening Invoice Creation Tool): translatability of messages by barredterra · Pull Request #43056 · frappe/erpnext · GitHub
Translated document names
Bad :
cg = frappe.new_doc("Customer Group")
cg.name = _("Non Profit")
cg.insert()
# ...
cg = frappe.get_doc("Customer Group", _("Non Profit"))
# or, equally bad, in this case:
cg = frappe.get_doc("Customer Group", "Non Profit")
Good :
cg = frappe.new_doc("Customer Group")
cg.name = "Non Profit"
cg.insert()
# ...
cg = frappe.get_doc("Customer Group", "Non Profit")
Translations can change after insertion. The current user can have a different language than the one who created the record. Document names should be translated only for display, never in the database.
Examples: