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

update init i18n params #154

Merged
merged 4 commits into from
Feb 25, 2025
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
533 changes: 271 additions & 262 deletions examples/standalone/main.ts

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions examples/standalone/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions packages/core/src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Lang } from '../types'
import { mergeDeep } from '../utils'
import CN from './zh-CN.json'
import FA from "./fa.json"
import PA from "./pa.json"
import FA from './fa.json'
import PA from './pa.json'
export default class I18n {
public lang: Lang

Expand All @@ -14,11 +14,16 @@ export default class I18n {
en: Object.keys(CN).reduce<Record<string, string>>(
(previous, current) => ((previous[current] = current), previous),
{}
),
)
}

constructor(defaultLang: Lang) {
constructor(defaultLang: Lang, userLanguages?: Partial<Record<Lang, any>>) {
this.lang = defaultLang === 'auto' ? (navigator.language as Lang) : defaultLang

if (userLanguages) {
mergeDeep(this.languages, userLanguages)
}

if (!this.languages[this.lang]) {
navigator.languages.some((lang) => {
if (this.languages[lang as Lang]) {
Expand All @@ -37,6 +42,7 @@ export default class I18n {
return false
})
}

if (!this.languages[this.lang]) this.lang = 'en'
}

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
PlayerListener,
PlayerOptions,
PlayerPlugin,
RequiredPartial,
Source
} from './types'

Expand All @@ -28,13 +29,13 @@ const defaultOptions = {
isLive: false,
autopause: true,
isNativeUI: () => isQQBrowser
}
} as const

export class Player<Context extends Record<string, any> = Record<string, any>> {
static players: Player[] = []

container: HTMLElement
options: Required<PlayerOptions>
options: RequiredPartial<PlayerOptions, 'languages'>

locales: I18n
eventEmitter: EventEmitter
Expand Down Expand Up @@ -62,7 +63,7 @@ export class Player<Context extends Record<string, any> = Record<string, any>> {
typeof options === 'string' ? { source: { src: options } } : options
)

this.locales = new I18n(this.options.lang)
this.locales = new I18n(this.options.lang, this.options.languages)
this.eventEmitter = new EventEmitter()
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface PlayerOptions {
playsinline?: boolean
preload?: 'auto' | 'metadata' | 'none'
lang?: Lang
languages?: Partial<Record<Lang, any>>
isLive?: boolean
videoAttr?: Record<string, boolean | string>
isNativeUI?: () => boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/danmaku/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oplayer/danmaku",
"version": "1.2.25",
"version": "1.2.26-beta.0",
"description": "Danmaku plugin for oplayer",
"type": "module",
"main": "./dist/index.es.js",
Expand Down