Skip to content

Commit

Permalink
feat(site): add cache for generated languages
Browse files Browse the repository at this point in the history
  • Loading branch information
vitonsky committed Jun 24, 2024
1 parent aef3e62 commit c5b8d50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions site/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Generated files
.docusaurus
.cache-loader
.locales-cache.json

# Misc
.DS_Store
Expand Down
25 changes: 25 additions & 0 deletions site/translateLocales.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crypto from 'crypto';
import { existsSync } from 'fs';
import path from 'path';
import { Scheduler } from '@translate-tools/core/scheduling/Scheduler';
import { YandexTranslator } from '@translate-tools/core/translators/YandexTranslator';
Expand Down Expand Up @@ -44,14 +46,37 @@ const localesDirectories = ['./src/components/Landing/locales'];
const source = await readFile(
path.join(localesDir, sourceLanguage + '.json'),
).then((buffer) => JSON.parse(buffer.toString()));

const cacheFilename = path.resolve('./.locales-cache.json');
const translationCache = existsSync(cacheFilename)
? await readFile(cacheFilename).then((buffer) =>
JSON.parse(buffer.toString()),
)
: {};

const sourceDataHash = crypto
.createHash('sha512')
.update(JSON.stringify(source, null, '\t'))
.digest('hex');
const langs = languages.filter((lang) => lang !== sourceLanguage);
for (const lang of langs) {
if (translationCache[lang] === sourceDataHash) {
console.log(
`Skip translation for "${lang}", since already have translated version`,
);
continue;
}

console.log(`Translate messages for "${lang}"`);
const translatedObject = await translateObject(source, sourceLanguage, lang);
await writeFile(
path.join(localesDir, lang + '.json'),
JSON.stringify(translatedObject, null, '\t'),
);

// Sync cache
translationCache[lang] = sourceDataHash;
await writeFile(cacheFilename, JSON.stringify(translationCache, null, '\t'));
}
}
})();

0 comments on commit c5b8d50

Please sign in to comment.