| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /**
- * This class repesents phrasebook, which is something like dictionary,
- * but not for words, for all phrases in app. It give functions which
- * could find and translates phrases.
- */
- class phrasebook {
- #collection;
- /**
- * This create new phrasebook, which given collection of phrqses.
- *
- * @param {Map} collection - Collection of phrases to translate from.
- */
- constructor(collection) {
- if (!(collection instanceof Map)) {
- throw new Error("Phrasebook must be indialized with map.");
- }
- this.#collection = collection;
- }
- /**
- * This function translate phrase to lang which coresponds to the
- * phrasebook. It try to find given phrase in the phrasebook, and
- * when find it, return that. In the other way, when phrase could
- * not being fount, write warning to the debug console, and return
- * phrase in given not translated form. When phrase in phrasebook
- * is not string, then also return not translated phrase and
- * report it into developer console.
- *
- * @param {string} phrase - Phrase to translate.
- * @return {string} Translated phrase.
- */
- get(phrase) {
- if (typeof(phrase) !== "string") {
- throw new Error("Phrase to translate must be string.");
- }
- if (this.#collection.has(phrase)) {
- const translated = this.#collection.get(phrase);
-
- if (typeof(translated) !== "string") {
- console.error("Result for \"" + phrase + "\" is not string.");
- return phrase;
- }
- return translated;
- }
- console.warn("Could not find \"" + phrase + "\" in phrasebook.");
- return phrase;
- }
- }
- exports.phrasebook = phrasebook;
|