Merge branch 'RED-7497' into 'master'

RED-7497: Localazy integration

See merge request redactmanager/red-ui!52
This commit is contained in:
Timo Bejan 2023-09-13 08:03:08 +02:00
commit 1e24db86ee
13 changed files with 828 additions and 676 deletions

View File

@ -11,4 +11,30 @@ workflow:
include:
- project: 'gitlab/gitlab'
ref: 'main'
file: 'ci-templates/docker_build_nexus.yml'
file: 'ci-templates/docker_build_nexus_v2.yml'
localazy update:
image: node:20.5
cache:
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
script:
- git config user.email "${CI_EMAIL}"
- git config user.name "${CI_USERNAME}"
- git remote add gitlab_origin https://${CI_USERNAME}:${CI_ACCESS_TOKEN}@gitlab.knecon.com/redactmanager/red-ui.git
- cd tools/localazy
- yarn install --cache-folder .yarn-cache
- yarn start
- cd ../..
- git add .
- git commit -m "push back localazy update"
- git push gitlab_origin HEAD:${CI_COMMIT_REF_NAME} -o ci.skip
rules:
- if: $LOCALAZY_RUN
- changes:
- tools/localazy/**/*
- red-ui/src/assets/i18n/**/*

File diff suppressed because it is too large Load Diff

View File

@ -2533,4 +2533,4 @@
}
},
"yesterday": "Yesterday"
}
}

@ -1 +1 @@
Subproject commit d1df30b56ea5abd03b0c8623f68fc96db9fef271
Subproject commit 60ace0115100fe43591cabe973b5cb0067def173

View File

@ -11,6 +11,7 @@
"nx": "nx",
"start": "nx serve",
"update": "nx migrate latest",
"localazy": "ts-node tools/localazy/src/index.ts",
"migrate": "nx migrate --run-migrations",
"workspace-generator": "nx workspace-generator",
"analyze": "nx build --stats-json && webpack-bundle-analyzer dist/apps/red-ui/stats.json",
@ -75,6 +76,7 @@
"@angular/compiler-cli": "16.2.2",
"@angular/language-service": "16.2.2",
"@bartholomej/ngx-translate-extract": "^8.0.2",
"@localazy/ts-api": "^1.0.0",
"@nx/eslint-plugin": "16.7.4",
"@nx/jest": "16.7.4",
"@nx/linter": "16.7.4",

2
tools/localazy/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
yarn.lock
node_modules/

View File

@ -0,0 +1,15 @@
{
"name": "localazy",
"version": "1.0.0",
"dependencies": {
"@types/node": "^20.5.7"
},
"scripts": {
"start": "ts-node src/index.ts"
},
"devDependencies": {
"@localazy/ts-api": "^1.0.0",
"ts-node": "10.9.1",
"typescript": "5.1.3"
}
}

View File

@ -0,0 +1,2 @@
export const TOKEN: string = process.env.LOCALAZY_TOKEN || '';
export const FILENAME = 'i18n.json';

View File

@ -0,0 +1,63 @@
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[]> {
const response = await api.listKeysInFileForLanguage({ projectId, fileId, lang });
console.log(`Downloaded ${response.keys.length} keys for ${lang} language`);
return response.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

@ -0,0 +1,33 @@
import { downloadLanguage, getFileId, getProjectId, listKeys, updateLocalDe, updateLocalEn, uploadLocal } from './functions';
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 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,16 @@
export function get(object: any, path: string[]): any {
return path.reduce((o, k) => (o || {})[k], object);
}
export function set(object: any, path: string[], value: any): void {
path.reduce((o, k, i) => {
if (i === path.length - 1) {
o[k] = value;
} else {
if (o[k] === undefined) {
o[k] = {};
}
}
return o[k];
}, object);
}

View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6", "DOM"],
"outDir": "build",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}

View File

@ -2494,6 +2494,11 @@
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b"
integrity sha512-Hcv+nVC0kZnQ3tD9GVu5xSMR4VVYOteQIr/hwFPVEvPdlXqgGEuRjiheChHgdM+JyqdgNcmzZOX/tnl0JOiI7A==
"@localazy/ts-api@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@localazy/ts-api/-/ts-api-1.1.0.tgz#e36edbea775fa950a6dcd54693686ed63fa67218"
integrity sha512-0iLFWRxmKPkuruASye4A6CQoYMDGLqQmPLS76zKhPywwNTc89oIlaWrkl63c2EP70NzHxKu6BnpmJAjqFSeXrg==
"@materia-ui/ngx-monaco-editor@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@materia-ui/ngx-monaco-editor/-/ngx-monaco-editor-6.0.0.tgz#9ae93666019e9a6d4f787370b4373cbb63a04a38"