mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor: Move node i18n logic to NodeTypes
(no-changelog) (#7035)
This commit is contained in:
parent
b716241b42
commit
53361d1d62
|
@ -10,6 +10,9 @@ import { NodeHelpers } from 'n8n-workflow';
|
||||||
import { Service } from 'typedi';
|
import { Service } from 'typedi';
|
||||||
import { RESPONSE_ERROR_MESSAGES } from './constants';
|
import { RESPONSE_ERROR_MESSAGES } from './constants';
|
||||||
import { LoadNodesAndCredentials } from './LoadNodesAndCredentials';
|
import { LoadNodesAndCredentials } from './LoadNodesAndCredentials';
|
||||||
|
import { join, dirname } from 'path';
|
||||||
|
import { readdir } from 'fs/promises';
|
||||||
|
import type { Dirent } from 'fs';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class NodeTypes implements INodeTypes {
|
export class NodeTypes implements INodeTypes {
|
||||||
|
@ -78,4 +81,46 @@ export class NodeTypes implements INodeTypes {
|
||||||
private get knownNodes() {
|
private get knownNodes() {
|
||||||
return this.nodesAndCredentials.known.nodes;
|
return this.nodesAndCredentials.known.nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getNodeTranslationPath({
|
||||||
|
nodeSourcePath,
|
||||||
|
longNodeType,
|
||||||
|
locale,
|
||||||
|
}: {
|
||||||
|
nodeSourcePath: string;
|
||||||
|
longNodeType: string;
|
||||||
|
locale: string;
|
||||||
|
}) {
|
||||||
|
const nodeDir = dirname(nodeSourcePath);
|
||||||
|
const maxVersion = await this.getMaxVersion(nodeDir);
|
||||||
|
const nodeType = longNodeType.replace('n8n-nodes-base.', '');
|
||||||
|
|
||||||
|
return maxVersion
|
||||||
|
? join(nodeDir, `v${maxVersion}`, 'translations', locale, `${nodeType}.json`)
|
||||||
|
: join(nodeDir, 'translations', locale, `${nodeType}.json`);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getMaxVersion(dir: string) {
|
||||||
|
const entries = await readdir(dir, { withFileTypes: true });
|
||||||
|
|
||||||
|
const dirnames = entries.reduce<string[]>((acc, cur) => {
|
||||||
|
if (this.isVersionedDirname(cur)) acc.push(cur.name);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!dirnames.length) return null;
|
||||||
|
|
||||||
|
return Math.max(...dirnames.map((d) => parseInt(d.charAt(1), 10)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private isVersionedDirname(dirent: Dirent) {
|
||||||
|
if (!dirent.isDirectory()) return false;
|
||||||
|
|
||||||
|
const ALLOWED_VERSIONED_DIRNAME_LENGTH = [2, 3]; // e.g. v1, v10
|
||||||
|
|
||||||
|
return (
|
||||||
|
ALLOWED_VERSIONED_DIRNAME_LENGTH.includes(dirent.name.length) &&
|
||||||
|
dirent.name.toLowerCase().startsWith('v')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
import { join, dirname } from 'path';
|
|
||||||
import { readdir } from 'fs/promises';
|
|
||||||
import type { Dirent } from 'fs';
|
|
||||||
|
|
||||||
const ALLOWED_VERSIONED_DIRNAME_LENGTH = [2, 3]; // e.g. v1, v10
|
|
||||||
|
|
||||||
function isVersionedDirname(dirent: Dirent) {
|
|
||||||
if (!dirent.isDirectory()) return false;
|
|
||||||
|
|
||||||
return (
|
|
||||||
ALLOWED_VERSIONED_DIRNAME_LENGTH.includes(dirent.name.length) &&
|
|
||||||
dirent.name.toLowerCase().startsWith('v')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getMaxVersion(from: string) {
|
|
||||||
const entries = await readdir(from, { withFileTypes: true });
|
|
||||||
|
|
||||||
const dirnames = entries.reduce<string[]>((acc, cur) => {
|
|
||||||
if (isVersionedDirname(cur)) acc.push(cur.name);
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!dirnames.length) return null;
|
|
||||||
|
|
||||||
return Math.max(...dirnames.map((d) => parseInt(d.charAt(1), 10)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the full path to a node translation file in `/dist`.
|
|
||||||
*/
|
|
||||||
export async function getNodeTranslationPath({
|
|
||||||
nodeSourcePath,
|
|
||||||
longNodeType,
|
|
||||||
locale,
|
|
||||||
}: {
|
|
||||||
nodeSourcePath: string;
|
|
||||||
longNodeType: string;
|
|
||||||
locale: string;
|
|
||||||
}): Promise<string> {
|
|
||||||
const nodeDir = dirname(nodeSourcePath);
|
|
||||||
const maxVersion = await getMaxVersion(nodeDir);
|
|
||||||
const nodeType = longNodeType.replace('n8n-nodes-base.', '');
|
|
||||||
|
|
||||||
return maxVersion
|
|
||||||
? join(nodeDir, `v${maxVersion}`, 'translations', locale, `${nodeType}.json`)
|
|
||||||
: join(nodeDir, 'translations', locale, `${nodeType}.json`);
|
|
||||||
}
|
|
|
@ -3,7 +3,6 @@ import get from 'lodash/get';
|
||||||
import { Request } from 'express';
|
import { Request } from 'express';
|
||||||
import type { INodeTypeDescription, INodeTypeNameVersion } from 'n8n-workflow';
|
import type { INodeTypeDescription, INodeTypeNameVersion } from 'n8n-workflow';
|
||||||
import { Authorized, Post, RestController } from '@/decorators';
|
import { Authorized, Post, RestController } from '@/decorators';
|
||||||
import { getNodeTranslationPath } from '@/TranslationHelpers';
|
|
||||||
import { Config } from '@/config';
|
import { Config } from '@/config';
|
||||||
import { NodeTypes } from '@/NodeTypes';
|
import { NodeTypes } from '@/NodeTypes';
|
||||||
|
|
||||||
|
@ -35,7 +34,7 @@ export class NodeTypesController {
|
||||||
nodeTypes: INodeTypeDescription[],
|
nodeTypes: INodeTypeDescription[],
|
||||||
) => {
|
) => {
|
||||||
const { description, sourcePath } = this.nodeTypes.getWithSourcePath(name, version);
|
const { description, sourcePath } = this.nodeTypes.getWithSourcePath(name, version);
|
||||||
const translationPath = await getNodeTranslationPath({
|
const translationPath = await this.nodeTypes.getNodeTranslationPath({
|
||||||
nodeSourcePath: sourcePath,
|
nodeSourcePath: sourcePath,
|
||||||
longNodeType: description.name,
|
longNodeType: description.name,
|
||||||
locale: defaultLocale,
|
locale: defaultLocale,
|
||||||
|
|
Loading…
Reference in a new issue