Skip to content

Instantly share code, notes, and snippets.

@RichardBradley
Created August 4, 2025 10:14
Show Gist options
  • Select an option

  • Save RichardBradley/70dfa1cae92ff3d2c7aa9377eb6d4d8e to your computer and use it in GitHub Desktop.

Select an option

Save RichardBradley/70dfa1cae92ff3d2c7aa9377eb6d4d8e to your computer and use it in GitHub Desktop.
localised-character-count

Localised Character Count

This is adapted from the Gov UK character count component, to allow localisation of characters under/at/over limit text. It uses translations of the standard phrases for these.

Localised Text Details

Field Pluralisation English (Default from Gov UK Component) Welsh
charactersUnderLimitText one You have %{count} character remaining Mae gennych %{count} nod yn weddill
other You have %{count} characters remaining Mae gennych %{count} o nodau yn weddill
charactersAtLimitText You have 0 characters remaining Nid oes gennych unrhyw nodau ar ôl
charactersOverLimitText one You have %{count} character too many Mae gennych %{count} nod yn ormod
other You have %{count} characters too many Mae gennych %{count} o nodau yn ormod

localisedCharacterCount Nunjucks Macro

Parameters

For full details of parameters accepted by GovUK Character Count component see Nunjucks maro options in Gov UK documentation. This component will override any values provided for the fields charactersUnderLimitText, charactersAtLimitText, and charactersOverLimitText as above. The component takes an additional boolean parameter isWelshLocale which is used to determine whether to use the Welsh or English language phrases for the character limit texts. If not set the English language phrases will be used.

Name Type Description
id string The ID of the textarea. Defaults to the value of name.
name string Required. The name of the textarea, which is submitted with the form data.
rows string Optional number of textarea rows (default is 5 rows).
value string Optional initial value of the textarea.
maxlength string Required. If maxwords is set, this is not required. The maximum number of characters. If maxwords is provided, the maxlength option will be ignored.
maxwords string Required. If maxlength is set, this is not required. The maximum number of words. If maxwords is provided, the maxlength option will be ignored.
threshold string The percentage value of the limit at which point the count message is displayed. If this attribute is set, the count message will be hidden by default.
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the error message component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Options for the form-group wrapper. See formGroup.
classes string Classes to add to the textarea.
attributes object HTML attributes (for example data attributes) to add to the textarea.
spellcheck boolean Optional field to enable or disable the spellcheck attribute on the character count.
countMessage object Options for the count message. See countMessage.
textareaDescriptionText string Message made available to assistive technologies to describe that the component accepts only a limited amount of content. It is visible on the page when JavaScript is unavailable. The component will replace the %{count} placeholder with the value of the maxlength or maxwords parameter.
wordsUnderLimitText object Message displayed when the number of words is under the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of remaining words. This is a pluralised list of messages.
wordsAtLimitText string Message displayed when the number of words reaches the configured maximum, maxwords. This message is displayed visually and through assistive technologies.
wordsUnderLimitText object Message displayed when the number of words is over the configured maximum, maxwords. This message is displayed visually and through assistive technologies. The component will replace the %{count} placeholder with the number of characters above the maximum. This is a pluralised list of messages.
isWelshLocale boolean Whether the page is being displayed in the welsh locale, determines whether or not the character limit texts are set in Welsh

Options for formGroup

Name Type Description
classes string Classes to add to the form group (for example to show error state for the whole group).

Options for countMessage

Name Type Description
classes string Classes to add to the count message.

Options for label

Name Type Description
text string Required. If html is set, this is not required. Text to use within the label. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the label. If html is provided, the text argument will be ignored.
for string The value of the for attribute, the ID of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.

Options for hint

Name Type Description
text string Required. If html is set, this is not required. Text to use within the hint. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the hint. If html is provided, the text argument will be ignored.
id string Optional ID attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag
{% macro localisedCharacterCount(params) %}
{%- include "./template.njk" -%}
{% endmacro %}
{% from "components/character-count/macro.njk" import govukCharacterCount %}
{% set passedParams = {
id: params.id,
name: params.name,
rows: params.rows,
value: params.value,
maxlength: params.maxlength,
maxwords: params.maxwords,
threshold: params.threshold,
label: params.label,
hint: params.hint,
errorMessage: params.errorMessage,
formGroup: params.formGroup,
classes: params.classes,
attributes: params.attributes,
spellcheck: params.spellcheck,
countMessage: params.countMessage,
textareaDescriptionText: params.textareaDescriptionText,
wordsUnderLimitText: params.wordsUnderLimitText,
wordsAtLimitText: params.wordsAtLimitText,
wordsUnderLimitText: params.wordsUnderLimitText,
lang: 'cy' if params.isWelshLocale,
charactersUnderLimitText: {
one: 'Mae gennych %{count} nod yn weddill',
other: 'Mae gennych %{count} o nodau yn weddill'
} if params.isWelshLocale,
charactersAtLimitText: 'Nid oes gennych unrhyw nodau ar ôl' if params.isWelshLocale,
charactersOverLimitText: {
one: 'Mae gennych %{count} nod yn ormod',
other: 'Mae gennych %{count} o nodau yn ormod'
} if params.isWelshLocale
} %}
{{ govukCharacterCount(passedParams) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment