#760 - Return locales from settings

This commit is contained in:
Elio Struyf
2024-02-23 08:58:43 +01:00
parent a8777c4032
commit 48ada1c352
4 changed files with 44 additions and 8 deletions
+24
View File
@@ -43,6 +43,30 @@ export class i18n {
i18n.processedFiles = {};
}
/**
* Retrieves all the I18nConfig settings.
*
* @returns An array of I18nConfig settings.
*/
public static getAll() {
const i18nSettings = Settings.get<I18nConfig[]>(SETTING_CONTENT_I18N) || [];
const folders = Folders.get();
if (folders) {
for (const folder of folders) {
if (folder.locales) {
for (const locale of folder.locales) {
if (!i18nSettings.some((i18n) => i18n.locale === locale.locale)) {
i18nSettings.push(locale);
}
}
}
}
}
return i18nSettings;
}
/**
* Retrieves the I18nConfig settings from the application.
* @returns An array of I18nConfig objects if settings are found, otherwise undefined.
+3
View File
@@ -21,6 +21,9 @@ export const GeneralCommands = {
get: 'getSecret',
set: 'setSecret'
},
content: {
locales: 'getContentLocales'
},
runCommand: 'runCommand',
getLocalization: 'getLocalization',
openOnWebsite: 'openOnWebsite'
+4 -8
View File
@@ -22,7 +22,7 @@ import { DashboardMessage } from '../DashboardMessage';
import { EventData } from '@estruyf/vscode/dist/models';
import { parseWinPath } from '../../helpers/parseWinPath';
import { sortPages } from '../../utils/sortPages';
import { ExtensionState } from '../../constants';
import { ExtensionState, GeneralCommands } from '../../constants';
import { SortingOption } from '../models';
import { I18nConfig } from '../../models';
import { usePrevious } from '../../panelWebView/hooks/usePrevious';
@@ -268,14 +268,10 @@ export default function usePages(pages: Page[]) {
}
if (pages && pages.length > 0) {
// Store the locale information
const config: I18nConfig[] = [];
pages.forEach((page) => {
if (page.fmLocale && !config.some(locale => locale.locale === page.fmLocale?.locale)) {
config.push(page.fmLocale);
}
messageHandler.request<I18nConfig[]>(GeneralCommands.toVSCode.content.locales).then((config) => {
console.log('config', config);
setLocales(config || []);
});
setLocales(config);
}
}, [settings?.draftField, pages, sorting, search, tag, category, locale, filters, folder]);
@@ -2,6 +2,7 @@ import { GeneralCommands } from '../../constants';
import { PostMessageData } from '../../models';
import { BaseListener } from './BaseListener';
import { getLocalizationFile } from '../../utils/getLocalizationFile';
import { i18n } from '../../commands/i18n';
export class LocalizationListener extends BaseListener {
/**
@@ -13,6 +14,9 @@ export class LocalizationListener extends BaseListener {
case GeneralCommands.toVSCode.getLocalization:
this.getLocalization();
break;
case GeneralCommands.toVSCode.content.locales:
this.getContentLocales(msg.command, msg.requestId);
break;
}
}
@@ -21,4 +25,13 @@ export class LocalizationListener extends BaseListener {
this.sendMsg(GeneralCommands.toWebview.setLocalization as any, fileContents);
}
private static async getContentLocales(command: string, requestId?: string) {
if (!command || !requestId) {
return;
}
const config = i18n.getAll();
this.sendRequest(command as any, requestId, config);
}
}