Skip to content

Commit

Permalink
A-1208586834272224 | Intl support for micro-frontends (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjun-Go authored Oct 24, 2024
1 parent 2e4441e commit 7ff2be1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
21 changes: 17 additions & 4 deletions src/features/i18n/utils.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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();
}

0 comments on commit 7ff2be1

Please sign in to comment.