Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for localisation via JavaScript configuration to Accordion component #2826

Merged
merged 5 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ If you're not using the Nunjucks macro, you can customise these using data-* att
- `data-i18n.show-all-sections`
- `data-i18n.hide-all-sections`

This was added in [pull request #2818: Add support for localisation via data-* attributes to Accordion component](https://github.com/alphagov/govuk-frontend/pull/2818).
You can also change this text for all instances of the Accordion using a JavaScript configuration object. See [our guidance on localising GOV.UK Frontend](https://design-system.service.gov.uk/get-started/localisation/) for how to do this.

This was added in pull requests:

- [#2818: Add support for localisation via data-* attributes to Accordion component](https://github.com/alphagov/govuk-frontend/pull/2818)
- [#2826: Add support for localisation via JavaScript configuration to Accordion component](https://github.com/alphagov/govuk-frontend/pull/2826)

### Recommended changes

Expand Down
16 changes: 16 additions & 0 deletions app/views/examples/translated/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,19 @@
iconFallbackText: "Rhybudd"
}) }}
{% endblock %}

{% block bodyEnd %}
<script src="/public/all.js"></script>
<script>
window.GOVUKFrontend.initAll({
accordion: {
i18n: {
showAllSections: "Dangos adrannau",
hideAllSections: "Cuddio adrannau",
},
"i18n.showSection": "Dangos<span class=\"govuk-visually-hidden\"> adran</span>",
"i18n.hideSection": "Cuddio<span class=\"govuk-visually-hidden\"> adran</span>",
}
})
</script>
{% endblock %}
2 changes: 1 addition & 1 deletion src/govuk/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function initAll (options) {

var $accordions = scope.querySelectorAll('[data-module="govuk-accordion"]')
nodeListForEach($accordions, function ($accordion) {
new Accordion($accordion).init()
new Accordion($accordion, options.accordion).init()
})

var $details = scope.querySelectorAll('[data-module="govuk-details"]')
Expand Down
10 changes: 10 additions & 0 deletions src/govuk/common.unit.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('Common JS utilities', () => {
})
})

it('ignores empty objects when merging', () => {
const test1 = mergeConfigs({}, config1)
const test2 = mergeConfigs(config1, {}, config2)
const test3 = mergeConfigs(config3, {})

expect(test1).toEqual(mergeConfigs(config1))
expect(test2).toEqual(mergeConfigs(config1, config2))
expect(test3).toEqual(mergeConfigs(config3))
})

it('prioritises the last parameter provided', () => {
const config = mergeConfigs(config1, config2, config3, config1)
expect(config).toEqual({
Expand Down
4 changes: 2 additions & 2 deletions src/govuk/components/accordion/accordion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import I18n from '../../i18n.mjs'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'

function Accordion ($module) {
function Accordion ($module, config) {
this.$module = $module
this.$sections = $module.querySelectorAll('.govuk-accordion__section')
this.$showAllButton = ''
Expand All @@ -34,7 +34,7 @@ function Accordion ($module) {
showSection: 'Show<span class="govuk-visually-hidden"> this section</span>'
}
}
this.config = mergeConfigs(defaultConfig, $module.dataset)
this.config = mergeConfigs(defaultConfig, config || {}, $module.dataset)
querkmachine marked this conversation as resolved.
Show resolved Hide resolved
this.i18n = new I18n(extractConfigByNamespace(this.config, 'i18n'))

this.controlsClass = 'govuk-accordion__controls'
Expand Down
34 changes: 34 additions & 0 deletions src/govuk/components/accordion/accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ describe('/components/accordion', () => {
expect(allSectionsToggleText).toEqual(showAllSectionsDataAttribute)
})

it('should localise "Show all sections" based on JavaScript configuration', async () => {
await page.goto(baseUrl + '/examples/translated', { waitUntil: 'load' })

const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)

expect(allSectionsToggleText).toBe('Dangos adrannau')
})

it('should localise "Hide all sections" based on data attribute', async () => {
await page.goto(baseUrl + '/components/accordion/with-translations/preview', { waitUntil: 'load' })
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
Expand All @@ -283,6 +291,15 @@ describe('/components/accordion', () => {
expect(allSectionsToggleText).toEqual(hideAllSectionsDataAttribute)
})

it('should localise "Hide all sections" based on JavaScript configuration', async () => {
await page.goto(baseUrl + '/examples/translated', { waitUntil: 'load' })
await page.click('.govuk-accordion .govuk-accordion__show-all')

const allSectionsToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__show-all-text').innerHTML)

expect(allSectionsToggleText).toBe('Cuddio adrannau')
})

it('should localise "Show section" based on data attribute', async () => {
await page.goto(baseUrl + '/components/accordion/with-translations/preview', { waitUntil: 'load' })

Expand All @@ -292,6 +309,14 @@ describe('/components/accordion', () => {
expect(firstSectionToggleText).toEqual(showSectionDataAttribute)
})

it('should localise "Show section" based on JavaScript configuration', async () => {
await page.goto(baseUrl + '/examples/translated', { waitUntil: 'load' })

const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)

expect(firstSectionToggleText).toBe('Dangos<span class="govuk-visually-hidden"> adran</span>')
})

it('should localise "Hide section" based on data attribute', async () => {
await page.goto(baseUrl + '/components/accordion/with-translations/preview', { waitUntil: 'load' })
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')
Expand All @@ -301,6 +326,15 @@ describe('/components/accordion', () => {

expect(firstSectionToggleText).toEqual(hideSectionDataAttribute)
})

it('should localise "Hide section" based on JavaScript configuration', async () => {
await page.goto(baseUrl + '/examples/translated', { waitUntil: 'load' })
await page.click('.govuk-accordion .govuk-accordion__section:nth-of-type(2) .govuk-accordion__section-header')

const firstSectionToggleText = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle-text').innerHTML)

expect(firstSectionToggleText).toBe('Cuddio<span class="govuk-visually-hidden"> adran</span>')
})
})
})
})
Expand Down