Skip to content

Commit

Permalink
Improve usage of config and i18n #7972
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Oct 21, 2024
1 parent 29ff14c commit 4613777
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 54 deletions.
1 change: 0 additions & 1 deletion modules/app/src/main/resources/admin/tools/main/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<api>collaboration</api>
<api>content</api>
<api>export</api>
<api>i18n</api>
<api>license</api>
<api>styles</api>
<api>com.enonic.xp.app.main:launcher</api>
Expand Down
36 changes: 0 additions & 36 deletions modules/app/src/main/resources/apis/i18n/i18n.js

This file was deleted.

6 changes: 0 additions & 6 deletions modules/app/src/main/resources/apis/i18n/i18n.xml

This file was deleted.

9 changes: 5 additions & 4 deletions modules/app/src/main/resources/assets/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {ConnectionDetector} from '@enonic/lib-admin-ui/system/ConnectionDetector
import {AppHelper} from '@enonic/lib-admin-ui/util/AppHelper';
import {CONFIG} from '@enonic/lib-admin-ui/util/Config';
import {i18n} from '@enonic/lib-admin-ui/util/Messages';
import {i18nInit} from '@enonic/lib-admin-ui/util/MessagesInitializer';
import * as $ from 'jquery';
import {AppContext} from 'lib-contentstudio/app/AppContext';
import {ContentDeletePromptEvent} from 'lib-contentstudio/app/browse/ContentDeletePromptEvent';
Expand Down Expand Up @@ -66,6 +65,7 @@ import {ContentAppHelper} from 'lib-contentstudio/app/wizard/ContentAppHelper';
import {ContentWizardPanelParams} from 'lib-contentstudio/app/wizard/ContentWizardPanelParams';
import * as Q from 'q';
import {JSONObject} from '@enonic/lib-admin-ui/types';
import {Messages} from '@enonic/lib-admin-ui/util/Messages';

// Dynamically import and execute all input types, since they are used
// on-demand, when parsing XML schemas and has not real usage in app
Expand Down Expand Up @@ -643,10 +643,11 @@ function initProjectContext(application: Application): Q.Promise<void> {
if (!configScriptId) {
throw Error('Unable to fetch app config');
}
CONFIG.setConfig(JSON.parse(document.getElementById(configScriptId).innerText) as JSONObject);

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await i18nInit(CONFIG.getString('services.i18nUrl'));
CONFIG.setConfig(JSON.parse(document.getElementById(configScriptId).innerText) as JSONObject);
if (Messages.isEmpty()) {
Messages.setMessages(JSON.parse(CONFIG.getString('phrasesAsJson')) as object);
}

const body = Body.get();

Expand Down
22 changes: 19 additions & 3 deletions modules/app/src/main/resources/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
const admin = require('/lib/xp/admin');
const portal = require('/lib/xp/portal');
const contextLib = require('/lib/xp/context');
const i18n = require('/lib/xp/i18n');

function getPhrases() {
const locales = admin.getLocales();
const phrases = {};
const bundles = ['i18n/common', 'i18n/common_wcag', 'i18n/phrases', 'i18n/dialogs', 'i18n/wcag', 'i18n/page-editor'];

bundles.forEach(function (bundle) {
const bundlePhrases = i18n.getPhrases(locales, [bundle]);
for (const key in bundlePhrases) {
if (Object.prototype.hasOwnProperty.call(bundlePhrases, key)) {
phrases[key] = bundlePhrases[key];
}
}
});

return phrases;
}

function getConfig() {
const context = contextLib.get();
Expand Down Expand Up @@ -35,9 +53,6 @@ function getConfig() {
contentUrl: portal.apiUrl({
api: 'content',
}),
i18nUrl: portal.apiUrl({
api: 'i18n',
}),
licenseUrl: portal.apiUrl({
api: 'license',
}),
Expand All @@ -60,6 +75,7 @@ function getConfig() {
application: 'admin',
api: 'widget',
}),
phrasesAsJson: JSON.stringify(getPhrases(), null, 4),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {i18nInit} from '@enonic/lib-admin-ui/util/MessagesInitializer';
import {ObjectHelper} from '@enonic/lib-admin-ui/ObjectHelper';
import {Body} from '@enonic/lib-admin-ui/dom/Body';
import {PageView, PageViewBuilder} from './PageView';
Expand Down Expand Up @@ -69,6 +68,7 @@ import {ResetComponentViewEvent} from './event/incoming/manipulation/ResetCompon
import {PageStateEvent} from './event/incoming/common/PageStateEvent';
import {PageState} from '../app/wizard/page/PageState';
import {PageBuilder} from '../app/page/Page';
import {Messages} from '@enonic/lib-admin-ui/util/Messages';

export class LiveEditPage {

Expand Down Expand Up @@ -141,6 +141,10 @@ export class LiveEditPage {
}

CONFIG.setConfig(event.getConfig());
if (Messages.isEmpty()) {
Messages.setMessages(JSON.parse(CONFIG.getString('phrasesAsJson')) as object);
}

ProjectContext.get().setProject(Project.fromJson(event.getProjectJson()));
PageState.setState(event.getPageJson() ? new PageBuilder().fromJson(event.getPageJson()).build() : null);

Expand All @@ -152,9 +156,7 @@ export class LiveEditPage {
ContentContext.get().setContent(content);
});

const initPromise: Q.Promise<void> = i18nInit(CONFIG.getString('services.i18nUrl'), ['i18n/page-editor']);

Q.all([initPromise, contentPromise]).then(() => {
Q.all([contentPromise]).then(() => {
const body = Body.get().loadExistingChildren();
try {
this.pageView = new PageViewBuilder()
Expand Down

0 comments on commit 4613777

Please sign in to comment.