|
|
@@ -1,56 +1,143 @@
|
|
|
+const translation = require("./translation.js").translation;
|
|
|
+
|
|
|
/**
|
|
|
* 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 {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var {Map}
|
|
|
+ * This store phrases in flat notation.
|
|
|
+ */
|
|
|
+ #phrases;
|
|
|
|
|
|
- #collection;
|
|
|
+ /**
|
|
|
+ * @var {?object}
|
|
|
+ * This store object for nested object notation.
|
|
|
+ */
|
|
|
+ #objects;
|
|
|
|
|
|
/**
|
|
|
- * This create new phrasebook, which given collection of phrqses.
|
|
|
- *
|
|
|
- * @param {Map} collection - Collection of phrases to translate from.
|
|
|
+ * This create new phrasebook from phrases map, and optional object
|
|
|
+ * for phrases in object notation.
|
|
|
+ *
|
|
|
+ * @param {Map} phrases - This contain phrases in flat notation.
|
|
|
+ * @param {?object} objects - This contain phrases in object notation.
|
|
|
*/
|
|
|
- constructor(collection) {
|
|
|
- if (!(collection instanceof Map)) {
|
|
|
- throw new Error("Phrasebook must be indialized with map.");
|
|
|
+ constructor(phrases, objects = null) {
|
|
|
+ if (!(phrases instanceof Map)) {
|
|
|
+ throw new TypeError("Phrases must an map.");
|
|
|
}
|
|
|
|
|
|
- this.#collection = collection;
|
|
|
+ if (objects !== null && typeof(objects) !== "object") {
|
|
|
+ throw new TypeError("Objects must be null or object.");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.#phrases = phrases;
|
|
|
+ this.#objects = objects;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 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.
|
|
|
+ * This translate given phrase. When phrase is in the nested object
|
|
|
+ * notation, then try to find phrase in objects. When not, try to find
|
|
|
+ * phrase in the flat phrases. When could not find phrase, then return
|
|
|
+ * not translated phrase. Content always is returned as translation
|
|
|
+ * object, which could be also formated wich numbers, dates and
|
|
|
+ * much more.
|
|
|
*
|
|
|
- * @param {string} phrase - Phrase to translate.
|
|
|
- * @return {string} Translated phrase.
|
|
|
+ * @param {string} phrase - Phrase to translate.
|
|
|
+ * @returns {translation} - Translated phrase.
|
|
|
+ */
|
|
|
+ translate(phrase) {
|
|
|
+ if (this.#is_nested(phrase)) {
|
|
|
+ return this.#translate_nested(phrase);
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.#translate_flat(phrase);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This translate given phrase. When phrase is in the nested object
|
|
|
+ * notation, then try to find phrase in objects. When not, try to find
|
|
|
+ * phrase in the flat phrases. When could not find phrase, then return
|
|
|
+ * not translated phrase. Content always is returned as translation
|
|
|
+ * object, which could be also formated wich numbers, dates and
|
|
|
+ * much more.
|
|
|
+ *
|
|
|
+ * @param {string} phrase - Phrase to translate.
|
|
|
+ * @returns {translation} - Translated phrase.
|
|
|
*/
|
|
|
get(phrase) {
|
|
|
- if (typeof(phrase) !== "string") {
|
|
|
- throw new Error("Phrase to translate must be string.");
|
|
|
+ return this.translate(phrase);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check that phrase is nested or not.
|
|
|
+ *
|
|
|
+ * @param {string} phrase - Phrase to check that is nested
|
|
|
+ * @returns {bool} - True when nested, false when not
|
|
|
+ */
|
|
|
+ #is_nested(phrase) {
|
|
|
+ return phrase.indexOf(".") !== -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This translate object notated phrase.
|
|
|
+ *
|
|
|
+ * @param {string} phrase - Phrase to translate.
|
|
|
+ * @returns {translation} - Translated phrase.
|
|
|
+ */
|
|
|
+ #translate_nested(phrase) {
|
|
|
+ if (this.#objects === null) {
|
|
|
+ return this.#translate_flat(phrase);
|
|
|
}
|
|
|
|
|
|
- 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;
|
|
|
+ const parts = phrase.trim().split(".");
|
|
|
+ let current = this.#objects;
|
|
|
+
|
|
|
+ for (const part in parts) {
|
|
|
+ if (!(part in current)) {
|
|
|
+ return new translation(phrase, false);
|
|
|
}
|
|
|
|
|
|
- return translated;
|
|
|
}
|
|
|
|
|
|
- console.warn("Could not find \"" + phrase + "\" in phrasebook.");
|
|
|
+ if (typeof(current) !== "string") {
|
|
|
+ return new translation(phrase, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new translation(current, true);
|
|
|
+ }
|
|
|
|
|
|
- return phrase;
|
|
|
+ /**
|
|
|
+ * This translate flat phrase.
|
|
|
+ *
|
|
|
+ * @param {string} phrase - Phrase to translate.
|
|
|
+ * @returns {translation} - Translated phrase.
|
|
|
+ */
|
|
|
+ #translate_flat(phrase) {
|
|
|
+ const prepared = phrasebook.prepare(phrase);
|
|
|
+ const found = this.#phrases.has(prepared);
|
|
|
+ const translated = found ? this.#phrases.get(prepared) : phrase;
|
|
|
+
|
|
|
+ return new translation(translated, found);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This prepars phrase, that mean replece all spaces with "_", trim
|
|
|
+ * and also replace all big letters with lowwer.
|
|
|
+ *
|
|
|
+ * @param {string} content - Phrase to preapre.
|
|
|
+ * @return {string} - Prepared phrase.
|
|
|
+ */
|
|
|
+ static prepare(content) {
|
|
|
+ return content
|
|
|
+ .trim()
|
|
|
+ .replaceAll(" ", "_")
|
|
|
+ .replaceAll(".", "_")
|
|
|
+ .toLowerCase();
|
|
|
}
|
|
|
}
|
|
|
|