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
26 changes: 22 additions & 4 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ModuleInstance } from './main.js'
import { InstanceStatus } from '@companion-module/base'
// const constants = require('./constants')
import { SIMPLE_SIGNALS, LANGUAGES } from './constants.js'
import type { ModuleInstance } from './main.js'
import { SIMPLE_SIGNALS } from './constants.js'
import { LanguageType, getLanguagesFromAPI } from './languages.js'

export function UpdateActions(self: ModuleInstance): void {
self.setActionDefinitions({
Expand Down Expand Up @@ -39,7 +39,7 @@ export function UpdateActions(self: ModuleInstance): void {
id: 'language',
type: 'dropdown',
label: 'Language',
choices: LANGUAGES,
choices: self.inputLanguages,
default: 'en',
},
],
Expand All @@ -61,5 +61,23 @@ export function UpdateActions(self: ModuleInstance): void {
}
},
},

reloadLanguages: {
name: 'Reload languages',
options: [],
callback: async () => {
try {
const languages = await getLanguagesFromAPI(self, LanguageType.INPUT)
self.inputLanguages = languages
self.updateActions() // export actions
self.updatePresetDefintions() // export presets
self.updateStatus(InstanceStatus.Ok)
self.log('debug', 'Successfully reloaded languages from API')
} catch (error) {
self.log('error', `Failed to fetch languages from API: ${error}`)
self.updateStatus(InstanceStatus.ConnectionFailure)
}
},
},
})
}
43 changes: 0 additions & 43 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,3 @@ export const SIMPLE_SIGNALS: DropdownChoiceWithButtonText[] = [
{ id: 'captions:visibility:hide', label: 'Temporarily hide captions', buttonText: 'Hide captions' },
{ id: 'captions:visibility:show', label: 'Show captions', buttonText: 'Show captions' },
] as const

// TODO: It would be lovely if this list could be obtained from CaptionKit rather than hard-coded in the module
export const LANGUAGES: DropdownChoice[] = [
{ id: 'en', label: 'English' },
{ id: 'en-US', label: 'English (US)' },
{ id: 'en-AU', label: 'English (AU)' },
{ id: 'en-GB', label: 'English (GB)' },
{ id: 'en-NZ', label: 'English (NZ)' },
{ id: 'bg', label: 'Bulgarian' },
{ id: 'cs', label: 'Czech' },
{ id: 'da', label: 'Danish' },
{ id: 'nl', label: 'Dutch' },
{ id: 'fr', label: 'French' },
{ id: 'fr-CA', label: 'French (Canada)' },
{ id: 'de', label: 'German' },
{ id: 'de-CH', label: 'German (Switzerland)' },
{ id: 'el', label: 'Greek' },
{ id: 'hi', label: 'Hindi' },
{ id: 'hu', label: 'Hungarian' },
{ id: 'id', label: 'Indonesian' },
{ id: 'it', label: 'Italian' },
{ id: 'ko', label: 'Korean' },
{ id: 'ja', label: 'Japanese' },
{ id: 'lt', label: 'Lithuanian' },
{ id: 'ms', label: 'Malay' },
{ id: 'no', label: 'Norwegian' },
{ id: 'pl', label: 'Polish' },
{ id: 'pt', label: 'Portuguese' },
{ id: 'pt-BR', label: 'Portuguese (Brazil)' },
{ id: 'ro', label: 'Romanian' },
{ id: 'ru', label: 'Russian' },
{ id: 'es', label: 'Spanish' },
{ id: 'es-419', label: 'Spanish (Latin America)' },
{ id: 'sv', label: 'Swedish' },
{ id: 'th', label: 'Thai' },
{ id: 'tr', label: 'Turkish' },
{ id: 'uk', label: 'Ukrainian' },
{ id: 'vi', label: 'Vietnamese' },
{ id: 'zh', label: 'Chinese (Mandarin, Simplified)' },
{ id: 'zh-TW', label: 'Chinese (Mandarin, Traditional)' },
{ id: 'zh-HK', label: 'Chinese (Cantonese, Traditional)' },
{ id: 'multi', label: 'English & Spanish (Bilingual)' },
] as const
21 changes: 21 additions & 0 deletions src/languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { InstanceStatus, type DropdownChoice } from '@companion-module/base'
import type { ModuleInstance } from './main.js'

export enum LanguageType {
INPUT = 'input',
OUTPUT = 'output',
}

export async function getLanguagesFromAPI(self: ModuleInstance, type: LanguageType): Promise<DropdownChoice[]> {
const json = await (await fetch(`https://api.captionkit.io/v2/languages/${type}`)).json()
try {
const languages = Object.entries(json as Record<string, string>).map(([id, label]) => ({ id, label }))
self.log('debug', `Fetched ${type} languages: ${JSON.stringify(languages)}`)

return languages
} catch (error) {
self.log('error', `Failed to fetch ${type} languages from API: ${error}`)
self.updateStatus(InstanceStatus.ConnectionFailure)
return []
}
}
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { InstanceBase, runEntrypoint, InstanceStatus, SomeCompanionConfigField } from '@companion-module/base'
import {
InstanceBase,
runEntrypoint,
InstanceStatus,
type SomeCompanionConfigField,
type DropdownChoice,
} from '@companion-module/base'
import { GetConfigFields, type ModuleConfig } from './config.js'
import { UpdateVariableDefinitions } from './variables.js'
import { UpgradeScripts } from './upgrades.js'
import { UpdateActions } from './actions.js'
import { UpdateFeedbacks } from './feedbacks.js'
import { UpdatePresets } from './presets.js'
import { getLanguagesFromAPI, LanguageType } from './languages.js'

export class ModuleInstance extends InstanceBase<ModuleConfig> {
config!: ModuleConfig // Setup in init()
inputLanguages: DropdownChoice[] = [] // Cached list of languages fetched from API, used for populating dropdowns in actions and presets

constructor(internal: unknown) {
super(internal)
Expand All @@ -22,7 +30,12 @@ export class ModuleInstance extends InstanceBase<ModuleConfig> {
this.updateFeedbacks() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
this.updatePresetDefintions() // export presets

this.inputLanguages = await getLanguagesFromAPI(this, LanguageType.INPUT)
this.updateActions() // export actions
this.updatePresetDefintions() // export presets
}

// When module gets deleted
async destroy(): Promise<void> {
this.log('debug', 'destroy')
Expand Down
34 changes: 29 additions & 5 deletions src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { type CompanionPresetDefinition, type CompanionPresetDefinitions, combineRgb } from '@companion-module/base'
import type { ModuleInstance } from './main.js'
import type { CompanionPresetDefinition, CompanionPresetDefinitions } from '@companion-module/base'
import { combineRgb } from '@companion-module/base'
import { SIMPLE_SIGNALS, LANGUAGES } from './constants.js'
import { SIMPLE_SIGNALS } from './constants.js'

export function UpdatePresets(self: ModuleInstance): void {
const presets: CompanionPresetDefinitions = {}
const presets: CompanionPresetDefinitions = {
reloadLanguages: {
type: 'button',
category: 'Settings',
name: 'Reload languages',
style: {
size: 14,
bgcolor: combineRgb(0, 0, 0),
color: combineRgb(255, 255, 255),
text: 'Reload languages',
},
steps: [
{
down: [
{
actionId: 'reloadLanguages',
options: {},
},
],
up: [],
},
],
feedbacks: [],
},
}

for (const signal of SIMPLE_SIGNALS) {
const preset: CompanionPresetDefinition = {
Expand Down Expand Up @@ -35,7 +58,8 @@ export function UpdatePresets(self: ModuleInstance): void {
presets[signal.id] = preset
}

for (const language of LANGUAGES) {
const languages = self.inputLanguages
for (const language of languages) {
const preset: CompanionPresetDefinition = {
type: 'button',
category: 'Languages',
Expand Down
Loading