Manual Reference Source Test

src/helpers/Language.js

import LanguageModel from '../models/Language';
import LanguageService from '../services/LanguageService';
import { aggregate } from '../utilities/ClassUtility';
import Helper from './Helper';


/**
 * Helper class representing a Language.
 */
class Language extends aggregate(LanguageModel, Helper) {
  /**
   * Constructor for Language.  Can be used to represent a language, translate a key to text, and
   * pull back translations from the server, storing it in this._translations.
   *
   * @param {Object} [json]                   - Object containing response described in LangaugeModel
   * @param {Boolean} [getTranslations=false] - If true, pull back translations from server
   */
  constructor(json = {}, getTranslations = false) {
    super(json);
    /** @type {Object} */
    this._translations = {};
    if (getTranslations) {
      this.getTranslations();
    }
  }

  /** @type {String}  - The type */
  static get type() {
    return 'languages';
  }

  /**
   * Synchronizes the language information with the server and sets it to this._translations
   *
   * @return {Promise}  - Returns a key/value object with the key being the word and value being
   *                      the translation.
   *
   * @example
   * const lang = new Language({ iso: 'en' });
   * lang.getTranslations();
   *
   * @example
   * const lang = new Language({ iso: 'en' }, true);
   * // automatically calls lang.getTranslations();
   */
  getTranslations() {
    return new LanguageService().getTranslations(this.iso).then(t => (this._translations = t));
  }

  /**
   * Returns the translation for the language based on the 'key'.
   *
   * @param  {!String} key - The key corresponding to the translation.
   * @return {String}      - The value correspondingto the translation.
   *
   * @example
   * const lang = new Language({ iso: 'en' }, true);
   * console.log(lang.translate('welcome_description'));
   *
   * @example
   * languageService.getLanguages().then((languages: Language[]) => {
   *   languages[0].getTranslations() // will get translations for the language;
   *     .then(()=>{
   *       console.log(languages[0].translate('welcome_text'));
   *     });
   * });

   */
  translate(key) {
    return this._translations[key];
  }
}

export default Language;