Can Django autocapitalize first letters like how Laravel does?

Can Django autocapitalize first letters inside placeholders like how Laravel does?

Field name/attribute:

msgid "first_name"
msgstr "ime" 

Error message with normal placeholder:

msgid "field.alpha"
# field placeholder value is first_name, translated field name is "ime"
msgstr "{field} mora sadržati samo slova.

Result:

ime mora sadržati samo slova.

Error message with capitalized placeholder:

# field placeholder value is first_name, translated field name is "Ime"
msgstr "{Field} mora sadržati samo slova."

Result:

Ime mora sadržati samo slova.

Validator with translated error message with translated string that replaces placeholder.

self.fields["first_name"].validators.append(AlphaValidator(_("field.alpha").format(field=_("first_name"))))

Laravel validation.php:

return [
    "alpha" => ":Attribute mora sadržati samo slova.",

    "attributes" => [
        "first_name" => "ime",
    ],
];

How Laravel does:

  • Get translated attribute from attributes.first_name, which is ime
  • Replace the placeholder :attribute in the message :Attribute mora sadržati samo slova. with the translated attribute
  • If placeholder is capitalized (:Attribute), capitalize attribute too (Ime)
  • Result: Ime mora sadržati samo slova.
Вернуться на верх