Uppercase first letter of string (sentence case)

A recurring problem I can’t solve directly using the existing Hugo template functions is to convert i18n values (or in general, any predefined strings) to sentence case.

Consider the following translations (for simplicity as a table):

i18n key en de fr
evoting e-voting E-Voting vote électronique

I’d like to be able to use the translation above as-is (e.g. for use in sentences) as well as with the first letter uppercased (e.g. for use in titles).

The existing functions title and humanize both don’t yield sentence case:

expr en de fr
i18n "evoting" | title E-Voting E-Voting Vote Électronique
i18n "evoting" | humanize E voting E voting Vote électronique

I know I can resort to a partial as a kind of custom function to implement what I want. A one-liner is enough:

{{ print (slicestr . 0 1 | upper) (slicestr . 1) -}}

When saved as e.g. layouts/partials/upper1st.html, I can then use {{ i18n "evoting" | partial "upper1st" }} to convert the above translation to sentence case.

But since this is such a basic task, I would wish for a dedicated template function to do this, say sentencify, instead of having to rely on a partial.

Anyone feels the same? Any chance this could be added to Hugo?

1 Like
{{ "hello world" | strings.FirstUpper }} --> Hello world

It’s in the Go docs
https://pkg.go.dev/github.com/gohugoio/hugo/tpl/strings

But not in our docs:
https://github.com/gohugoio/hugoDocs/issues/739

3 Likes

Thank you, that is very helpful! I wasn’t aware of this Hugo Go pkg doc up until now…

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.