Skip to content

Commit

Permalink
[CP-432] Improving Phrase flow
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbezsmertnyi committed Aug 23, 2021
1 parent 9088543 commit 5c7bc50
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 210 deletions.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"prerelease": "npm run app:prerelease",
"storybook:serve": "npm run app:storybook:serve --",
"storybook:build": "npm run app:storybook:build --",
"dist": "npm run app:dist --",
"dist:all": "npm run app:dist:all --",
"dist:dev": "npm run app:dist:dev --",
"dist:dev:all": "npm run app:dist:dev:all --",
"dist:prod": "npm run app:dist:prod --",
"dist:prod:all": "npm run app:dist:dev:prod --",
"APP COMMANDS": "=========================================================",
"app:develop": "lerna run --scope @mudita/mudita-center-app develop",
"app:lint": "lerna run --scope @mudita/mudita-center-app lint",
Expand All @@ -28,8 +30,10 @@
"app:prerelease": "lerna run --scope @mudita/mudita-center-app prerelease",
"app:storybook:serve": "lerna run --scope @mudita/mudita-center-app storybook:serve",
"app:storybook:build": "lerna run --scope @mudita/mudita-center-app storybook:build",
"app:dist": "lerna run --scope @mudita/mudita-center-app dist",
"app:dist:all": "lerna run --scope @mudita/mudita-center-app dist:all",
"app:dist:dev": "lerna run --scope @mudita/mudita-center-app dist:dev",
"app:dist:prod": "lerna run --scope @mudita/mudita-center-app dist:prod",
"app:dist:dev:all": "lerna run --scope @mudita/mudita-center-app dist:dev:all",
"app:dist:prod:all": "lerna run --scope @mudita/mudita-center-app dist:prod:all",
"app:electron:rebuild-serialport": "lerna run --scope @mudita/mudita-center-app electron:rebuild-serialport --",
"PURE COMMANDS": "========================================================",
"pure:build": "lerna run --scope @mudita/pure build",
Expand Down
18 changes: 9 additions & 9 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
"lint:typecheck": "tsc --noEmit",
"lint:fix": "npm run lint:js -- --fix",
"DIST COMMANDS": "=========================================================",
"dist": "npm run build && electron-builder --publish never",
"dist:all": "npm run dist -- -mwl",
"dist:dev": "npm run build && electron-builder --publish never",
"dist:dev:all": "npm run dist -- -mwl",
"dist:prod": "npm run build && electron-builder --publish never",
"dist:prod:all": "npm run dist -- -mwl",
"TEST COMMANDS": "=========================================================",
"test": "TZ=UTC jest --config=jest-unit.config.js",
"test:all": "TZ=UTC jest --config=jest.config.js",
Expand All @@ -38,10 +40,8 @@
"storybook:serve": "start-storybook -p 6006 --no-dll",
"storybook:build": "build-storybook",
"DATA UPDATE/OVERWRITE COMMANDS": "=========================================",
"translations:update": "node ./scripts/updateDefaultTranslations.js",
"translations:overwrite": "cross-env OVERWRITE=true npm run translations:update",
"translations:send": "node ./scripts/updatePhraseTranslations.js",
"translations:sort": "node ./scripts/sortTranslations.js",
"translations:sort": "ts-node ./scripts/sort-translations.ts",
"translations:sync": "ts-node ./scripts/sync-translations.ts",
"fonts:download": "node ./scripts/downloadFonts.js",
"news:download": "ts-node ./scripts/downloadNews.ts",
"app-configuration:download": "ts-node ./scripts/downloadAppConfiguration.ts",
Expand All @@ -51,9 +51,9 @@
"prerelease": "npm run translations:overwrite && npm run build",
"prestart": "npm run build",
"pretest:e2e": "npm run build",
"predevelop": "npm run translations:update",
"posttranslations:send": "npm run translations:sort",
"posttranslations:update": "npm run translations:sort",
"predist:prod": "npm run translations:sync",
"predist:prod:all": "npm run translations:sync",
"posttranslations:sync": "npm run translations:sort",
"posttranslations:sort": "prettier --write src/**/*.json",
"posttest:coverage": "prettier --write ./jest.coverage.json"
},
Expand Down
40 changes: 40 additions & 0 deletions packages/app/scripts/sort-translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace SortTranslations {
const fs = require("fs-extra")
const { availableLanguages } = require("../src/translations.config.json")

const script = async () => {
console.log("Sorting translations")

try {
for (const { code } of availableLanguages) {
const filePath = `./src/renderer/locales/default/${code}.json`

if (await fs.pathExists(filePath)) {
let data: Record<string, string> = await fs.readJson(filePath)

data = Object.entries(data)
.sort((a, b) => {
const keyA = a[0].toLowerCase()
const keyB = b[0].toLowerCase()
return keyA > keyB ? 1 : keyB > keyA ? -1 : 0
})
.reduce(
(
object: Record<string, string>,
[key, value]: [string, string]
) => ((object[key] = value), object),
{}
)

await fs.writeFileSync(filePath, JSON.stringify(data, null, 2))

console.log(`Translation for ${code} sorted`)
}
}
} catch (error) {
console.log(error)
}
}

script()
}
25 changes: 0 additions & 25 deletions packages/app/scripts/sortTranslations.js

This file was deleted.

164 changes: 164 additions & 0 deletions packages/app/scripts/sync-translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
namespace SyncTranslation {
const axios = require("axios")
const path = require("path")
const fs = require("fs-extra")
const { availableLanguages } = require("../src/translations.config.json")
const {
localesUrl,
phraseUrl,
axiosConfig,
axiosDevConfig,
} = require("../src/common/configs/phrase")
const FormData = require("form-data")

require("dotenv").config({
path: path.join(__dirname, "../../../.env"),
})

interface AvailableLanguage {
id: string
code: string
}

interface ExternalKey {
id: string
content: string
unverified: boolean
excluded: boolean
plural_suffix: string
key: {
id: string
name: string
plural: boolean
data_type: string
tags: string[]
}
created_at: string
updated_at: string
placeholders: string[]
state: string
locale: {
id: string
name: string
code: string
}
}

const getTranslations = async (
languageId: string
): Promise<ExternalKey[]> => {
let haveData = true
let currentPage = 0
let translations = []

try {
while (haveData) {
const { data } = await axios.get(
`${localesUrl}/${languageId}/translations`,
{
...axiosConfig,
params: {
per_page: 100,
page: currentPage + 1,
},
}
)

if (data.length) {
translations.push(...data)
currentPage++
} else {
haveData = false
}
}

return translations
} catch (error) {
console.log(error)
return []
}
}

const uploadTranslations = async (languageId: string, filePath: string) => {
try {
await fs.ensureFileSync(path.resolve(filePath))

const formData = new FormData()
formData.append("file_format", "react_simple_json")
formData.append("locale_id", languageId)
formData.append("file", fs.createReadStream(filePath))

await axios.post(`${phraseUrl}/uploads`, formData, {
headers: {
...axiosDevConfig.headers,
...formData.getHeaders(),
},
})
} catch (error) {
console.log(error)
}
}

const deleteTranslation = async (ids: string[]) => {
try {
ids.forEach(async (id) => {
await axios.delete(`${phraseUrl}/keys/${id}`, {
...axiosDevConfig,
})
})
} catch (error) {
console.log(error)
}
}

const updateInternalTranslations = async (
languageId: string,
filePath: string
) => {
try {
const { data } = await axios.get(`${localesUrl}/${languageId}/download`, {
...axiosConfig,
params: { file_format: "react_simple_json" },
})

await fs.writeJson(path.resolve(filePath), data)
} catch (error) {
console.log(error)
}
}

const script = async () => {
try {
console.log(`Syncing local translations with phrase.com`)

const localesDir = "./src/renderer/locales/default/"

availableLanguages.forEach(async (language: AvailableLanguage) => {
const localesJsonPath = path.join(localesDir, `${language.code}.json`)

await uploadTranslations(language.id, localesJsonPath)

const internalTranslations = await fs.readJsonSync(localesJsonPath)
const externalTranslations = await getTranslations(language.id)

const removedDiff = externalTranslations.reduce(
(acc: string[], value: ExternalKey) => {
if (!internalTranslations.hasOwnProperty(value.key.name)) {
acc.push(value.key.id)
}

return acc
},
[]
)

await deleteTranslation(removedDiff)
await updateInternalTranslations(language.id, localesJsonPath)
})
} catch (error) {
console.log(error.response)
}
}

script()
}
86 changes: 0 additions & 86 deletions packages/app/scripts/updateDefaultTranslations.js

This file was deleted.

Loading

0 comments on commit 5c7bc50

Please sign in to comment.