Skip to content
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
12 changes: 6 additions & 6 deletions app/scripts/translatte/commands/clearServerStrings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { listToGroupList } from "@togglecorp/fujs";
import { isTruthyString, listToGroupList } from "@togglecorp/fujs";
import { fetchServerState, postLanguageStrings, writeFilePromisify } from "../utils";

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

const bulkActions = listToGroupList(
serverStrings,
serverStrings.filter(({ page_name }) => isTruthyString(page_name)),
({ language }) => language,
({ key, page_name }) => ({
action: "delete" as const,
Expand All @@ -19,7 +19,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
response: object,
}[] = [];

console.log('Pusing delete actions for en...')
console.log('Pushing delete actions for en...')
const enResponse = await postLanguageStrings(
'en',
bulkActions.en,
Expand All @@ -31,7 +31,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
logs.push({ responseFor: 'en', response: enResponseJson });


console.log('Pusing delete actions for fr...')
console.log('Pushing delete actions for fr...')
const frResponse = await postLanguageStrings(
'fr',
bulkActions.fr,
Expand All @@ -42,7 +42,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
const frResponseJson = await frResponse.json();
logs.push({ responseFor: 'fr', response: frResponseJson });

console.log('Pusing delete actions for es...')
console.log('Pushing delete actions for es...')
const esResponse = await postLanguageStrings(
'es',
bulkActions.es,
Expand All @@ -52,7 +52,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
const esResponseJson = await esResponse.json();
logs.push({ responseFor: 'es', response: esResponseJson });

console.log('Pusing delete actions for ar...')
console.log('Pushing delete actions for ar...')
const arResponse = await postLanguageStrings(
'ar',
bulkActions.ar,
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/translatte/commands/exportServerStringsToExcel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import xlsx from 'exceljs';

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

async function exportServerStringsToExcel(
apiUrl: string,
Expand Down Expand Up @@ -35,7 +35,7 @@ async function exportServerStringsToExcel(

const keyGroupedStrings = mapToList(
listToGroupList(
serverStrings,
serverStrings.filter(({ page_name, key }) => isTruthyString(page_name) && isTruthyString(key)),
({ page_name, key }) => `${page_name}:${key}`,
),
(list) => {
Expand Down
10 changes: 7 additions & 3 deletions app/scripts/translatte/commands/pushMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
const serverActionsForCurrentLanguage = actions.flatMap((actionItem) => {
if (language === 'en') {
if (actionItem.action === 'add') {
if (isFalsyString(actionItem.value)) {
return undefined;
}

return {
action: 'set' as const,
key: actionItem.key,
Expand Down Expand Up @@ -168,7 +172,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
);

await writeFilePromisify(
`server-actions.json`,
`/tmp/server-actions.json`,
JSON.stringify(serverActions, null, 2),
'utf8',
);
Expand All @@ -193,7 +197,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
const setActions = actions.filter(({ action }) => action === 'set');
const deleteActions = actions.filter(({ action }) => action === 'delete');

console.log(`Pusing deleted actions for ${lang}...`)
console.log(`Pushing deleted actions for ${lang}...`)
const deleteResponse = await postLanguageStrings(
lang,
deleteActions,
Expand Down Expand Up @@ -221,7 +225,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
await applyAction(serverActions.ar.language, serverActions.ar.actions);

await writeFilePromisify(
`push-migration-logs.json`,
`/tmp/push-migration-logs.json`,
JSON.stringify(logs, null, 2),
'utf8',
);
Expand Down
206 changes: 206 additions & 0 deletions app/scripts/translatte/commands/pushStringsDref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import xlsx, { CellValue } from 'exceljs';
import { fetchServerState, postLanguageStrings } from "../utils";
import { encodeDate, isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
import { Language, ServerActionItem, SourceStringItem } from '../types';
import { Md5 } from 'ts-md5';


function getValueFromCellValue(cellValue: CellValue) {
if (isNotDefined(cellValue)) {
return undefined;
}

if (
typeof cellValue === 'number'
|| typeof cellValue === 'string'
|| typeof cellValue === 'boolean'
) {
return cellValue;
}

if (cellValue instanceof Date) {
return encodeDate(cellValue);
}

if ('error' in cellValue) {
return undefined;
}

if ('richText' in cellValue) {
return cellValue.richText.map(({ text }) => text).join('');
}

if ('hyperlink' in cellValue) {
const MAIL_IDENTIFIER = 'mailto:';
if (cellValue.hyperlink.startsWith(MAIL_IDENTIFIER)) {
return cellValue.hyperlink.substring(MAIL_IDENTIFIER.length);
}

return cellValue.hyperlink;
}

if (isNotDefined(cellValue.result)) {
return undefined;
}

if (typeof cellValue.result === 'object' && 'error' in cellValue.result) {
return undefined;
}

// Formula result
return getValueFromCellValue(cellValue.result);
}

async function pushStringsDref(importFilePath: string, apiUrl: string, accessToken: string) {
const strings = await fetchServerState(apiUrl);
const enStrings = strings.filter((string) => string.language === 'en');

const workbook = new xlsx.Workbook();

await workbook.xlsx.readFile(importFilePath);

const firstSheet = workbook.worksheets[0];
const columns = firstSheet.columns.map(
(column) => {
const key = column.values?.[1]?.toString();
if (isNotDefined(key)) {
return undefined;
}
return { key, column: column.number }
}
).filter(isDefined);

const columnMap = listToMap(
columns,
({ key }) => key,
({ column }) => column,
);

const updatedStrings: SourceStringItem[] = [];

firstSheet.eachRow((row) => {
const keyColumn = columnMap['Key'];
const key = isDefined(keyColumn) ? String(getValueFromCellValue(row.getCell(keyColumn).value)) : undefined;

const namespaceColumn = columnMap['Namespace'];
const namespace = isDefined(namespaceColumn) ? String(getValueFromCellValue(row.getCell(namespaceColumn).value)) : undefined;

if (isNotDefined(key) || isNotDefined(namespace)) {
return;
}

const enColumnKey = columnMap['EN'];
const frColumnKey = columnMap['FR'];
const esColumnKey = columnMap['ES'];
const arColumnKey = columnMap['AR'];

const enValue = isDefined(enColumnKey) ? getValueFromCellValue(row.getCell(enColumnKey).value) : undefined;

const strings = enStrings.filter(({ value }) => value === enValue);

if (strings.length > 0) {
strings.forEach((string) => {
const frValue = isDefined(frColumnKey) ? getValueFromCellValue(row.getCell(frColumnKey).value) : undefined;
const esValue = isDefined(esColumnKey) ? getValueFromCellValue(row.getCell(esColumnKey).value) : undefined;
const arValue = isDefined(arColumnKey) ? getValueFromCellValue(row.getCell(arColumnKey).value) : undefined;

updatedStrings.push({
...string,
language: 'fr',
value: String(frValue),
});

updatedStrings.push({
...string,
language: 'es',
value: String(esValue),
});

updatedStrings.push({
...string,
language: 'ar',
value: String(arValue),
});
});
}

if (strings.length === 0) {
const frValue = isDefined(frColumnKey) ? getValueFromCellValue(row.getCell(frColumnKey).value) : undefined;
const esValue = isDefined(esColumnKey) ? getValueFromCellValue(row.getCell(esColumnKey).value) : undefined;
const arValue = isDefined(arColumnKey) ? getValueFromCellValue(row.getCell(arColumnKey).value) : undefined;

const hash = Md5.hashStr(String(enValue));

updatedStrings.push({
key,
page_name: namespace,
hash,
language: 'en',
value: String(enValue),
});

updatedStrings.push({
key,
page_name: namespace,
hash,
language: 'fr',
value: String(frValue),
});

updatedStrings.push({
key,
page_name: namespace,
hash,
language: 'es',
value: String(esValue),
});

updatedStrings.push({
key,
page_name: namespace,
hash,
language: 'ar',
value: String(arValue),
});
}
});

const languageGroupedActions = mapToList(
listToGroupList(
updatedStrings,
({ language }) => language,
(languageString) => {
const serverAction: ServerActionItem = {
action: 'set',
key: languageString.key,
page_name: languageString.page_name,
value: languageString.value,
hash: languageString.hash,
}

return serverAction;
},
),
(actions, language) => ({
language: language as Language,
actions,
})
);

for (let i = 0; i < languageGroupedActions.length; i++) {
const action = languageGroupedActions[i];

console.log(`posting ${action.language} actions...`);
const result = await postLanguageStrings(
action.language,
action.actions,
apiUrl,
accessToken,
)

const resultJson = await result.json();
console.info(resultJson);
}
}

export default pushStringsDref;
Loading
Loading