Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed dynamic imports of locale - removed unused properties #959

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export default class Extension extends BaseExtension<Config>
settingsDialogue: SettingsDialogue;
shareDialogue: ShareDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export default class Extension extends BaseExtension<Config>
settingsDialogue: SettingsDialogue;
shareDialogue: ShareDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};
lastAvCanvasIndex?: number;

create(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export default class Extension extends BaseExtension<Config>
rightPanel: MoreInfoRightPanel;
settingsDialogue: SettingsDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ export default class Extension extends BaseExtension<Config>
shareDialogue: ShareDialogue;
cfiFragement: string;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ export default class Extension extends BaseExtension<Config>
rightPanel: MoreInfoRightPanel;
settingsDialogue: SettingsDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ export default class ModelViewerExtension extends BaseExtension<Config> {
settingsDialogue: SettingsDialogue;
shareDialogue: ShareDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ export default class OpenSeadragonExtension extends BaseExtension<Config> {
settingsDialogue: SettingsDialogue;
shareDialogue: ShareDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export default class Extension extends BaseExtension<Config>
rightPanel: MoreInfoRightPanel;
settingsDialogue: SettingsDialogue;
defaultConfig: Config = defaultConfig;
locales = {
"en-GB": defaultConfig,
};

create(): void {
super.create();
Expand Down
34 changes: 12 additions & 22 deletions src/content-handlers/iiif/modules/uv-shared-module/BaseExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ export class BaseExtension<T extends BaseConfig> implements IExtension {
tabbing: boolean = false;
browserDetect: BrowserDetect;
locales = {};
defaultConfig: T;
defaultConfig: T = {} as any;
localeLoaders: Record<string, () => Promise<any>> = {
"en-GB": () => import("../../../../locales/en-GB.json"),
"cy-GB": () => import("../../../../locales/cy-GB.json"),
"fr-FR": () => import("../../../../locales/fr-FR.json"),
"pl-PL": () => import("../../../../locales/pl-PL.json"),
"sv-SE": () => import("../../../../locales/sv-SE.json"),
};


public create(): void {
const that = this;
Expand Down Expand Up @@ -469,33 +477,15 @@ export class BaseExtension<T extends BaseConfig> implements IExtension {
}

public async loadConfig(locale: string, extension: string): Promise<any> {
let uv_locale = locale;
if (extension) {
uv_locale = "_";
this.locales["_"] = () =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you intentionally dropping the extension config loading? If so, the extension parameter on this method no longer serves a purpose. If not, we need to figure out how to approach it. I'm guessing it's probably still needed, but wondering if perhaps Ed's recent refactoring changed something.

Copy link
Contributor Author

@stephenwf stephenwf Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any extension that didn't have a single configuration file. If an extension requires localisation in the future, it can figure that out itself!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the way this worked was that it loaded the extension-specific config file, and then replaced the localization placeholders in that file with strings retrieved from the locale file. If we remove the loading of the extension-specific config file, I don't see how we have the framework on which to inject all the translations... but maybe I'm missing some initialization of this.defaultConfig somewhere...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so as I understand it, each extension has the import for the config.

// extensions/[name]/Extension.ts

import defaultConfig from './config/config.json';

class XyzExtension {
  defaultConfig = defaultConfig;
  // ...
}

The injections happen on line 480 using this.defaultConfig which is cloned and patched from line 489 (I didn't change that part)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see, excellent. No objections from me, then... It's a little confusing that this method now takes an argument that it doesn't use, but maybe it's worth keeping that as a hook for the future. Beyond that, if this solves the problem at hand, I don't object!

(And sorry for my uninformed questions -- the perils of trying to figure out code by looking at GitHub alone is that you don't always get the best insights... :-) ).

import(`../../extensions/${extension}/config/config.json`);
}

let config = this.locales[uv_locale];

if (!config) {
throw new Error("Unable to load config");
}
if (typeof config === "object") {
config = JSON.parse(JSON.stringify(config));
} else if (typeof config === "function") {
config = await config();
config = JSON.parse(JSON.stringify(config));
}

return this.translateLocale(config, locale);
return this.translateLocale(this.defaultConfig, locale);
}

private async translateLocale(
config: Object,
locale: String
): Promise<Object> {
let localeStrings = await import(`../../../../locales/${locale}.json`);
let loader = this.localeLoaders[locale as any] || this.localeLoaders["en-GB"];
let localeStrings = (await loader()) || {};
let conf = JSON.stringify(config);

for (let str in localeStrings) {
Expand Down
Loading