From 7ff2be1680e411594f18f6ffdcf787cd3980b825 Mon Sep 17 00:00:00 2001 From: Arjun G <91885483+Arjun-Go@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:26:00 +0530 Subject: [PATCH] A-1208586834272224 | Intl support for micro-frontends (#250) --- src/constants.js | 2 ++ src/features/i18n/utils.js | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/constants.js b/src/constants.js index a0a9104b..8bc7258c 100644 --- a/src/constants.js +++ b/src/constants.js @@ -17,6 +17,8 @@ export const ADDRESS_HEIRARCHY = hostUrl + "/openmrs/module/addresshierarchy/ajax/getOrderedAddressHierarchyLevels.form"; export const homePageUrl = "/bahmni/home/#/dashboard"; +export const IPD_CONFIG_URL = + "/bahmni_config/openmrs/i18n/micro-frontends-dist/ipd"; export const MEDICATIONS_BASE_URL = RESTWS_V1 + "/ipd/schedule/type/medication"; export const EDIT_MEDICATIONS_BASE_URL = diff --git a/src/features/i18n/utils.js b/src/features/i18n/utils.js index ec560353..60fca310 100644 --- a/src/features/i18n/utils.js +++ b/src/features/i18n/utils.js @@ -1,4 +1,4 @@ -import { LS_LANG_KEY, BASE_URL } from "../../constants"; +import { LS_LANG_KEY, IPD_CONFIG_URL, BASE_URL } from "../../constants"; const translationsBaseUrl = "i18n"; @@ -8,11 +8,24 @@ export function getLocale() { export const getTranslations = async (locale) => { const fileName = `locale_${locale}.json`; - return fetchTranslations(fileName); + try { + return await fetchTranslations(`${IPD_CONFIG_URL}/${fileName}`); + } catch (error) { + console.warn( + `Primary translation file not found, falling back to secondary: ${error.message}` + ); + return await fetchTranslations( + `${BASE_URL}${translationsBaseUrl}/${fileName}` + ); + } }; -async function fetchTranslations(fileName) { - const url = `${BASE_URL}${translationsBaseUrl}/${fileName}`; +async function fetchTranslations(url) { const response = await fetch(url); + if (!response.ok) { + throw new Error( + `Failed to fetch translations from ${url}: ${response.statusText}` + ); + } return response.json(); }