{"size": 2082, "ext": "js", "max_stars_repo_path": "dist/index-node8-dev.es.js", "max_stars_repo_name": "alpjs/alp-translate", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "dist/index-node8-dev.es.js", "max_issues_repo_name": "alpjs/alp-translate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "dist/index-node8-dev.es.js", "max_forks_repo_name": "alpjs/alp-translate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.76, "max_line_length": 87, "alphanum_fraction": 0.644092219}
import IntlMessageFormat from 'intl-messageformat';
import _t from 'flow-runtime';
import Logger from 'nightingale-logger';

function load(translations, language) {
  let _translationsType = _t.ref('Map', _t.string(), _t.any());

  let _languageType = _t.string();

  const _returnType = _t.return(_t.ref('Map', _t.string(), _t.ref(IntlMessageFormat)));

  _t.param('translations', _translationsType).assert(translations);
  _t.param('language', _languageType).assert(language);

  const result = new Map();

  (function loadMap(map, prefix) {
    map.forEach((value, key) => {
      if (typeof value === 'object') {
        return loadMap(value, `${prefix}${key}.`);
      }

      result.set(`${prefix}${key}`, new IntlMessageFormat(value, language));
    });
  })(translations, '');

  return _returnType.assert(result);
}

const logger = new Logger('alp:translate');

function alpTranslate(dirname) {
  let _dirnameType = _t.string();

  _t.param('dirname', _dirnameType).assert(dirname);

  dirname = _dirnameType.assert(dirname.replace(/\/*$/, '/'));
  return app => {
    Object.assign(app.context, {
      t(key, args) {
        let _keyType = _t.string();

        let _argsType = _t.nullable(_t.object());

        const _returnType = _t.return(_t.string());

        _t.param('key', _keyType).assert(key);

        _t.param('args', _argsType).assert(args);

        const msg = app.translations.get(this.language).get(key);
        if (!msg) {
          logger.warn('invalid msg', { language: this.language, key });
          return _returnType.assert(key);
        }

        return _returnType.assert(msg.format(args));
      }
    });

    app.translations = new Map();
    app.config.get('availableLanguages').forEach(language => {
      let _languageType = _t.string();

      _t.param('language', _languageType).assert(language);

      const translations = app.config.loadConfigSync(dirname + language);
      app.translations.set(language, load(translations, language));
    });
  };
}

export default alpTranslate;
//# sourceMappingURL=index-node8-dev.es.js.map
