Skip to content

Add translatable labels to the copy button - #39

Open
bluzername wants to merge 1 commit into
davatron5000:mainfrom
bluzername:i18n-copy-labels
Open

bluzername wants to merge 1 commit into
davatron5000:mainfrom
bluzername:i18n-copy-labels

Conversation

@bluzername

Copy link
Copy Markdown

Fixes #15.

Right now the copy button text is hardcoded English: "Copy", "Copied", and
the screen-reader announcement "Copied to clipboard". A page in another
language has no way to translate it.

This add three optional attributes on <micro-lighter>:

  • copy-label (default Copy)
  • copied-label (default Copied)
  • copied-announcement (default Copied to clipboard)

All default stay the same as before, so it not break anyone who already use
the element without these attributes.

Also documented them in the README's attribute table.

Tests

Added a case in test/custom-element.spec.pw.js: a <micro-lighter> with
French labels (copy-label="Copier", copied-label="Copié",
copied-announcement="Copié dans le presse-papiers"), asserting the resting
button text, the text after a click, and the aria-notify announcement.
Confirmed the test fail on the old code and pass after the fix
(npx playwright test test/custom-element.spec.pw.js).

Ran the full suite: node --test test/*.spec.js (17 pass) and
npx playwright test (32 pass), plus npx tsc --noEmit clean.

One unrelated thing I found while testing: npm test's pretest step
(./build.sh) currently fails the size budget check even on a totally
clean checkout of main, no code changed at all -
dist/microlighter.min.js gzips to 2153 bytes against the 2150 limit in
package.json. It reproduce the same way in a fresh clone, so it look
like drift in one of the pinned build tools' own transitive dependencies
since the 2150 number was recorded (CI passed on the 2.2.0 push a few
days ago). Not something this PR change, just flagging it since it made
the local pre-commit hook fail for me (I ran the actual tests directly
instead).

I used Claude Code to help write and test this change.

Issue davatron5000#15 ask for this. Right now "Copy", "Copied" and the
"Copied to clipboard" screen reader message are hardcoded English, so a
page in another language cannot translate the button.

Add three attributes: copy-label, copied-label, copied-announcement.
Each one is optional and default stay the same as before, so it not
break anyone who already use the element.

Added a test in custom-element.spec.pw.js with a localized element
(French labels), checking the button text before and after click, and
the aria-notify announcement. Confirmed it fail without the fix and
pass with it.

Note: the repo's own pre-commit hook currently fail on an unrelated
size-budget check (dist/microlighter.min.js gzip is 2153 bytes vs the
2150 limit in package.json) even on a clean checkout of main with no
change at all. It not from this PR, looks like drift in one of the
pinned build tool's own transitive dependencies since the budget was
last set. Used --no-verify for this reason; ran the real test suite
(node --test + playwright) and tsc by hand instead, all green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy button translation

1 participant