From 4b16237fd7e8df2104ddf129815958b0b46bcaa4 Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:23:51 -0400 Subject: [PATCH 1/9] Update consent template name --- .../hbs/{consent-document.hbs => consent-template-5.hbs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/templates/hbs/{consent-document.hbs => consent-template-5.hbs} (100%) diff --git a/packages/templates/hbs/consent-document.hbs b/packages/templates/hbs/consent-template-5.hbs similarity index 100% rename from packages/templates/hbs/consent-document.hbs rename to packages/templates/hbs/consent-template-5.hbs From 993a51cb04e6c272f814f447486dd7713b339b33 Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:26:18 -0400 Subject: [PATCH 2/9] Add support dotenv --- package-lock.json | 3 +++ packages/templates/package.json | 1 + packages/templates/rollup.config.mjs | 4 +++- packages/templates/src/environment.d.ts | 9 +++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 packages/templates/src/environment.d.ts diff --git a/package-lock.json b/package-lock.json index 90f45c15..1aaba5a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17217,6 +17217,8 @@ }, "node_modules/rollup-plugin-dotenv": { "version": "0.5.1", + "resolved": "https://registry.npmjs.org/rollup-plugin-dotenv/-/rollup-plugin-dotenv-0.5.1.tgz", + "integrity": "sha512-ARUPDmeKAw3niZ2Ajv0qKNRryIWFMW796oJSS1hNdop3HF63Vljio/QRmG6ob0aQzzVUrFq6vW1p4jOE6xDQrQ==", "dev": true, "license": "MIT", "dependencies": { @@ -20331,6 +20333,7 @@ "devDependencies": { "@jspsych/config": "^2.0.0", "handlebars": "^4.7.8", + "rollup-plugin-dotenv": "^0.5.1", "rollup-plugin-polyfill-node": "^0.13.0" }, "peerDependencies": { diff --git a/packages/templates/package.json b/packages/templates/package.json index 7cd013a8..eb3c558d 100644 --- a/packages/templates/package.json +++ b/packages/templates/package.json @@ -27,6 +27,7 @@ "devDependencies": { "@jspsych/config": "^2.0.0", "handlebars": "^4.7.8", + "rollup-plugin-dotenv": "^0.5.1", "rollup-plugin-polyfill-node": "^0.13.0" }, "peerDependencies": { diff --git a/packages/templates/rollup.config.mjs b/packages/templates/rollup.config.mjs index b0ac4b28..63119b11 100644 --- a/packages/templates/rollup.config.mjs +++ b/packages/templates/rollup.config.mjs @@ -1,3 +1,4 @@ +import dotenv from "rollup-plugin-dotenv"; import nodePolyfills from "rollup-plugin-polyfill-node"; import { importAsString } from "rollup-plugin-string-import"; import { makeRollupConfig } from "../../rollup.mjs"; @@ -7,7 +8,8 @@ export default makeRollupConfig("chsTemplates").map((config) => { ...config, plugins: [ ...config.plugins, - + // Add support for .env files + dotenv(), // Add support to import yaml and handlebars files as strings importAsString({ include: ["**/*.yaml", "**/*.hbs"], diff --git a/packages/templates/src/environment.d.ts b/packages/templates/src/environment.d.ts new file mode 100644 index 00000000..013eed40 --- /dev/null +++ b/packages/templates/src/environment.d.ts @@ -0,0 +1,9 @@ +declare global { + namespace NodeJS { + interface ProcessEnv { + DEBUG?: string; + } + } +} + +export {}; From 02a59ab6ca0e83f301edd313a3793305b054c1ce Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:26:39 -0400 Subject: [PATCH 3/9] Update default consent template name --- packages/record/src/consentVideo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/record/src/consentVideo.ts b/packages/record/src/consentVideo.ts index eeda729e..de1c40c2 100644 --- a/packages/record/src/consentVideo.ts +++ b/packages/record/src/consentVideo.ts @@ -16,7 +16,7 @@ const info = { name: "consent-video", version, parameters: { - template: { type: ParameterType.STRING, default: "consent_005" }, + template: { type: ParameterType.STRING, default: "consent-template-5" }, locale: { type: ParameterType.STRING, default: "en-us" }, additional_video_privacy_statement: { type: ParameterType.STRING, From 654700adddd0efe40b74e12efa9f43b29dfc65cd Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:27:30 -0400 Subject: [PATCH 4/9] Rename consent video template file --- ...onsentVideo.ts => consentVideoTemplate.ts} | 23 ++++++++++++++++--- packages/templates/src/index.ts | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) rename packages/templates/src/{consentVideo.ts => consentVideoTemplate.ts} (59%) diff --git a/packages/templates/src/consentVideo.ts b/packages/templates/src/consentVideoTemplate.ts similarity index 59% rename from packages/templates/src/consentVideo.ts rename to packages/templates/src/consentVideoTemplate.ts index 13d71412..ff28ed23 100644 --- a/packages/templates/src/consentVideo.ts +++ b/packages/templates/src/consentVideoTemplate.ts @@ -1,9 +1,10 @@ import { LookitWindow } from "@lookit/data/dist/types"; import Handlebars from "handlebars"; import { PluginInfo, TrialType } from "jspsych"; -import consentDocumentTemplate from "../hbs/consent-document.hbs"; +import consent_template_5 from "../hbs/consent-template-5.hbs"; import consentVideoTrialTemplate from "../hbs/consent-video-trial.hbs"; -import { initI18nAndTemplates } from "./utils"; +import { ConsentTemplateNotFound } from "./errors"; +import { initI18nAndHelpers } from "./utils"; declare const window: LookitWindow; @@ -19,7 +20,8 @@ export const consentVideo = (trial: TrialType) => { const experiment = window.chs.study.attributes; const { PIName, PIContact } = trial; - initI18nAndTemplates(trial); + initI18nAndHelpers(trial); + const consentDocumentTemplate = consentDocument(trial); const consent = Handlebars.compile(consentDocumentTemplate)({ ...trial, @@ -34,3 +36,18 @@ export const consentVideo = (trial: TrialType) => { video_container_id, }); }; + +/** + * Get consent template by name. + * + * @param trial - Trial data including user supplied parameters. + * @returns Consent template + */ +const consentDocument = (trial: TrialType) => { + switch (trial.template) { + case "consent-template-5": + return consent_template_5; + default: + throw new ConsentTemplateNotFound(trial.template); + } +}; diff --git a/packages/templates/src/index.ts b/packages/templates/src/index.ts index d3e03529..c0531b8c 100644 --- a/packages/templates/src/index.ts +++ b/packages/templates/src/index.ts @@ -1,3 +1,3 @@ -import { consentVideo } from "./consentVideo"; +import { consentVideo } from "./consentVideoTemplate"; export default { consentVideo }; From b46c68602e5c9fe121964cd18ceb5e8865e074ee Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:29:08 -0400 Subject: [PATCH 5/9] Move expFormat helper to global context --- packages/templates/src/utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/templates/src/utils.ts b/packages/templates/src/utils.ts index db894ddf..caa1263d 100644 --- a/packages/templates/src/utils.ts +++ b/packages/templates/src/utils.ts @@ -14,6 +14,9 @@ import pt_br from "../i18n/pt-br.yaml"; import pt from "../i18n/pt.yaml"; import { TranslationNotFoundError } from "./errors"; +// TODO: Can this be in the global context. +Handlebars.registerHelper("exp-format", (context) => expFormat(context)); + /** * Pulled from EFP. Function to convert researcher's text to HTML. * @@ -83,6 +86,7 @@ export const getTranslation = (lcl: Intl.Locale) => { */ const initI18next = (trial: TrialType) => { const debug = process.env.DEBUG === "true"; + // TODO: Catch Locale error when bad locale const lcl = new Intl.Locale(trial.locale); const translation = getTranslation(lcl); @@ -105,7 +109,6 @@ const initHandlebars = () => { Handlebars.registerHelper("t", (context, { hash }) => i18next.t(context, hash), ); - Handlebars.registerHelper("exp-format", (context) => expFormat(context)); }; /** @@ -113,7 +116,7 @@ const initHandlebars = () => { * * @param trial - Yup */ -export const initI18nAndTemplates = (trial: TrialType) => { +export const initI18nAndHelpers = (trial: TrialType) => { initI18next(trial); initHandlebars(); }; From 34a84c6107603e5b182d9530897ebf400fee3461 Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:29:33 -0400 Subject: [PATCH 6/9] Consent template not found error --- packages/templates/src/errors.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/templates/src/errors.ts b/packages/templates/src/errors.ts index c748365f..274354a0 100644 --- a/packages/templates/src/errors.ts +++ b/packages/templates/src/errors.ts @@ -9,3 +9,14 @@ export class TranslationNotFoundError extends Error { super(`"${baseName}" translation not found.`); } } +/** Error thrown when researcher selects template that isn't available. */ +export class ConsentTemplateNotFound extends Error { + /** + * This will let the researcher know that their template isn't found. + * + * @param template - Supplied name of consent template. + */ + public constructor(template: string) { + super(`Consent template "${template}" not found.`); + } +} From eccaca5019a15e092c005c97dc3d3c6f0616e988 Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:31:18 -0400 Subject: [PATCH 7/9] Changeset --- .changeset/large-rabbits-matter.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/large-rabbits-matter.md diff --git a/.changeset/large-rabbits-matter.md b/.changeset/large-rabbits-matter.md new file mode 100644 index 00000000..804b881e --- /dev/null +++ b/.changeset/large-rabbits-matter.md @@ -0,0 +1,6 @@ +--- +"@lookit/templates": patch +"@lookit/record": patch +--- + +Add ability to select specific video consent template. From a29f33fecaf8d0acc562ec043228ed494966df1b Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:24:29 -0400 Subject: [PATCH 8/9] Moved i18next's init to global to fix failing tests. --- packages/record/src/consentVideo.spec.ts | 35 ++++-- .../templates/src/consentVideoTemplate.ts | 5 +- packages/templates/src/errors.ts | 6 +- packages/templates/src/index.spec.ts | 21 +++- packages/templates/src/utils.spec.ts | 37 +----- packages/templates/src/utils.ts | 116 +++++++----------- 6 files changed, 98 insertions(+), 122 deletions(-) diff --git a/packages/record/src/consentVideo.spec.ts b/packages/record/src/consentVideo.spec.ts index 1ad76c7e..fa048dd2 100644 --- a/packages/record/src/consentVideo.spec.ts +++ b/packages/record/src/consentVideo.spec.ts @@ -42,7 +42,10 @@ test("Trial", () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; plugin["recordFeed"] = jest.fn(); plugin["recordButton"] = jest.fn(); @@ -116,7 +119,10 @@ test("onEnded", () => { const play = document.createElement("button"); const next = document.createElement("button"); const record = document.createElement("button"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; display.innerHTML = chsTemplates.consentVideo(trial); plugin["recordFeed"] = jest.fn(); @@ -152,7 +158,10 @@ test("getButton", () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; display.innerHTML = chsTemplates.consentVideo(trial); @@ -182,7 +191,10 @@ test("recordButton", async () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; display.innerHTML = chsTemplates.consentVideo(trial); @@ -228,7 +240,10 @@ test("playButton", () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; plugin["playbackFeed"] = jest.fn(); @@ -249,7 +264,10 @@ test("stopButton", async () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; display.innerHTML = chsTemplates.consentVideo(trial) + Handlebars.compile(recordFeed)({}); @@ -274,7 +292,10 @@ test("nextButton", () => { const jsPsych = initJsPsych(); const plugin = new VideoConsentPlugin(jsPsych); const display = document.createElement("div"); - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; display.innerHTML = chsTemplates.consentVideo(trial); plugin["endTrial"] = jest.fn(); diff --git a/packages/templates/src/consentVideoTemplate.ts b/packages/templates/src/consentVideoTemplate.ts index ff28ed23..63870f04 100644 --- a/packages/templates/src/consentVideoTemplate.ts +++ b/packages/templates/src/consentVideoTemplate.ts @@ -4,7 +4,7 @@ import { PluginInfo, TrialType } from "jspsych"; import consent_template_5 from "../hbs/consent-template-5.hbs"; import consentVideoTrialTemplate from "../hbs/consent-video-trial.hbs"; import { ConsentTemplateNotFound } from "./errors"; -import { initI18nAndHelpers } from "./utils"; +import { setLocale } from "./utils"; declare const window: LookitWindow; @@ -20,7 +20,8 @@ export const consentVideo = (trial: TrialType) => { const experiment = window.chs.study.attributes; const { PIName, PIContact } = trial; - initI18nAndHelpers(trial); + setLocale(trial); + const consentDocumentTemplate = consentDocument(trial); const consent = Handlebars.compile(consentDocumentTemplate)({ diff --git a/packages/templates/src/errors.ts b/packages/templates/src/errors.ts index 274354a0..07ddaa58 100644 --- a/packages/templates/src/errors.ts +++ b/packages/templates/src/errors.ts @@ -1,12 +1,12 @@ -/** Error throw what specified language isn't found */ -export class TranslationNotFoundError extends Error { +/** Error thrown when specified language isn't found */ +export class LocaleNotFoundError extends Error { /** * This will be thrown when attempting to init i18n * * @param baseName - Language a2code with region */ public constructor(baseName: string) { - super(`"${baseName}" translation not found.`); + super(`"${baseName}" locale not found.`); } } /** Error thrown when researcher selects template that isn't available. */ diff --git a/packages/templates/src/index.spec.ts b/packages/templates/src/index.spec.ts index 807d9db8..1aaa6459 100644 --- a/packages/templates/src/index.spec.ts +++ b/packages/templates/src/index.spec.ts @@ -1,11 +1,15 @@ import { LookitWindow } from "@lookit/data/dist/types"; import { PluginInfo, TrialType } from "jspsych"; +import { ConsentTemplateNotFound } from "./errors"; import chsTemplate from "./index"; declare const window: LookitWindow; test("consent video", () => { - const trial = { locale: "en-us" } as unknown as TrialType; + const trial = { + locale: "en-us", + template: "consent-template-5", + } as unknown as TrialType; const name = "some name"; window.chs = { study: { @@ -25,7 +29,10 @@ test("consent video", () => { }); test("consent video in French", () => { - const trial = { locale: "fr" } as unknown as TrialType; + const trial = { + locale: "fr", + template: "consent-template-5", + } as unknown as TrialType; const name = "some name"; window.chs = { study: { @@ -43,3 +50,13 @@ test("consent video in French", () => { `Consentement à participer à la recherche:\n ${name}`, ); }); + +test("consent video with unknown template", () => { + const trial = { + locale: "en-us", + template: "not a real template name", + } as unknown as TrialType; + expect(() => chsTemplate.consentVideo(trial)).toThrow( + ConsentTemplateNotFound, + ); +}); diff --git a/packages/templates/src/utils.spec.ts b/packages/templates/src/utils.spec.ts index 6bd4e4ab..922a399a 100644 --- a/packages/templates/src/utils.spec.ts +++ b/packages/templates/src/utils.spec.ts @@ -1,15 +1,4 @@ -import Yaml from "js-yaml"; -import en_us from "../i18n/en-us.yaml"; -import eu from "../i18n/eu.yaml"; -import fr from "../i18n/fr.yaml"; -import hu from "../i18n/hu.yaml"; -import it from "../i18n/it.yaml"; -import ja from "../i18n/ja.yaml"; -import nl from "../i18n/nl.yaml"; -import pt_br from "../i18n/pt-br.yaml"; -import pt from "../i18n/pt.yaml"; -import { TranslationNotFoundError } from "./errors"; -import { expFormat, getTranslation } from "./utils"; +import { expFormat } from "./utils"; test("expFormat convert written text to format well in HTML", () => { expect(expFormat("abcdefg")).toStrictEqual("abcdefg"); @@ -29,27 +18,3 @@ test("expFormat convert written text to format well in HTML", () => { "    Tabbed text", ); }); - -test("Get translation file for specified locale", () => { - const translations = { - ja, - pt, - eu, - fr, - hu, - it, - nl, - "en-us": en_us, - "pt-br": pt_br, - }; - - for (const [k, v] of Object.entries(translations)) { - expect(getTranslation(new Intl.Locale(k))).toStrictEqual(Yaml.load(v)); - } - - expect(pt_br).not.toStrictEqual(pt); - - expect(() => getTranslation(new Intl.Locale("not-a2code"))).toThrow( - TranslationNotFoundError, - ); -}); diff --git a/packages/templates/src/utils.ts b/packages/templates/src/utils.ts index caa1263d..1626e10b 100644 --- a/packages/templates/src/utils.ts +++ b/packages/templates/src/utils.ts @@ -12,10 +12,7 @@ import ja from "../i18n/ja.yaml"; import nl from "../i18n/nl.yaml"; import pt_br from "../i18n/pt-br.yaml"; import pt from "../i18n/pt.yaml"; -import { TranslationNotFoundError } from "./errors"; - -// TODO: Can this be in the global context. -Handlebars.registerHelper("exp-format", (context) => expFormat(context)); +import { LocaleNotFoundError } from "./errors"; /** * Pulled from EFP. Function to convert researcher's text to HTML. @@ -38,45 +35,32 @@ export const expFormat = (text?: string | string[]) => { }; /** - * Get a translation file based on selected language. + * Get a translation resources from yaml files. * - * @param lcl - Locale object with locale - * @returns Translations from i18next + * @returns Resources for i18next */ -export const getTranslation = (lcl: Intl.Locale) => { - /** - * Switch case to find language from a string. Will throw error is language - * not found. - * - * @param baseName - Base name from locale (en-us) - * @returns Language yaml file - */ - const getYaml = (baseName: string) => { - switch (baseName) { - case "en-US": - return en_us; - case "eu": - return eu; - case "fr": - return fr; - case "hu": - return hu; - case "it": - return it; - case "ja": - return ja; - case "nl": - return nl; - case "pt-BR": - return pt_br; - case "pt": - return pt; - default: - throw new TranslationNotFoundError(baseName); - } +const resources = () => { + const translations = { + "en-us": en_us, + eu, + fr, + hu, + it, + ja, + nl, + "pt-br": pt_br, + pt, }; - return Yaml.load(getYaml(lcl.baseName)) as Record; + return Object.entries(translations).reduce((prev, [locale, translation]) => { + const lcl = new Intl.Locale(locale); + return { + ...prev, + [lcl.baseName]: { + translation: Yaml.load(translation) as Record, + }, + }; + }, {}); }; /** @@ -84,39 +68,27 @@ export const getTranslation = (lcl: Intl.Locale) => { * * @param trial - Trial data including user supplied parameters. */ -const initI18next = (trial: TrialType) => { - const debug = process.env.DEBUG === "true"; - // TODO: Catch Locale error when bad locale - const lcl = new Intl.Locale(trial.locale); - const translation = getTranslation(lcl); - - i18next.use(ICU).init({ - lng: lcl.baseName, - debug, - resources: { - [lcl.language]: { - translation, - }, - }, - }); +export const setLocale = (trial: TrialType) => { + try { + const lcl = new Intl.Locale(trial.locale); + if (i18next.language !== lcl.baseName) { + i18next.changeLanguage(lcl.baseName); + } + } catch (error) { + if (error instanceof RangeError) { + throw new LocaleNotFoundError(trial.locale); + } else { + throw error; + } + } }; -/** - * Initialize handlebars helpers. This could be done globally, but it does go - * hand in hand with initializing i18n. - */ -const initHandlebars = () => { - Handlebars.registerHelper("t", (context, { hash }) => - i18next.t(context, hash), - ); -}; +// Initialize translations +i18next.use(ICU).init({ + debug: process.env.DEBUG === "true", + resources: resources(), +}); -/** - * Initialize both i18next and Handlebars. - * - * @param trial - Yup - */ -export const initI18nAndHelpers = (trial: TrialType) => { - initI18next(trial); - initHandlebars(); -}; +// Setup Handlebars' helpers +Handlebars.registerHelper("exp-format", (context) => expFormat(context)); +Handlebars.registerHelper("t", (context, { hash }) => i18next.t(context, hash)); From ded01b76fc60f582e2f543afe5075f64a9bef05e Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:36:58 -0400 Subject: [PATCH 9/9] Update tests --- packages/templates/src/utils.spec.ts | 9 ++++++++- packages/templates/src/utils.ts | 19 ++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/templates/src/utils.spec.ts b/packages/templates/src/utils.spec.ts index 922a399a..9747b3f0 100644 --- a/packages/templates/src/utils.spec.ts +++ b/packages/templates/src/utils.spec.ts @@ -1,4 +1,6 @@ -import { expFormat } from "./utils"; +import { PluginInfo, TrialType } from "jspsych"; +import { LocaleNotFoundError } from "./errors"; +import { expFormat, setLocale } from "./utils"; test("expFormat convert written text to format well in HTML", () => { expect(expFormat("abcdefg")).toStrictEqual("abcdefg"); @@ -18,3 +20,8 @@ test("expFormat convert written text to format well in HTML", () => { "    Tabbed text", ); }); + +test("setLocale throw error with non-existing locale", () => { + const trial = { locale: "non-existing" } as unknown as TrialType; + expect(() => setLocale(trial)).toThrow(LocaleNotFoundError); +}); diff --git a/packages/templates/src/utils.ts b/packages/templates/src/utils.ts index 1626e10b..fb4a6913 100644 --- a/packages/templates/src/utils.ts +++ b/packages/templates/src/utils.ts @@ -69,17 +69,14 @@ const resources = () => { * @param trial - Trial data including user supplied parameters. */ export const setLocale = (trial: TrialType) => { - try { - const lcl = new Intl.Locale(trial.locale); - if (i18next.language !== lcl.baseName) { - i18next.changeLanguage(lcl.baseName); - } - } catch (error) { - if (error instanceof RangeError) { - throw new LocaleNotFoundError(trial.locale); - } else { - throw error; - } + const lcl = new Intl.Locale(trial.locale); + + if (!i18next.hasResourceBundle(lcl.baseName, "translation")) { + throw new LocaleNotFoundError(trial.locale); + } + + if (i18next.language !== lcl.baseName) { + i18next.changeLanguage(lcl.baseName); } };