UNPKG

@atrysglobal/babel-ripper

Version:

Interface and strict typing toolkit for accessing the BABEL unified translation service.

73 lines (72 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BabelRipper = void 0; class BabelRipper { constructor(translations) { this.translations = translations; } /** * Returns the translation result for a given * translation reference. * * @param {string} input - Translation reference. * @returns {string} - Translation result. * * @example * * const dictionary = { * hello: 'messages.hello', * bye: 'messages.bye' * } * * const babel = new BabelProxyService(...); * const result = await babel.loadGuttedTranslations(dictionary); * * result.get(dictionary.hello); // Hola! * result.get(dictionary.bye); // Adiós! * */ get(input) { return (this.translations.find((el) => typeof input === 'string' ? el.id === input : el.id === input.target)?.message || ''); } /** * Returns the translation result for a given * translation reference. * * @deprecated This function will be deprecated in future versions. Please use the `get` function instead. * * @param {string} input - Translation reference. * @returns {string} - Translation result. * * @example * * const dictionary = { * hello: 'messages.hello', * bye: 'messages.bye' * } * * const babel = new BabelProxyService(...); * const result = await babel.loadGuttedTranslations(dictionary); * * result.getTranslation(dictionary.hello); // Hola! * result.getTranslation(dictionary.bye); // Adiós! * */ getTranslation(input) { return (this.translations.find((el) => typeof input === 'string' ? el.id === input : el.id === input.target)?.message || ''); } /** * Returns the translation identifier for a given * translation message. * * @param {string} input - Translation message. * @returns {string} - Translation identifier. * */ getIdentifier(input) { return (this.translations.find((el) => typeof input === 'string' ? el.message === input : el.message === input.message)?.id || ''); } } exports.BabelRipper = BabelRipper;