@banou/media-player 0.10.29 → 0.10.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -1
- package/dist/de-DphEdq0O.js +52 -0
- package/dist/engine/index.js +1 -1
- package/dist/{engine-DeEY2qao.js → engine-5XbZqrD3.js} +213 -162
- package/dist/es-419-D6gsvnn8.js +52 -0
- package/dist/fr-BqAzAJX0.js +52 -0
- package/dist/index.js +931 -844
- package/dist/it-NTcUh3Gr.js +52 -0
- package/dist/ja-DSmX_bxj.js +52 -0
- package/dist/ko-DyPzWasj.js +52 -0
- package/dist/labels.d.ts +71 -0
- package/dist/language/index.d.ts +6 -0
- package/dist/language/index.js +34 -27
- package/dist/pl-C6Ow4ra5.js +52 -0
- package/dist/pt-BR-wudK3giz.js +52 -0
- package/dist/react/labels.d.ts +28 -0
- package/dist/react/video-player.d.ts +9 -0
- package/dist/ru-D5kxBkik.js +52 -0
- package/dist/tr-CW7R7N9k.js +52 -0
- package/dist/utils/track-label.d.ts +9 -8
- package/dist/zh-Hans-IhL1Pl78.js +52 -0
- package/dist/zh-Hant-C5YyFjEc.js +52 -0
- package/package.json +3 -1
- package/src/lib/engine/playback.ts +6 -5
- package/src/lib/engine/source-buffer.ts +3 -1
- package/src/lib/labels.ts +75 -0
- package/src/lib/language/index.ts +13 -1
- package/src/lib/locales/de.json +49 -0
- package/src/lib/locales/es-419.json +49 -0
- package/src/lib/locales/fr.json +49 -0
- package/src/lib/locales/it.json +49 -0
- package/src/lib/locales/ja.json +49 -0
- package/src/lib/locales/ko.json +49 -0
- package/src/lib/locales/pl.json +49 -0
- package/src/lib/locales/pt-BR.json +49 -0
- package/src/lib/locales/ru.json +49 -0
- package/src/lib/locales/tr.json +49 -0
- package/src/lib/locales/zh-Hans.json +49 -0
- package/src/lib/locales/zh-Hant.json +49 -0
- package/src/lib/react/components/burn-in-hint.tsx +4 -1
- package/src/lib/react/components/chrome.tsx +9 -0
- package/src/lib/react/components/control-bar.tsx +15 -26
- package/src/lib/react/components/errors.tsx +11 -9
- package/src/lib/react/components/overlay.tsx +18 -5
- package/src/lib/react/components/playback-slider.tsx +4 -1
- package/src/lib/react/components/settings.tsx +23 -31
- package/src/lib/react/components/skip-chapter.tsx +5 -2
- package/src/lib/react/components/sound.tsx +5 -2
- package/src/lib/react/components/subtitles.tsx +7 -4
- package/src/lib/react/components/track-menu.tsx +8 -4
- package/src/lib/react/components/volume-slider.tsx +3 -1
- package/src/lib/react/hooks/use-playback.ts +12 -2
- package/src/lib/react/labels.tsx +125 -0
- package/src/lib/react/video-player.tsx +14 -1
- package/src/lib/utils/track-label.ts +16 -11
|
@@ -4,6 +4,8 @@ import { css } from '@emotion/react'
|
|
|
4
4
|
import { ChevronLeft, ChevronRight, Settings } from 'lucide-react'
|
|
5
5
|
|
|
6
6
|
import { usePlayer } from '../player'
|
|
7
|
+
import { useLabels } from '../labels'
|
|
8
|
+
import { LABELS } from '../../labels'
|
|
7
9
|
import { TooltipDisplay } from './tooltip-display'
|
|
8
10
|
import PlaybackSlider from './playback-slider'
|
|
9
11
|
import { TrackMenu, popoverStyle, useTrackMenu } from './track-menu'
|
|
@@ -104,6 +106,8 @@ ${popoverStyle}
|
|
|
104
106
|
|
|
105
107
|
`
|
|
106
108
|
|
|
109
|
+
const SPEEDS = [0.5, 1, 1.5, 2]
|
|
110
|
+
|
|
107
111
|
enum PopoverContent {
|
|
108
112
|
Default,
|
|
109
113
|
PlaybackRate,
|
|
@@ -113,6 +117,7 @@ enum PopoverContent {
|
|
|
113
117
|
|
|
114
118
|
export const SettingsAction = () => {
|
|
115
119
|
const player = usePlayer()
|
|
120
|
+
const { format, number } = useLabels()
|
|
116
121
|
const playbackRate = usePlayer((state) => state.playbackRate)
|
|
117
122
|
const audioTracks = usePlayer((state) => state.audioTracks)
|
|
118
123
|
const selectedAudioTrack = usePlayer((state) => state.selectedAudioTrack)
|
|
@@ -159,17 +164,13 @@ export const SettingsAction = () => {
|
|
|
159
164
|
className='settings'
|
|
160
165
|
type='button'
|
|
161
166
|
onClick={toggle}
|
|
162
|
-
aria-label=
|
|
167
|
+
aria-label={format(LABELS.settings)}
|
|
163
168
|
aria-expanded={open}
|
|
164
169
|
>
|
|
165
170
|
<Settings />
|
|
166
171
|
</button>
|
|
167
172
|
}
|
|
168
|
-
toolTipText={
|
|
169
|
-
<span>
|
|
170
|
-
Settings
|
|
171
|
-
</span>
|
|
172
|
-
}
|
|
173
|
+
toolTipText={<span>{format(LABELS.settings)}</span>}
|
|
173
174
|
/>
|
|
174
175
|
{
|
|
175
176
|
open && popoverContent === PopoverContent.Default && (
|
|
@@ -183,7 +184,7 @@ export const SettingsAction = () => {
|
|
|
183
184
|
{audioTracks.length > 1
|
|
184
185
|
? (
|
|
185
186
|
<div onClick={changePopoverContent(PopoverContent.Audio)}>
|
|
186
|
-
<div>
|
|
187
|
+
<div>{format(LABELS.audio)}</div>
|
|
187
188
|
<div>
|
|
188
189
|
<ChevronRight />
|
|
189
190
|
</div>
|
|
@@ -193,7 +194,7 @@ export const SettingsAction = () => {
|
|
|
193
194
|
{qualityRows > 1
|
|
194
195
|
? (
|
|
195
196
|
<div onClick={changePopoverContent(PopoverContent.Quality)}>
|
|
196
|
-
<div>
|
|
197
|
+
<div>{format(LABELS.quality)}</div>
|
|
197
198
|
<div>
|
|
198
199
|
{qualityLabel ? <span className='secondary'>{qualityLabel}</span> : null}
|
|
199
200
|
<ChevronRight />
|
|
@@ -202,14 +203,12 @@ export const SettingsAction = () => {
|
|
|
202
203
|
)
|
|
203
204
|
: null}
|
|
204
205
|
<div onClick={changePopoverContent(PopoverContent.PlaybackRate)}>
|
|
205
|
-
<div>
|
|
206
|
+
<div>{format(LABELS.playbackSpeed)}</div>
|
|
206
207
|
<div>
|
|
207
208
|
<span className='secondary'>
|
|
208
|
-
{
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
: playbackRate.toFixed(2)
|
|
212
|
-
}
|
|
209
|
+
{playbackRate === 1
|
|
210
|
+
? format(LABELS.defaultSpeed)
|
|
211
|
+
: number(playbackRate, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
|
213
212
|
</span>
|
|
214
213
|
<ChevronRight />
|
|
215
214
|
</div>
|
|
@@ -225,11 +224,11 @@ export const SettingsAction = () => {
|
|
|
225
224
|
if (volumeNormalizer.available) volumeNormalizer.setEnabled(!volumeNormalizer.enabled)
|
|
226
225
|
}}
|
|
227
226
|
>
|
|
228
|
-
<div>
|
|
227
|
+
<div>{format(LABELS.normalizeVolume)}</div>
|
|
229
228
|
<div>
|
|
230
229
|
{volumeNormalizer.available
|
|
231
230
|
? <span className={volumeNormalizer.enabled ? 'switch on' : 'switch'} />
|
|
232
|
-
: <span className='secondary'>
|
|
231
|
+
: <span className='secondary'>{format(LABELS.unavailable)}</span>}
|
|
233
232
|
</div>
|
|
234
233
|
</div>
|
|
235
234
|
)
|
|
@@ -242,24 +241,17 @@ export const SettingsAction = () => {
|
|
|
242
241
|
<div className='popover playback-rate'>
|
|
243
242
|
<div className="back" onClick={changePopoverContent(PopoverContent.Default)}>
|
|
244
243
|
<ChevronLeft />
|
|
245
|
-
<span>
|
|
244
|
+
<span>{format(LABELS.playbackSpeed)}</span>
|
|
246
245
|
</div>
|
|
247
246
|
<div className="slider no-hover">
|
|
248
247
|
<PlaybackSlider />
|
|
249
248
|
</div>
|
|
250
249
|
<div className="options no-hover">
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
</div>
|
|
257
|
-
<div onClick={() => setPlaybackRate(1.5)}>
|
|
258
|
-
1.5x
|
|
259
|
-
</div>
|
|
260
|
-
<div onClick={() => setPlaybackRate(2)}>
|
|
261
|
-
2x
|
|
262
|
-
</div>
|
|
250
|
+
{SPEEDS.map((rate) => (
|
|
251
|
+
<div key={rate} onClick={() => setPlaybackRate(rate)}>
|
|
252
|
+
{format(LABELS.speed, { rate: number(rate) })}
|
|
253
|
+
</div>
|
|
254
|
+
))}
|
|
263
255
|
</div>
|
|
264
256
|
</div>
|
|
265
257
|
)
|
|
@@ -267,7 +259,7 @@ export const SettingsAction = () => {
|
|
|
267
259
|
{
|
|
268
260
|
open && popoverContent === PopoverContent.Audio && (
|
|
269
261
|
<TrackMenu
|
|
270
|
-
title=
|
|
262
|
+
title={format(LABELS.audio)}
|
|
271
263
|
tracks={audioTracks}
|
|
272
264
|
selected={selectedAudioTrack}
|
|
273
265
|
onSelect={chooseAudio}
|
|
@@ -280,7 +272,7 @@ export const SettingsAction = () => {
|
|
|
280
272
|
{
|
|
281
273
|
open && popoverContent === PopoverContent.Quality && quality && (
|
|
282
274
|
<TrackMenu
|
|
283
|
-
title=
|
|
275
|
+
title={format(LABELS.quality)}
|
|
284
276
|
tracks={quality.options}
|
|
285
277
|
selected={quality.selectedId ?? undefined}
|
|
286
278
|
onSelect={chooseQuality}
|
|
@@ -4,6 +4,8 @@ import { css } from '@emotion/react'
|
|
|
4
4
|
import { fonts } from '../../utils/fonts'
|
|
5
5
|
import { classifyChapters } from '../../utils/chapters'
|
|
6
6
|
import { usePlayer } from '../player'
|
|
7
|
+
import { useLabels } from '../labels'
|
|
8
|
+
import { LABELS } from '../../labels'
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* How far AHEAD of the chapter the offer appears, in seconds.
|
|
@@ -26,7 +28,7 @@ const OFFER_MS = 6_000
|
|
|
26
28
|
/** A jump back by more than this is a seek rather than playback, and re-opens the offer. */
|
|
27
29
|
const SEEK_BACK_S = 1
|
|
28
30
|
|
|
29
|
-
const
|
|
31
|
+
const OFFERS = { opening: LABELS.skipOpening, ending: LABELS.skipEnding } as const
|
|
30
32
|
|
|
31
33
|
const style = css`
|
|
32
34
|
position: absolute;
|
|
@@ -100,6 +102,7 @@ export const SkipChapter = () => {
|
|
|
100
102
|
*/
|
|
101
103
|
const seekPreview = usePlayer((state) => state.seekPreview)
|
|
102
104
|
const player = usePlayer()
|
|
105
|
+
const { format } = useLabels()
|
|
103
106
|
|
|
104
107
|
const kinds = useMemo(() => classifyChapters(chapters), [chapters])
|
|
105
108
|
|
|
@@ -163,7 +166,7 @@ export const SkipChapter = () => {
|
|
|
163
166
|
return (
|
|
164
167
|
<div css={style} className={show && !seekPreview ? 'show' : ''}>
|
|
165
168
|
<button type='button' className='skip-chapter' onClick={skip}>
|
|
166
|
-
{
|
|
169
|
+
{format(OFFERS[skippable.kind])}
|
|
167
170
|
</button>
|
|
168
171
|
</div>
|
|
169
172
|
)
|
|
@@ -6,6 +6,8 @@ import { css } from '@emotion/react'
|
|
|
6
6
|
import { Volume1, Volume2, VolumeX } from 'lucide-react'
|
|
7
7
|
|
|
8
8
|
import { usePlayer } from '../player'
|
|
9
|
+
import { useLabels } from '../labels'
|
|
10
|
+
import { LABELS } from '../../labels'
|
|
9
11
|
import { TooltipDisplay } from './tooltip-display'
|
|
10
12
|
import { fonts } from '../../utils/fonts'
|
|
11
13
|
import VolumeSlider from './volume-slider'
|
|
@@ -83,6 +85,7 @@ export type SoundProps = {
|
|
|
83
85
|
|
|
84
86
|
export const Sound = ({ ref }: SoundProps) => {
|
|
85
87
|
const player = usePlayer()
|
|
88
|
+
const { format } = useLabels()
|
|
86
89
|
const volume = usePlayer((state) => state.volume)
|
|
87
90
|
const muted = usePlayer((state) => state.muted)
|
|
88
91
|
|
|
@@ -107,7 +110,7 @@ export const Sound = ({ ref }: SoundProps) => {
|
|
|
107
110
|
ref={ref}
|
|
108
111
|
// A static name with the state on `aria-pressed`. Renaming a control when it is activated
|
|
109
112
|
// announces the state twice and breaks the expectation that a control keeps its name.
|
|
110
|
-
aria-label=
|
|
113
|
+
aria-label={format(LABELS.mute)}
|
|
111
114
|
aria-pressed={muted || volume === 0}
|
|
112
115
|
>
|
|
113
116
|
{muted || volume === 0
|
|
@@ -119,7 +122,7 @@ export const Sound = ({ ref }: SoundProps) => {
|
|
|
119
122
|
</button>
|
|
120
123
|
}
|
|
121
124
|
toolTipText={
|
|
122
|
-
<span>{muted ?
|
|
125
|
+
<span>{format(muted ? LABELS.unmuteKey : LABELS.muteKey)}</span>
|
|
123
126
|
}
|
|
124
127
|
/>
|
|
125
128
|
<div className='volume-slider-container'>
|
|
@@ -3,6 +3,8 @@ import { css } from '@emotion/react'
|
|
|
3
3
|
import { Captions, CaptionsOff } from 'lucide-react'
|
|
4
4
|
|
|
5
5
|
import { usePlayer } from '../player'
|
|
6
|
+
import { useLabels } from '../labels'
|
|
7
|
+
import { LABELS } from '../../labels'
|
|
6
8
|
import { TooltipDisplay } from './tooltip-display'
|
|
7
9
|
import { TrackMenu, popoverStyle, useTrackMenu } from './track-menu'
|
|
8
10
|
|
|
@@ -33,6 +35,7 @@ ${popoverStyle}
|
|
|
33
35
|
* on a source that owns its own player it is slow enough to be a considered act.
|
|
34
36
|
*/
|
|
35
37
|
export const SubtitlesAction = () => {
|
|
38
|
+
const { format } = useLabels()
|
|
36
39
|
const subtitleTracks = usePlayer((state) => state.subtitleTracks)
|
|
37
40
|
const selectedSubtitleTrack = usePlayer((state) => state.selectedSubtitleTrack)
|
|
38
41
|
const selectSubtitleTrack = usePlayer((state) => state.selectSubtitleTrack)
|
|
@@ -58,23 +61,23 @@ export const SubtitlesAction = () => {
|
|
|
58
61
|
className='subtitles'
|
|
59
62
|
type='button'
|
|
60
63
|
onClick={toggle}
|
|
61
|
-
aria-label=
|
|
64
|
+
aria-label={format(LABELS.subtitles)}
|
|
62
65
|
aria-expanded={open}
|
|
63
66
|
aria-pressed={on}
|
|
64
67
|
>
|
|
65
68
|
{on ? <Captions className='captions' /> : <CaptionsOff className='captions-off' />}
|
|
66
69
|
</button>
|
|
67
70
|
}
|
|
68
|
-
toolTipText={<span>
|
|
71
|
+
toolTipText={<span>{format(LABELS.subtitles)}</span>}
|
|
69
72
|
/>
|
|
70
73
|
{
|
|
71
74
|
open && (
|
|
72
75
|
<TrackMenu
|
|
73
|
-
title=
|
|
76
|
+
title={format(LABELS.subtitles)}
|
|
74
77
|
tracks={subtitleTracks}
|
|
75
78
|
selected={selectedSubtitleTrack}
|
|
76
79
|
onSelect={(id) => runSelect(id ?? null, () => selectSubtitleTrack(id))}
|
|
77
|
-
offLabel={subtitleOffLabel ??
|
|
80
|
+
offLabel={subtitleOffLabel ?? format(LABELS.subtitlesOff)}
|
|
78
81
|
pending={pending}
|
|
79
82
|
failed={failed}
|
|
80
83
|
/>
|
|
@@ -6,6 +6,8 @@ import { css } from '@emotion/react'
|
|
|
6
6
|
import { ChevronLeft } from 'lucide-react'
|
|
7
7
|
|
|
8
8
|
import { fonts } from '../../utils/fonts'
|
|
9
|
+
import { useLabels } from '../labels'
|
|
10
|
+
import { LABELS } from '../../labels'
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* The popover surface and the track rows, shared by every menu in the control bar.
|
|
@@ -36,9 +38,10 @@ export const popoverStyle = css`
|
|
|
36
38
|
once subtitles moved out to its own button, and a long track list scrolls at the cap either way. */
|
|
37
39
|
height: max-content;
|
|
38
40
|
/* The room actually above the bar. A short player had the top of the menu clipped by that same
|
|
39
|
-
\`overflow: hidden\`.
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
\`overflow: hidden\`. \`--mp-height\` is the player's own height, which the chrome writes: with no
|
|
42
|
+
size container in the ancestry \`100cqh\` is the small viewport's, and a translated menu whose rows
|
|
43
|
+
wrap reached the cap and rose 2px past a phone's 220px tall player. */
|
|
44
|
+
max-height: min(160px, calc(var(--mp-height, 100cqh) - 100% - 16px));
|
|
42
45
|
@media (min-width: 768px) {
|
|
43
46
|
right: calc(16px + env(safe-area-inset-right, 0px));
|
|
44
47
|
width: 250px;
|
|
@@ -166,6 +169,7 @@ export const TrackMenu = (
|
|
|
166
169
|
failed?: boolean
|
|
167
170
|
}
|
|
168
171
|
) => {
|
|
172
|
+
const { format } = useLabels()
|
|
169
173
|
const busy = pending !== undefined
|
|
170
174
|
const row = (id: string | number | null, label: string, className?: string) => {
|
|
171
175
|
const isPending = busy && pending === id
|
|
@@ -190,7 +194,7 @@ export const TrackMenu = (
|
|
|
190
194
|
{onBack ? <ChevronLeft /> : null}
|
|
191
195
|
<span>{title}</span>
|
|
192
196
|
</div>
|
|
193
|
-
{failed ? <div className="no-hover failed">
|
|
197
|
+
{failed ? <div className="no-hover failed">{format(LABELS.switchFailed)}</div> : null}
|
|
194
198
|
{offLabel ? row(null, offLabel) : null}
|
|
195
199
|
{tracks.map(({ id, label }) => row(id, label, 'description'))}
|
|
196
200
|
</div>
|
|
@@ -4,6 +4,7 @@ import { css } from '@emotion/react'
|
|
|
4
4
|
|
|
5
5
|
import { TooltipDisplay } from './tooltip-display'
|
|
6
6
|
import { usePlayer } from '../player'
|
|
7
|
+
import { useLabels } from '../labels'
|
|
7
8
|
import { useDragValue } from '../hooks/use-drag-value'
|
|
8
9
|
import { logToLinearVolume } from '../../utils/volume-utils'
|
|
9
10
|
|
|
@@ -58,6 +59,7 @@ type VolumeSliderType = {
|
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
export const VolumeSlider = ({ value, onChange }: VolumeSliderType) => {
|
|
62
|
+
const { number } = useLabels()
|
|
61
63
|
const volume = usePlayer((state) => state.volume)
|
|
62
64
|
const muted = usePlayer((state) => state.muted)
|
|
63
65
|
|
|
@@ -132,7 +134,7 @@ export const VolumeSlider = ({ value, onChange }: VolumeSliderType) => {
|
|
|
132
134
|
}
|
|
133
135
|
toolTipText={
|
|
134
136
|
<div className='volume-value'>
|
|
135
|
-
{displayVolumePercent}
|
|
137
|
+
{number(displayVolumePercent / 100, { style: 'percent' })}
|
|
136
138
|
</div>
|
|
137
139
|
}
|
|
138
140
|
/>
|
|
@@ -6,6 +6,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
6
6
|
|
|
7
7
|
import { isMediaElementError, startPlayback } from '../../engine'
|
|
8
8
|
import { usePlayer } from '../player'
|
|
9
|
+
import { useLabels } from '../labels'
|
|
9
10
|
import { toNamedTracks } from '../../utils/track-label'
|
|
10
11
|
import { languageOf, preferredTrack } from '../../language'
|
|
11
12
|
|
|
@@ -169,6 +170,15 @@ export const usePlayback = (
|
|
|
169
170
|
onPlaybackErrorRef.current = options?.onPlaybackError
|
|
170
171
|
const onOutputRef = useRef(onOutput)
|
|
171
172
|
onOutputRef.current = onOutput
|
|
173
|
+
const labels = useLabels()
|
|
174
|
+
const labelsRef = useRef(labels)
|
|
175
|
+
labelsRef.current = labels
|
|
176
|
+
// named again when the labels change, as they do when a catalogue arrives after the streams
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
const { audio, subtitles } = streams.current
|
|
179
|
+
if (subtitles.length) player.setSourceState({ subtitleTracks: toNamedTracks(subtitles, labels) })
|
|
180
|
+
if (audio.length) player.setSourceState({ audioTracks: toNamedTracks(audio, labels) })
|
|
181
|
+
}, [player, labels])
|
|
172
182
|
|
|
173
183
|
// The store's ids are opaque, but everything this hook writes into it came from libav, so every id
|
|
174
184
|
// that comes back is one of its own stream indices.
|
|
@@ -395,14 +405,14 @@ export const usePlayback = (
|
|
|
395
405
|
if (cancelled) return
|
|
396
406
|
streams.current.subtitles = subtitles
|
|
397
407
|
player.setSourceState({
|
|
398
|
-
subtitleTracks: toNamedTracks(subtitles),
|
|
408
|
+
subtitleTracks: toNamedTracks(subtitles, labelsRef.current),
|
|
399
409
|
...subtitleChoiceFor.current === size ? {} : { selectedSubtitleTrack: preferredSubtitle(preferredSubtitleRef.current, subtitles) },
|
|
400
410
|
})
|
|
401
411
|
},
|
|
402
412
|
onAudioStreams: (audio, selected) => {
|
|
403
413
|
if (cancelled) return
|
|
404
414
|
streams.current.audio = audio
|
|
405
|
-
player.setSourceState({ audioTracks: toNamedTracks(audio), selectedAudioTrack: selected })
|
|
415
|
+
player.setSourceState({ audioTracks: toNamedTracks(audio, labelsRef.current), selectedAudioTrack: selected })
|
|
406
416
|
},
|
|
407
417
|
})
|
|
408
418
|
if (cancelled) {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { IntlShape } from '@formatjs/intl'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import type { Label, LabelValues } from '../labels'
|
|
4
|
+
|
|
5
|
+
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
|
|
6
|
+
|
|
7
|
+
import { fill } from '../labels'
|
|
8
|
+
|
|
9
|
+
type Catalogue = Readonly<Record<string, string>>
|
|
10
|
+
|
|
11
|
+
// One chunk per language, fetched with FormatJS by the first player shown in it, so a player in English loads neither.
|
|
12
|
+
const CATALOGUES: Readonly<Record<string, () => Promise<{ default: Catalogue }>>> = {
|
|
13
|
+
'de': () => import('../locales/de.json'),
|
|
14
|
+
'es-419': () => import('../locales/es-419.json'),
|
|
15
|
+
'fr': () => import('../locales/fr.json'),
|
|
16
|
+
'it': () => import('../locales/it.json'),
|
|
17
|
+
'ja': () => import('../locales/ja.json'),
|
|
18
|
+
'ko': () => import('../locales/ko.json'),
|
|
19
|
+
'pl': () => import('../locales/pl.json'),
|
|
20
|
+
'pt-BR': () => import('../locales/pt-BR.json'),
|
|
21
|
+
'ru': () => import('../locales/ru.json'),
|
|
22
|
+
'tr': () => import('../locales/tr.json'),
|
|
23
|
+
'zh-Hans': () => import('../locales/zh-Hans.json'),
|
|
24
|
+
'zh-Hant': () => import('../locales/zh-Hant.json'),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The languages the labels are written in besides English, one catalogue each. */
|
|
28
|
+
export const LANGUAGES: readonly string[] = Object.keys(CATALOGUES)
|
|
29
|
+
|
|
30
|
+
const parts = (tag: string) => {
|
|
31
|
+
try {
|
|
32
|
+
const locale = new Intl.Locale(tag)
|
|
33
|
+
const { language, script } = locale.maximize()
|
|
34
|
+
return { tag: locale.baseName, language, script }
|
|
35
|
+
} catch {
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The catalogue serving `language`, a BCP 47 tag: its own, else the one of its language and script once maximized
|
|
42
|
+
* (`es-MX` finds `es-419`, `pt-PT` finds `pt-BR`, `zh-TW` finds `zh-Hant`), else one of its language. Undefined for
|
|
43
|
+
* English and for a language with none.
|
|
44
|
+
*/
|
|
45
|
+
export const catalogueFor = (language: string) => {
|
|
46
|
+
const wanted = parts(language)
|
|
47
|
+
if (!wanted) return undefined
|
|
48
|
+
const offered = LANGUAGES.map((tag) => ({ ...parts(tag)!, catalogue: tag }))
|
|
49
|
+
return (
|
|
50
|
+
offered.find(({ tag }) => tag === wanted.tag)
|
|
51
|
+
?? offered.find(({ language, script }) => language === wanted.language && script === wanted.script)
|
|
52
|
+
?? offered.find(({ language }) => language === wanted.language)
|
|
53
|
+
)?.catalogue
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const loaded = new Map<string, IntlShape>()
|
|
57
|
+
const loading = new Map<string, Promise<IntlShape>>()
|
|
58
|
+
|
|
59
|
+
const load = (language: string, catalogue: string) => {
|
|
60
|
+
let pending = loading.get(language)
|
|
61
|
+
if (!pending) {
|
|
62
|
+
pending = Promise.all([import('@formatjs/intl'), CATALOGUES[catalogue]!()])
|
|
63
|
+
.then(([{ createIntl }, { default: messages }]) => {
|
|
64
|
+
const intl = createIntl({ locale: language, defaultLocale: 'en', messages })
|
|
65
|
+
loaded.set(language, intl)
|
|
66
|
+
return intl
|
|
67
|
+
})
|
|
68
|
+
// the next player asks again
|
|
69
|
+
pending.catch(() => loading.delete(language))
|
|
70
|
+
loading.set(language, pending)
|
|
71
|
+
}
|
|
72
|
+
return pending
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The chrome's text in the language of the player around it. */
|
|
76
|
+
export type Labels = {
|
|
77
|
+
/** `label` in the player's language, with its values filled in. */
|
|
78
|
+
format: <L extends Label>(label: L, ...values: LabelValues<L>) => string
|
|
79
|
+
/** A number as `locale` writes it, as English does when it is undefined. */
|
|
80
|
+
number: (value: number, options?: Intl.NumberFormatOptions) => string
|
|
81
|
+
/**
|
|
82
|
+
* The tag the player was given, which its numbers and track languages are written in even when no catalogue serves
|
|
83
|
+
* it: `en` when Intl cannot read it, undefined when it was given none.
|
|
84
|
+
*/
|
|
85
|
+
locale: string | undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// a tag Intl cannot read makes every formatter given it throw
|
|
89
|
+
const readable = (tag: string) => {
|
|
90
|
+
try {
|
|
91
|
+
return Intl.getCanonicalLocales(tag)[0]
|
|
92
|
+
} catch {
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const labelsOf = (intl: IntlShape | undefined, language: string | undefined): Labels => {
|
|
98
|
+
const locale = language ? readable(language) ?? 'en' : undefined
|
|
99
|
+
return {
|
|
100
|
+
format: (label, ...[values]) => intl ? intl.formatMessage({ id: label, defaultMessage: label }, values) : fill(label, values),
|
|
101
|
+
number: (value, options) => new Intl.NumberFormat(locale ?? 'en', options).format(value),
|
|
102
|
+
locale,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const ENGLISH = labelsOf(undefined, undefined)
|
|
107
|
+
const LabelsContext = createContext(ENGLISH)
|
|
108
|
+
|
|
109
|
+
export const useLabels = () => useContext(LabelsContext)
|
|
110
|
+
|
|
111
|
+
/** The labels of a player given `language`: English at once, then its catalogue's once that has loaded. */
|
|
112
|
+
export const LabelsProvider = ({ language, children }: { language?: string, children: ReactNode }) => {
|
|
113
|
+
const catalogue = language ? catalogueFor(language) : undefined
|
|
114
|
+
// the cache is what is read, this only renders again once it holds the language
|
|
115
|
+
const [, setLoaded] = useState<IntlShape>()
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!language || !catalogue || loaded.has(language)) return
|
|
118
|
+
let dropped = false
|
|
119
|
+
load(language, catalogue).then((intl) => { if (!dropped) setLoaded(intl) }, () => {})
|
|
120
|
+
return () => { dropped = true }
|
|
121
|
+
}, [language, catalogue])
|
|
122
|
+
const intl = language && catalogue ? loaded.get(language) : undefined
|
|
123
|
+
const labels = useMemo(() => language ? labelsOf(intl, language) : ENGLISH, [intl, language])
|
|
124
|
+
return <LabelsContext.Provider value={labels}>{children}</LabelsContext.Provider>
|
|
125
|
+
}
|
|
@@ -18,6 +18,7 @@ import { usePassThroughPictureInPicture } from './hooks/use-pass-through-picture
|
|
|
18
18
|
import { useVolumeNormalizer } from './hooks/use-volume-normalizer'
|
|
19
19
|
import { createPlaybackTap } from '../audio/playback-tap'
|
|
20
20
|
import Chrome from './components/chrome'
|
|
21
|
+
import { LabelsProvider } from './labels'
|
|
21
22
|
import { languageOf } from '../language'
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -94,6 +95,16 @@ type CommonOptions = {
|
|
|
94
95
|
*/
|
|
95
96
|
controls?: boolean
|
|
96
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The language of the player's own labels (the controls, tooltips, menus, skip offers and failures), a BCP 47 tag
|
|
100
|
+
* such as the host page shows (`fr`, `es-MX`, `zh-TW`). The labels are shown in the closest language the player has,
|
|
101
|
+
* the numbers in them written as this tag writes them, and track languages named in it, even when the labels are
|
|
102
|
+
* English (`en-DE` writes `0,5x`). A language's text loads the first time a player shows it, and the labels are
|
|
103
|
+
* English until it has. Without it, or for a language the player does not have, they are English and nothing more is
|
|
104
|
+
* loaded; without it, track languages are named in the browser's.
|
|
105
|
+
*/
|
|
106
|
+
language?: string
|
|
107
|
+
|
|
97
108
|
/**
|
|
98
109
|
* Where the keyboard shortcuts listen. `'window'`, the default, takes keys from the whole page, which suits a
|
|
99
110
|
* page that is only the player. `'player'` takes a key only when the focus is inside the player (a click on the
|
|
@@ -652,7 +663,9 @@ const rootStyle = css`
|
|
|
652
663
|
export const MediaPlayer = (options: MediaPlayerOptions & { children?: ReactNode }) => (
|
|
653
664
|
<div css={rootStyle}>
|
|
654
665
|
<Player.Provider>
|
|
655
|
-
<
|
|
666
|
+
<LabelsProvider language={options.language}>
|
|
667
|
+
<PlayerRoot options={options}>{options.children}</PlayerRoot>
|
|
668
|
+
</LabelsProvider>
|
|
656
669
|
</Player.Provider>
|
|
657
670
|
</div>
|
|
658
671
|
)
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import type { Labels } from '../react/labels'
|
|
2
|
+
|
|
3
|
+
import { LABELS, fill } from '../labels'
|
|
4
|
+
|
|
1
5
|
export type LabelledTrack = {
|
|
2
6
|
streamIndex: number
|
|
3
7
|
title: string
|
|
@@ -11,13 +15,13 @@ export type NamedTrack = {
|
|
|
11
15
|
}
|
|
12
16
|
|
|
13
17
|
/**
|
|
14
|
-
* Human name for a track's language tag. `fallback: 'none'` is what makes
|
|
15
|
-
* undefined instead of echoing itself, so the caller can fall through to the title.
|
|
18
|
+
* Human name for a track's language tag, in `locale`, else in the browser's language. `fallback: 'none'` is what makes
|
|
19
|
+
* an unknown tag come back undefined instead of echoing itself, so the caller can fall through to the title.
|
|
16
20
|
*/
|
|
17
|
-
export const displayLanguage = (language: string | undefined): string | undefined => {
|
|
21
|
+
export const displayLanguage = (language: string | undefined, locale?: string): string | undefined => {
|
|
18
22
|
if (!language || language === 'und') return undefined
|
|
19
23
|
try {
|
|
20
|
-
return new Intl.DisplayNames([navigator.language || 'en'], { type: 'language', fallback: 'none' }).of(language) || undefined
|
|
24
|
+
return new Intl.DisplayNames([locale ?? (navigator.language || 'en')], { type: 'language', fallback: 'none' }).of(language) || undefined
|
|
21
25
|
} catch {
|
|
22
26
|
return undefined
|
|
23
27
|
}
|
|
@@ -26,12 +30,13 @@ export const displayLanguage = (language: string | undefined): string | undefine
|
|
|
26
30
|
const tidy = (title: string | undefined) => title?.replace(/_/g, ' ').trim() || undefined
|
|
27
31
|
|
|
28
32
|
/**
|
|
29
|
-
* Names a set of tracks for a menu, by language, falling back to the title then the stream index
|
|
30
|
-
* The title is appended only to break a tie, because on a MultiSub release every title is
|
|
31
|
-
* source tag and titling every row gives nine identical entries.
|
|
33
|
+
* Names a set of tracks for a menu, by language, falling back to the title then the stream index, in the language of
|
|
34
|
+
* `labels` (English without). The title is appended only to break a tie, because on a MultiSub release every title is
|
|
35
|
+
* the same source tag and titling every row gives nine identical entries.
|
|
32
36
|
*/
|
|
33
|
-
export const labelTracks = <T extends LabelledTrack>(tracks: T[]): { track: T, label: string }[] => {
|
|
34
|
-
const
|
|
37
|
+
export const labelTracks = <T extends LabelledTrack>(tracks: T[], labels?: Labels): { track: T, label: string }[] => {
|
|
38
|
+
const untitled = (number: number) => labels ? labels.format(LABELS.track, { number }) : fill(LABELS.track, { number })
|
|
39
|
+
const bases = tracks.map((track) => displayLanguage(track.language, labels?.locale) ?? tidy(track.title) ?? untitled(track.streamIndex))
|
|
35
40
|
const counts = bases.reduce<Record<string, number>>((acc, base) => ({ ...acc, [base]: (acc[base] ?? 0) + 1 }), {})
|
|
36
41
|
return tracks.map((track, index) => {
|
|
37
42
|
const base = bases[index]!
|
|
@@ -47,5 +52,5 @@ export const labelTracks = <T extends LabelledTrack>(tracks: T[]): { track: T, l
|
|
|
47
52
|
* The store's ids are opaque so a source that owns its own player can use its own track ids; the id
|
|
48
53
|
* here is the libav stream index, which is what this half of the code has to say.
|
|
49
54
|
*/
|
|
50
|
-
export const toNamedTracks = <T extends LabelledTrack>(tracks: T[]): NamedTrack[] =>
|
|
51
|
-
labelTracks(tracks).map(({ track, label }) => ({ id: track.streamIndex, label }))
|
|
55
|
+
export const toNamedTracks = <T extends LabelledTrack>(tracks: T[], labels?: Labels): NamedTrack[] =>
|
|
56
|
+
labelTracks(tracks, labels).map(({ track, label }) => ({ id: track.streamIndex, label }))
|