Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/i18n / dist / onesky / translations.js
Size: Mime:
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.uploadTranslationFiles = exports.updateTranslationFiles = exports.updateTranslationFile = exports.downloadMergedTranslationFiles = exports.getMergedProjectTranslations = exports.getProjectTranslations = void 0;

var _deepmerge = _interopRequireDefault(require("deepmerge"));

var _extract = require("./extract");

var _api = require("./api");

var _transform = require("./transform");

var _utils = require("./utils");

var _diff = require("./diff");

/**
 * Manage translations for OneSky
 *
 * @module @doodle/i18n/disty/onesky/translations
 */

/**
 * Download translations from a OneSky project for all defined languages in the project
 *
 * @param {string} projectId
 * @param {OneSkyCredentials} credentials
 * @returns {Promise<object>}
 * @async
 */
const getProjectTranslations = async (projectId, credentials) => {
  const projectLanguages = await (0, _api.getProjectLanguages)(projectId, credentials);
  const languageCodes = projectLanguages.map(({
    code
  }) => code);
  const projectMessages = await Promise.all(languageCodes.map(language => (0, _api.getProjectMessagesByLanguage)({
    projectId,
    language
  }, credentials)));
  return projectMessages.reduce((result, messages, index) => {
    result[languageCodes[index]] = (0, _transform.unflatten)(messages);
    return result;
  }, {});
};
/**
 * Download translations from multiple OneSky projects for all defined languages
 * in the respective projects and combine the message translations
 *
 * @param {string[]} projectIds
 * @param {OneSkyCredentials} credentials
 * @returns {Promise<object>}
 * @async
 */


exports.getProjectTranslations = getProjectTranslations;

const getMergedProjectTranslations = async (projectIds, credentials) => {
  const errors = {};
  const projectsTranslations = await Promise.all(projectIds.map(projectId => getProjectTranslations(projectId, credentials).catch(error => {
    errors[projectId] = error;
  })));

  if (Object.keys(errors).length) {
    throw new Error(JSON.stringify(errors));
  }

  return _deepmerge.default.all(projectsTranslations);
};
/**
 * Download all translations from multiple OneSky projects and write them to JSON files by language
 *
 * @param {string[]} projectIds
 * @param {string} targetPath
 * @param {OneSkyCredentials} credentials
 * @returns {Promise<object>}
 * @async
 */


exports.getMergedProjectTranslations = getMergedProjectTranslations;

const downloadMergedTranslationFiles = async (projectIds, targetPath, credentials) => {
  const combinedTranslations = await getMergedProjectTranslations(projectIds, credentials);
  await Promise.all(Object.keys(combinedTranslations).map(language => (0, _utils.writeJsonFile)(`${targetPath}/${language}.json`, combinedTranslations[language])));
  return combinedTranslations;
};
/**
 * Update a translation file containing hierarchical messages with changes from source messages
 *
 * @param {string} file
 * @param {object} sourceMessages
 * @param {bool} includeValues
 * @returns {Promise}
 * @async
 */


exports.downloadMergedTranslationFiles = downloadMergedTranslationFiles;

const updateTranslationFile = async (file, sourceMessages, includeValues = false) => {
  const translation = await (0, _utils.readJsonFile)(file);
  const messages = (0, _transform.flatten)(translation);
  const addedKeys = includeValues ? (0, _diff.getAddedKeys)(sourceMessages, messages) : [];
  const changedKeys = includeValues ? (0, _diff.getChangedKeys)(sourceMessages, messages) : [];
  const renamed = (0, _diff.getRenamedKeys)(sourceMessages, messages);
  const deleted = (0, _diff.getDeletedKeys)(sourceMessages, messages);
  const sourceMessageEntries = Object.entries(sourceMessages);
  const updatedMessages = (0, _diff.updateMessages)(messages, {
    added: sourceMessageEntries.filter(([k]) => addedKeys.indexOf(k) >= 0),
    changed: sourceMessageEntries.filter(([k]) => changedKeys.indexOf(k) >= 0),
    renamed,
    deleted
  });
  await (0, _utils.writeJsonFile)(file, updatedMessages);
};
/**
 * Update local translation files with changes from the source for all languages defined by the OneSky project
 *
 * @param {string} projectId
 * @param {OneSkyCredentials} credentials
 * @param {string} sourcePattern
 * @param {string} targetPath
 * @param {string} includeValues
 * @returns {Promise}
 * @async
 */


exports.updateTranslationFile = updateTranslationFile;

const updateTranslationFiles = async (projectId, credentials, sourcePattern, targetPath, includeValues = false) => {
  const projectLanguages = await (0, _api.getProjectLanguages)(projectId, credentials);
  const languageCodes = projectLanguages.map(({
    code
  }) => code);
  const baseLanguage = projectLanguages.filter(({
    is_base_language: isBaseLanguage
  }) => isBaseLanguage)[0].code;
  const sourceMessages = await (0, _extract.extractDefaultMessages)(`${sourcePattern}`);
  const errors = {};
  await Promise.all(languageCodes.map(language => updateTranslationFile(`${targetPath}/onesky/${projectId}/${language}.json`, sourceMessages, includeValues ? true : language === baseLanguage).catch(error => {
    errors[language] = error;
  })));

  if (Object.keys(errors).length) {
    console.error(errors);
  }
};
/**
 * Upload translation files for all languages of the project, not only the base language!
 *
 * @param {string} path path to folder containing the hierarchical `${language}.json` translation files
 * @param {OneSkyTranslationOptions} translationOptions
 * @param {OneSkyCredentials} credentials
 * @param {OneSkySyncOptions} syncOptions
 * @returns {Promise}
 * @async
 */


exports.updateTranslationFiles = updateTranslationFiles;

const uploadTranslationFiles = async (path, {
  projectId,
  fileName
}, credentials, {
  keepStrings,
  format
} = {}) => {
  const projectLanguages = await (0, _api.getProjectLanguages)(projectId, credentials);
  const languageCodes = projectLanguages.map(({
    code
  }) => code);

  const doUpload = async language => {
    const translations = await (0, _utils.readJsonFile)(`${path}/onesky/${projectId}/${language}.json`);
    await (0, _api.uploadProjectTranslation)({
      language,
      projectId,
      fileName
    }, (0, _transform.flatten)(translations), credentials, {
      keepStrings,
      format
    });
  };

  await Promise.all(languageCodes.map(doUpload));
};

exports.uploadTranslationFiles = uploadTranslationFiles;
//# sourceMappingURL=translations.js.map