* Prepares and performs translation on given content. * @param {number[]|string|Uint8Array|Chain} content * @param {boolean} isEncode True for encode, false for decode * @return {Promise} Resolves to translation result
(content, isEncode)
| 47 | * @return {Promise} Resolves to translation result |
| 48 | */ |
| 49 | async translate (content, isEncode) { |
| 50 | try { |
| 51 | // Track translation start time |
| 52 | const startTime = MathUtil.time() |
| 53 | |
| 54 | // Check if translation direction is allowed |
| 55 | if (isEncode === this._reverse && this.isEncodeOnly()) { |
| 56 | throw new InvalidInputError( |
| 57 | `Decoding is not defined in brick '${this.getMeta().title}'`) |
| 58 | } |
| 59 | |
| 60 | // Check for invalid settings |
| 61 | const invalidSettings = this.getSettingsForm().getInvalidFields() |
| 62 | if (invalidSettings.length > 0) { |
| 63 | throw new InvalidInputError( |
| 64 | `Can't ${isEncode ? 'encode' : 'decode'} with invalid settings: ` + |
| 65 | invalidSettings.map(setting => setting.getLabel() + ' (' + (setting.getMessage() || 'none') + ')').join(', ')) |
| 66 | } |
| 67 | |
| 68 | // Wrap content in Chain |
| 69 | content = Chain.wrap(content) |
| 70 | |
| 71 | // Perform translation |
| 72 | if (isEncode !== this._reverse) { |
| 73 | content = Chain.wrap(await this.willEncode(content)) |
| 74 | content = Chain.wrap(await this.performEncode(content)) |
| 75 | content = Chain.wrap(await this.didEncode(content)) |
| 76 | } else { |
| 77 | content = Chain.wrap(await this.willDecode(content)) |
| 78 | content = Chain.wrap(await this.performDecode(content)) |
| 79 | content = Chain.wrap(await this.didDecode(content)) |
| 80 | } |
| 81 | |
| 82 | // Track successful translation |
| 83 | this._lastError = null |
| 84 | this._lastTranslationMeta = { |
| 85 | isEncode, |
| 86 | duration: MathUtil.time() - startTime, |
| 87 | byteCount: !content.needsByteEncoding() ? content.getSize() : null, |
| 88 | charCount: !content.needsTextEncoding() ? content.getLength() : null |
| 89 | } |
| 90 | this.updateView() |
| 91 | return content |
| 92 | } catch (error) { |
| 93 | // Track failed translation |
| 94 | this._lastError = error |
| 95 | this._lastTranslationMeta = null |
| 96 | this.updateView() |
| 97 | throw error |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Wether to reverse translation. |
no test coverage detected