You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.ts 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import i18next from 'i18next';
  2. import { IConfig } from '../base/config/configType';
  3. import JITSI_TO_BCP47_MAP from './jitsi-bcp47-map.json';
  4. import logger from './logger';
  5. import TRANSCRIBER_LANGS from './transcriber-langs.json';
  6. const DEFAULT_TRANSCRIBER_LANG = 'en-US';
  7. /**
  8. * Determine which language to use for transcribing.
  9. *
  10. * @param {*} config - Application config.
  11. * @returns {string}
  12. */
  13. export function determineTranscriptionLanguage(config: IConfig) {
  14. const { transcription } = config;
  15. // if transcriptions are not enabled nothing to determine
  16. if (!transcription?.enabled) {
  17. return undefined;
  18. }
  19. // Depending on the config either use the language that the app automatically detected or the hardcoded
  20. // config BCP47 value.
  21. // Jitsi language detections uses custom language tags, but the transcriber expects BCP-47 compliant tags,
  22. // we use a mapping file to convert them.
  23. const bcp47Locale = transcription?.useAppLanguage ?? true
  24. ? JITSI_TO_BCP47_MAP[i18next.language as keyof typeof JITSI_TO_BCP47_MAP]
  25. : transcription?.preferredLanguage;
  26. // Check if the obtained language is supported by the transcriber
  27. let safeBCP47Locale = TRANSCRIBER_LANGS[bcp47Locale as keyof typeof TRANSCRIBER_LANGS] && bcp47Locale;
  28. if (!safeBCP47Locale) {
  29. safeBCP47Locale = DEFAULT_TRANSCRIBER_LANG;
  30. logger.warn(`Transcriber language ${bcp47Locale} is not supported, using default ${DEFAULT_TRANSCRIBER_LANG}`);
  31. }
  32. logger.info(`Transcriber language set to ${safeBCP47Locale}`);
  33. return safeBCP47Locale;
  34. }