Skip to content

Commit

Permalink
Support localized data dump
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Nov 20, 2024
1 parent e2ec16e commit c882e47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions web/src/api/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const http = axios.create({
responseType: "json",
});

// FIXME: share with web/src/context/installerL10n.tsx
function agamaLanguage(): string | undefined {
// language from cookie, empty string if not set (regexp taken from Cockpit)
// https://github.com/cockpit-project/cockpit/blob/98a2e093c42ea8cd2431cf15c7ca0e44bb4ce3f1/pkg/shell/shell-modals.jsx#L91
return decodeURIComponent(
document.cookie.replace(/(?:(?:^|.*;\s*)agamaLang\s*=\s*([^;]*).*$)|^.*$/, "$1"),
);
}

function mock_response(method: string, url: string) {
console.info("Demo mode, ignoring request", method, url);

Expand All @@ -53,10 +62,17 @@ function mock_response(method: string, url: string) {
*/
const get = (url: string) => {
if (process.env.AGAMA_DEMO) {
if (!(url in demo_data)) {
console.error("Missing demo data for REST API path", url);
const lang = agamaLanguage() || "en-US";

// try translated demo data first
if (demo_data[lang] && url in demo_data[lang]) {
return Promise.resolve(demo_data[lang][url]);
} else if (url in demo_data) {
return Promise.resolve(demo_data[url]);
} else {
console.error(`Missing demo data for REST API path ${url} (lang: ${lang})`);
return {};
}
return Promise.resolve(demo_data[url]);
} else {
return http.get(url).then(({ data }) => data);
}
Expand Down
1 change: 1 addition & 0 deletions web/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const copy_files = [
"./src/index.html",
// TODO: consider using something more complete like https://github.com/jantimon/favicons-webpack-plugin
"./src/assets/favicon.svg",
"./src/languages.json",
{ from: "./src/assets/products/*.svg", to: "assets/logos/[name][ext]" },
];

Expand Down

0 comments on commit c882e47

Please sign in to comment.