DM-558: Localazy integration for DM

This commit is contained in:
Adina Țeudan 2023-11-23 20:23:47 +02:00
parent 67823cfc1d
commit 095096e325
5 changed files with 181 additions and 1308 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,2 +1,15 @@
export const TOKEN: string = process.env.LOCALAZY_TOKEN || '';
export const Apps = {
RedactManager: 'redact',
DocuMine: 'scm',
} as const;
export const AppsList = [...Object.values(Apps)] as const;
export type Apps = (typeof Apps)[keyof typeof Apps];
export const Tokens: Record<Apps, string> = {
redact: process.env.LOCALAZY_RM_TOKEN || '',
scm: process.env.LOCALAZY_DM_TOKEN || '',
} as const;
export const FILENAME = 'i18n.json';

View File

@ -1,68 +0,0 @@
import { FILENAME, TOKEN } from './constants';
import LocalazyApi from '@localazy/ts-api';
import { Key } from '@localazy/ts-api/dist/models/responses/keys-in-file';
import { get, set } from './utils';
import * as fs from 'fs';
const api = LocalazyApi({
projectToken: TOKEN,
baseUrl: 'https://api.localazy.com',
});
export async function getProjectId(): Promise<string> {
const projects = await api.listProjects();
return projects[0].id;
}
export async function getFileId(projectId: string): Promise<string> {
const files = await api.listFiles({ projectId });
return files.find(file => file.name === FILENAME)!.id;
}
export async function uploadLocal(projectId: string, lang: 'en' | 'de'): Promise<void> {
const langFile = (await import(`./../../../apps/red-ui/src/assets/i18n/redact/${lang}.json`)).default;
await api.import({
projectId,
files: [
{
name: FILENAME,
content: { type: 'json', features: ['plural_icu', 'filter_untranslated'], [lang]: langFile },
},
],
deprecate: 'project',
});
}
export async function downloadLanguage(projectId: string, fileId: string, lang: 'en' | 'de'): Promise<any> {
const response = await api.getFileContents({ projectId, fileId, lang });
return JSON.parse(await response.text());
}
export async function listKeys(projectId: string, fileId: string, lang: 'en' | 'de'): Promise<Key[]> {
let keys: Key[] = [];
let next: string | undefined = undefined;
do {
const response = await api.listKeysInFileForLanguage({ projectId, fileId, lang, next });
next = response.next;
keys = [...keys, ...response.keys];
} while (next);
return keys;
}
export async function updateLocalEn(keys: Key[]): Promise<void> {
const enFile = (await import('./../../../apps/red-ui/src/assets/i18n/redact/en.json')).default;
keys.forEach(({ key, value }) => {
if (get(enFile, key) !== undefined) {
set(enFile, key, value);
}
});
await fs.promises.writeFile('./../../apps/red-ui/src/assets/i18n/redact/en.json', JSON.stringify(enFile, null, 2));
}
export async function updateLocalDe(projectId: string, fileId: string): Promise<void> {
const deFile = await downloadLanguage(projectId, fileId, 'de');
await fs.promises.writeFile('./../../apps/red-ui/src/assets/i18n/redact/de.json', JSON.stringify(deFile, null, 2));
}

View File

@ -1,33 +1,12 @@
import { downloadLanguage, getFileId, getProjectId, listKeys, updateLocalDe, updateLocalEn, uploadLocal } from './functions';
import { AppsList } from './constants';
import { LocalazyRunner } from './localazy-runner';
async function execute() {
const projectId = await getProjectId();
const fileId = await getFileId(projectId);
/** Update local en (source) file with potential remotely updated translations. */
const remoteKeys = await listKeys(projectId, fileId, 'en');
await updateLocalEn(remoteKeys);
/** Upload local en (source) file in order to add new keys and deprecate unused keys. */
await uploadLocal(projectId, 'en');
/** Download updated de file. */
await updateLocalDe(projectId, fileId);
async function execute(): Promise<void> {
for (const app of AppsList) {
const runner = new LocalazyRunner(app);
await runner.initialize();
await runner.synchronize();
}
async function uploadLocals() {
const projectId = await getProjectId();
await uploadLocal(projectId, 'de');
}
async function downloadDe() {
const projectId = await getProjectId();
const fileId = await getFileId(projectId);
console.log({ projectId, fileId });
console.log(await downloadLanguage(projectId, fileId, 'de'));
}
execute().then();
// uploadLocals().then();
// downloadDe().then();

View File

@ -0,0 +1,103 @@
import { Apps, FILENAME, Tokens } from './constants';
import LocalazyApi from '@localazy/ts-api';
import { Key } from '@localazy/ts-api/dist/models/responses/keys-in-file';
import { get, set } from './utils';
import * as fs from 'fs';
export class LocalazyRunner {
private readonly _api;
private _projectId = '';
private _fileId = '';
constructor(readonly app: Apps) {
this._api = LocalazyApi({
projectToken: Tokens[app],
baseUrl: 'https://api.localazy.com',
});
}
async initialize(): Promise<void> {
this._projectId = await this._getProjectId();
this._fileId = await this._getFileId();
}
async synchronize(): Promise<void> {
/** Update local en (source) file with potential remotely updated translations. */
const remoteKeys = await this._listKeys('en');
await this._updateLocalEn(remoteKeys);
/** Upload local en (source) file in order to add new keys and deprecate unused keys. */
await this._uploadLocal('en');
/** Download updated de file. */
await this._updateLocalDe();
}
/** For debug purposes only. */
async uploadLocalDe() {
await this._uploadLocal('de');
}
/** For debug purposes only. */
async downloadDe() {
console.log(await this._downloadLanguage('de'));
}
private async _uploadLocal(lang: 'en' | 'de'): Promise<void> {
const langFile = (await import(`./../../../apps/red-ui/src/assets/i18n/${this.app}/${lang}.json`)).default;
await this._api.import({
projectId: this._projectId,
files: [
{
name: FILENAME,
content: { type: 'json', features: ['plural_icu', 'filter_untranslated'], [lang]: langFile },
},
],
deprecate: 'project',
});
}
private async _listKeys(lang: 'en' | 'de'): Promise<Key[]> {
let keys: Key[] = [];
let next: string | undefined = undefined;
do {
const response = await this._api.listKeysInFileForLanguage({ projectId: this._projectId, fileId: this._fileId, lang, next });
next = response.next;
keys = [...keys, ...response.keys];
} while (next);
return keys;
}
private async _updateLocalEn(keys: Key[]): Promise<void> {
const enFile = (await import(`./../../../apps/red-ui/src/assets/i18n/${this.app}/en.json`)).default;
keys.forEach(({ key, value }) => {
if (get(enFile, key) !== undefined) {
set(enFile, key, value);
}
});
await fs.promises.writeFile(`./../../apps/red-ui/src/assets/i18n/${this.app}/en.json`, JSON.stringify(enFile, null, 2));
}
private async _updateLocalDe(): Promise<void> {
const deFile = await this._downloadLanguage('de');
await fs.promises.writeFile(`./../../apps/red-ui/src/assets/i18n/${this.app}/de.json`, JSON.stringify(deFile, null, 2));
}
private async _downloadLanguage(lang: 'en' | 'de'): Promise<any> {
const response = await this._api.getFileContents({ projectId: this._projectId, fileId: this._fileId, lang });
return JSON.parse(await response.text());
}
private async _getProjectId(): Promise<string> {
const projects = await this._api.listProjects();
return projects[0].id;
}
private async _getFileId(): Promise<string> {
const files = await this._api.listFiles({ projectId: this._projectId });
return files.find(file => file.name === FILENAME)!.id;
}
}