getTranslation.js 675 B

123456789101112131415161718192021222324252627
  1. import localesData from "../state/locales.js";
  2. import translateText from "../models/Translation.js";
  3. const getTranslation = async (names) => {
  4. const translated = {};
  5. const untranslated = [];
  6. const localesMap = await localesData.get(names);
  7. Object.keys(localesMap).forEach(key => {
  8. if (localesMap[key]) {
  9. translated[key] = localesMap[key];
  10. }
  11. else {
  12. untranslated.push(key);
  13. }
  14. });
  15. if (untranslated.length === 0) {
  16. return translated;
  17. }
  18. const translations = await translateText(untranslated);
  19. await localesData.set(translations);
  20. Object.assign(translated, translations);
  21. return translated;
  22. }
  23. export default getTranslation;