Skip to content
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- **Localizable seek value text (`seekValueText`).** New option templating the
seek slider's spoken `aria-valuetext`; `%1$s` is the current time and `%2$s`
the total duration (both formatted `M:SS`), with sequential `%s` and reordered
positional args supported for translation. Defaults to `'%1$s of %2$s'`, so
existing output is unchanged. Also settable via `data-seek-value-text`. Lets
consumers localize the connective text without reformatting the times.

## [1.19.3] — 2026-07-02

### Fixed
Expand Down
19 changes: 18 additions & 1 deletion dist/waveform-player.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,19 @@ function parseDataAttributes(element) {
setBool("showMarkers");
setBool("accessibleSeek");
if (element.dataset.seekLabel) options.seekLabel = element.dataset.seekLabel;
if (element.dataset.seekValueText) options.seekValueText = element.dataset.seekValueText;
if (element.dataset.errorText) options.errorText = element.dataset.errorText;
if (element.dataset.playIcon) options.playIcon = element.dataset.playIcon;
if (element.dataset.pauseIcon) options.pauseIcon = element.dataset.pauseIcon;
return options;
}
function formatSeekValueText(template, ...args) {
let sequentialIndex = 0;
return template.replace(/%(?:(\d+)\$)?s/g, (match, position) => {
const index = position ? Number(position) - 1 : sequentialIndex++;
return args[index] ?? match;
});
}
function formatTime(seconds) {
if (!seconds || isNaN(seconds) || seconds < 0) return "0:00";
const hrs = Math.floor(seconds / 3600);
Expand Down Expand Up @@ -752,8 +760,13 @@ var DEFAULT_OPTIONS = {
// (role="slider" + ARIA value attributes + arrow/page/home/end seeking).
// seekLabel sets the slider's accessible name; when null it falls back
// to the track title, then 'Seek'.
// seekValueText templates the slider's spoken aria-valuetext: %1$s is the
// current time and %2$s the total duration (both formatted M:SS). When null
// it falls back to '%1$s of %2$s'. Lets consumers localize the connective
// text without reformatting the times.
accessibleSeek: true,
seekLabel: null,
seekValueText: null,
// Content
title: null,
artist: null,
Expand Down Expand Up @@ -1340,7 +1353,11 @@ var WaveformPlayer = class _WaveformPlayer {
this.seekEl.setAttribute("aria-valuenow", String(Math.round(current)));
this.seekEl.setAttribute(
"aria-valuetext",
`${formatTime(current)} of ${formatTime(duration)}`
formatSeekValueText(
this.options.seekValueText || "%1$s of %2$s",
formatTime(current),
formatTime(duration)
)
);
}
/**
Expand Down
4 changes: 2 additions & 2 deletions dist/waveform-player.cjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/waveform-player.esm.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/waveform-player.esm.js.map

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion dist/waveform-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@
setBool("showMarkers");
setBool("accessibleSeek");
if (element.dataset.seekLabel) options.seekLabel = element.dataset.seekLabel;
if (element.dataset.seekValueText) options.seekValueText = element.dataset.seekValueText;
if (element.dataset.errorText) options.errorText = element.dataset.errorText;
if (element.dataset.playIcon) options.playIcon = element.dataset.playIcon;
if (element.dataset.pauseIcon) options.pauseIcon = element.dataset.pauseIcon;
return options;
}
function formatSeekValueText(template, ...args) {
let sequentialIndex = 0;
return template.replace(/%(?:(\d+)\$)?s/g, (match, position) => {
const index = position ? Number(position) - 1 : sequentialIndex++;
return args[index] ?? match;
});
}
function formatTime(seconds) {
if (!seconds || isNaN(seconds) || seconds < 0) return "0:00";
const hrs = Math.floor(seconds / 3600);
Expand Down Expand Up @@ -727,8 +735,13 @@
// (role="slider" + ARIA value attributes + arrow/page/home/end seeking).
// seekLabel sets the slider's accessible name; when null it falls back
// to the track title, then 'Seek'.
// seekValueText templates the slider's spoken aria-valuetext: %1$s is the
// current time and %2$s the total duration (both formatted M:SS). When null
// it falls back to '%1$s of %2$s'. Lets consumers localize the connective
// text without reformatting the times.
accessibleSeek: true,
seekLabel: null,
seekValueText: null,
// Content
title: null,
artist: null,
Expand Down Expand Up @@ -1315,7 +1328,11 @@
this.seekEl.setAttribute("aria-valuenow", String(Math.round(current)));
this.seekEl.setAttribute(
"aria-valuetext",
`${formatTime(current)} of ${formatTime(duration)}`
formatSeekValueText(
this.options.seekValueText || "%1$s of %2$s",
formatTime(current),
formatTime(duration)
)
);
}
/**
Expand Down
4 changes: 2 additions & 2 deletions dist/waveform-player.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/waveform-player.min.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ export interface WaveformPlayerOptions {
accessibleSeek?: boolean;
/** Accessible name for the seek slider (falls back to the title, then `'Seek'`). @default null */
seekLabel?: string | null;
/**
* Template for the seek slider's spoken `aria-valuetext`. `%1$s` is the
* current time and `%2$s` the total duration (both formatted `M:SS`);
* sequential `%s` and reordered positional args are also supported. Lets
* consumers localize the connective text without reformatting the times.
* Falls back to `'%1$s of %2$s'`.
* @default null
*/
seekValueText?: string | null;

// ── Content metadata ──────────────────────────────────────────
title?: string | null;
Expand Down
7 changes: 6 additions & 1 deletion src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {draw} from './drawing.js';
import {generateWaveform, generatePlaceholderWaveform} from './audio.js';
import {
formatTime,
formatSeekValueText,
extractTitleFromUrl,
generateId,
parseDataAttributes,
Expand Down Expand Up @@ -722,7 +723,11 @@ export class WaveformPlayer {
this.seekEl.setAttribute('aria-valuenow', String(Math.round(current)));
this.seekEl.setAttribute(
'aria-valuetext',
`${formatTime(current)} of ${formatTime(duration)}`
formatSeekValueText(
this.options.seekValueText || '%1$s of %2$s',
formatTime(current),
formatTime(duration)
)
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/js/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ export const DEFAULT_OPTIONS = {
// (role="slider" + ARIA value attributes + arrow/page/home/end seeking).
// seekLabel sets the slider's accessible name; when null it falls back
// to the track title, then 'Seek'.
// seekValueText templates the slider's spoken aria-valuetext: %1$s is the
// current time and %2$s the total duration (both formatted M:SS). When null
// it falls back to '%1$s of %2$s'. Lets consumers localize the connective
// text without reformatting the times.
accessibleSeek: true,
seekLabel: null,
seekValueText: null,

// Content
title: null,
Expand Down
20 changes: 20 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function parseDataAttributes(element) {
// Accessibility
setBool('accessibleSeek');
if (element.dataset.seekLabel) options.seekLabel = element.dataset.seekLabel;
if (element.dataset.seekValueText) options.seekValueText = element.dataset.seekValueText;
if (element.dataset.errorText) options.errorText = element.dataset.errorText;

// Custom icons (raw SVG markup)
Expand All @@ -229,6 +230,25 @@ export function parseDataAttributes(element) {
return options;
}

/**
* Interpolate a printf-style seek value-text template.
*
* Substitutes positional (`%1$s`, `%2$s`) and sequential (`%s`) placeholders so
* a localized template resolves the same way `sprintf` would — including
* templates that reorder the arguments for a given language. Placeholders with
* no matching argument are left intact.
* @param {string} template - Value-text template, e.g. `'%1$s of %2$s'`.
* @param {...string} args - Replacement values, in order (current, duration).
* @returns {string} The interpolated value text.
*/
export function formatSeekValueText(template, ...args) {
let sequentialIndex = 0;
return template.replace(/%(?:(\d+)\$)?s/g, (match, position) => {
const index = position ? Number(position) - 1 : sequentialIndex++;
return args[index] ?? match;
});
}

/**
* Format a duration as a clock string.
*
Expand Down
37 changes: 37 additions & 0 deletions test/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ describe('accessible seek slider', () => {
expect(seek.detail.percent).toBe(0);
});

it('localizes aria-valuetext with a custom seekValueText template (Spanish)', () => {
const { el, player } = track(
mount({ showTime: false, seekValueText: '%1$s de %2$s' })
);
const slider = el.querySelector('.waveform-container');

player.setProgress(30, 120);
expect(slider.getAttribute('aria-valuetext')).toBe('0:30 de 2:00');
});

it('supports reordered positional args in a translated template', () => {
// A translation may place the total before the current time; %2$s/%1$s
// must resolve independently of source order.
const { el, player } = track(
mount({ showTime: false, seekValueText: '%2$s en total, %1$s actual' })
);
const slider = el.querySelector('.waveform-container');

player.setProgress(30, 120);
expect(slider.getAttribute('aria-valuetext')).toBe(
'2:00 en total, 0:30 actual'
);
});

it('reads seekValueText from the data-seek-value-text attribute', () => {
const el = document.createElement('div');
el.dataset.seekValueText = '%1$s de %2$s';
document.body.appendChild(el);
const player = track(
new WaveformPlayer(el, { audioMode: 'external', showTime: false })
);
const slider = el.querySelector('.waveform-container');

player.setProgress(30, 120);
expect(slider.getAttribute('aria-valuetext')).toBe('0:30 de 2:00');
});

it('toggles aria-busy while loading', () => {
const { el, player } = track(mount());
const slider = el.querySelector('.waveform-container');
Expand Down
25 changes: 25 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import {
formatTime,
formatSeekValueText,
generateId,
mergeOptions,
extractTitleFromUrl,
Expand Down Expand Up @@ -78,6 +79,30 @@ describe('formatTime', () => {
});
});

describe('formatSeekValueText', () => {
it('substitutes positional placeholders', () => {
expect(formatSeekValueText('%1$s of %2$s', '0:30', '2:00')).toBe(
'0:30 of 2:00'
);
});

it('substitutes sequential %s placeholders in order', () => {
expect(formatSeekValueText('%s / %s', '0:30', '2:00')).toBe(
'0:30 / 2:00'
);
});

it('resolves reordered positional args independently of source order', () => {
expect(formatSeekValueText('%2$s, %1$s', '0:30', '2:00')).toBe(
'2:00, 0:30'
);
});

it('leaves placeholders with no matching argument intact', () => {
expect(formatSeekValueText('%1$s of %2$s', '0:30')).toBe('0:30 of %2$s');
});
});

describe('generateId', () => {
it('produces element-id-safe strings', () => {
expect(generateId('https://x.com/a.mp3')).toMatch(/^wp_[0-9a-z]+_[0-9a-z]+$/);
Expand Down