Skip to content

Add more commands to translatte #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/translatte/commands/exportMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function exportMigration(
});

const fileName = isNotDefined(exportFileName)
? `go-strings-${yyyy}-${mm}-${dd}`
? `go-migration-strings-${yyyy}-${mm}-${dd}`
: exportFileName;

await workbook.xlsx.writeFile(`${fileName}.xlsx`);
Expand Down
71 changes: 71 additions & 0 deletions app/scripts/translatte/commands/exportServerStringsToExcel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import xlsx from 'exceljs';

import { fetchServerState } from "../utils";
import { isFalsyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';

async function exportServerStringsToExcel(
apiUrl: string,
authToken?: string,
exportFileName?: string,
) {
const serverStrings = await fetchServerState(apiUrl, authToken);

const workbook = new xlsx.Workbook();
const now = new Date();
workbook.created = now;

const yyyy = now.getFullYear();
const mm = (now.getMonth() + 1).toString().padStart(2, '0');
const dd = now.getDate().toString().padStart(2, '0');
const HH = now.getHours().toString().padStart(2, '0');
const MM = now.getMinutes().toString().padStart(2, '0');

const worksheet = workbook.addWorksheet(
`${yyyy}-${mm}-${dd} ${HH}-${MM}`
);

worksheet.columns = [
{ header: 'Namespace', key: 'namespace' },
{ header: 'Key', key: 'key' },
{ header: 'EN', key: 'en' },
{ header: 'FR', key: 'fr' },
{ header: 'ES', key: 'es' },
{ header: 'AR', key: 'ar' },
]

const keyGroupedStrings = mapToList(
listToGroupList(
serverStrings,
({ page_name, key }) => `${page_name}:${key}`,
),
(list) => {
const value = listToMap(
list,
({ language }) => language,
({ value }) => value
);
const { key, page_name } = list[0];

return {
namespace: page_name,
key: key,
en: value.en,
fr: value.fr,
es: value.es,
ar: value.ar,
};
},
);

Object.values(keyGroupedStrings).forEach((keyGroupedString) => {
worksheet.addRow(keyGroupedString);
});

const fileName = isFalsyString(exportFileName)
? `go-server-strings-${yyyy}-${mm}-${dd}`
: exportFileName;

await workbook.xlsx.writeFile(`${fileName}.xlsx`);
}

export default exportServerStringsToExcel;
208 changes: 208 additions & 0 deletions app/scripts/translatte/commands/pushMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
import { Language, MigrationActionItem } from "../types";
import { fetchServerState, getCombinedKey, languages, postLanguageStrings, readMigrations } from "../utils";
import { Md5 } from "ts-md5";

async function pushMigration(migrationFilePath: string, apiUrl: string, authToken: string) {
const serverStrings = await fetchServerState(apiUrl, authToken);

const serverStringMapByCombinedKey = mapToMap(
listToGroupList(
serverStrings,
({ key, page_name }) => getCombinedKey(key, page_name),
),
(key) => key,
(list) => listToMap(
list,
({ language }) => language,
)
);

const migrations = await readMigrations(
[migrationFilePath]
);

const actions = migrations[0].content.actions;


function getItemsForNamespaceUpdate(actionItem: MigrationActionItem, language: Language) {
if (actionItem.action !== 'update') {
return undefined;
}

if (isNotDefined(actionItem.newNamespace)) {
return undefined;
}

const oldCombinedKey = getCombinedKey(
actionItem.key,
actionItem.namespace,
);

const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language];

if (isNotDefined(oldStringItem)) {
return undefined;
}

return [
{
action: 'delete' as const,
key: actionItem.key,
page_name: actionItem.namespace,
},
{
action: 'set' as const,
key: actionItem.key,
page_name: actionItem.newNamespace,
value: oldStringItem.value,
hash: oldStringItem.hash,
},
];
}

function getItemsForKeyUpdate(actionItem: MigrationActionItem, language: Language) {
if (actionItem.action !== 'update') {
return undefined;
}

if (isNotDefined(actionItem.newKey)) {
return undefined;
}

const oldCombinedKey = getCombinedKey(
actionItem.key,
actionItem.namespace,
);

const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language];

if (isNotDefined(oldStringItem)) {
return undefined;
}

return [
{
action: 'delete' as const,
key: actionItem.key,
page_name: actionItem.namespace,
},
{
action: 'set' as const,
key: actionItem.newKey,
page_name: actionItem.namespace,
value: oldStringItem.value,
hash: oldStringItem.hash,
},
];
}

const serverActions = listToMap(
languages.map((language) => {
const serverActionsForCurrentLanguage = actions.flatMap((actionItem) => {
if (language === 'en') {
if (actionItem.action === 'add') {
return {
action: 'set' as const,
key: actionItem.key,
page_name: actionItem.namespace,
value: actionItem.value,
hash: Md5.hashStr(actionItem.value),
}
}

if (actionItem.action === 'remove') {
return {
action: 'delete' as const,
key: actionItem.key,
page_name: actionItem.namespace,
}
}

if (isDefined(actionItem.newNamespace)) {
return getItemsForNamespaceUpdate(actionItem, language);
}

if (isDefined(actionItem.newKey)) {
return getItemsForKeyUpdate(actionItem, language);
}

if (isDefined(actionItem.newValue)) {
return {
action: 'set' as const,
key: actionItem.key,
page_name: actionItem.namespace,
value: actionItem.newValue,
hash: Md5.hashStr(actionItem.newValue),
}
}
} else {
if (actionItem.action === 'remove') {
return {
action: 'delete' as const,
key: actionItem.key,
page_name: actionItem.namespace,
}
}

if (actionItem.action === 'update') {
if (isDefined(actionItem.newNamespace)) {
return getItemsForNamespaceUpdate(actionItem, language);
}

if (isDefined(actionItem.newKey)) {
return getItemsForKeyUpdate(actionItem, language);
}
}
}

return undefined;
}).filter(isDefined);

return {
language,
actions: serverActionsForCurrentLanguage,
}
}),
({ language }) => language,
);

console.log('Pusing actions for en...')
const enResponse = await postLanguageStrings(
serverActions.en.language,
serverActions.en.actions,
apiUrl,
authToken,
);
console.log(await enResponse.json());


console.log('Pusing actions for fr...')
const frResponse = await postLanguageStrings(
serverActions.fr.language,
serverActions.fr.actions,
apiUrl,
authToken,
);
console.log(await frResponse.json());

console.log('Pusing actions for es...')
const esResponse = await postLanguageStrings(
serverActions.es.language,
serverActions.es.actions,
apiUrl,
authToken,
);
console.log(await esResponse.json());

console.log('Pusing actions for ar...')
const arResponse = await postLanguageStrings(
serverActions.ar.language,
serverActions.ar.actions,
apiUrl,
authToken,
);
console.log(await arResponse.json());
}

export default pushMigration;
Loading
Loading