Skip to content

v0.2.48..v0.2.49 changeset translation_assistant.js

Garret Voltz edited this page Oct 2, 2019 · 1 revision
diff --git a/translations/translation_assistant.js b/translations/translation_assistant.js
index 171806d..f508aeb 100644
--- a/translations/translation_assistant.js
+++ b/translations/translation_assistant.js
@@ -8,17 +8,36 @@ if (typeof hoot !== 'undefined') {
 
 translation_assistant = {
     difference: function(a, b) {
-            var diff = [];
-            for (var k in a) {
-                if (b.indexOf(a[k]) === -1) {
-                    diff.push(a[k]);
-                }
+        var diff = [];
+        for (var k in a) {
+            if (b.indexOf(a[k]) === -1) {
+                diff.push(a[k]);
             }
-            return diff;
-        },
+        }
+        return diff;
+    },
+    //Set the tag `tagKey=value` or append it unless `replace` is `true`
+    setTag: function(tags, tagKey, value, replace)
+    {
+        if (replace) { //force the tag value replacement, order is not possible to define in translation mapping :(
+            tags[tagKey] = value;
+        }
+        else if (tags[tagKey] && (tags[tagKey] !== value)) { //append values to existing tags
+            if (tags[tagKey] === "yes") { //replace a tag=yes with the new value instead of appending
+                tags[tagKey] = value;
+            } else { //append the value if unique and not yes
+                var values = tags[tagKey].split(";");
 
+                if (values.indexOf(value) === -1) {
+                    tags[tagKey] = tags[tagKey] + ";" + value;
+                }
+            }
+        } else {
+            tags[tagKey] = value;
+        }
+    },
     //Takes 'attrs' and returns OSM 'tags'
-    translateAttributes: function(attrs, layerName, attributeMapping, fcode, schema)
+    translateAttributes: function(attrs, layerName, attributeMapping, fcode, schema, replace)
     {
         var tags = {};
         var extras = []
@@ -54,19 +73,22 @@ translation_assistant = {
                             tags[tagKey] = k + "=" + attrs[key];
                         }
                     } else if (tagValue == k) { //tag is set to attribute value
-                        if (tags[tagKey] && (tags[tagKey] !== attrs[key])) { //append values to existing tags
-                            tags[tagKey] = tags[tagKey] + ";" + attrs[key];
-                        } else {
-                            tags[tagKey] = attrs[key];
-                        }
+                        translation_assistant.setTag(tags, tagKey, attrs[key], replace);
                     } else if (typeof tagValue === 'string') { //attribute is mapped to a static tag
-                        tags[tagKey] = tagValue;
+                        translation_assistant.setTag(tags, tagKey, tagValue, replace);
                     } else { //attribute value is mapped to a tag value (if tag key and value is not empty string)
-                        if (attrs[key].length > 0 && tagValue[attrs[key]].length > 0) {
-                            if (tags[tagKey] && (tags[tagKey] !== tagValue[attrs[key]])) { //append values to existing tags
-                                tags[tagKey] = tags[tagKey] + ";" + tagValue[attrs[key]];
-                            } else {
-                                tags[tagKey] = tagValue[attrs[key]];
+                        if (attrs[key].length > 0) {
+                            if (tagValue[attrs[key]] && tagValue[attrs[key]].length > 0) {
+                                translation_assistant.setTag(tags, tagKey, tagValue[attrs[key]], replace);
+                            } else { //check if the attribute value is a regular expression and run that
+                                for (var attrReg in tagValue) {
+                                    if (attrReg.startsWith("/") && attrReg.endsWith("/")) {
+                                        var regex = new RegExp(attrReg.substring(1, attrReg.length - 1));
+                                        if (regex.test(attrs[key])) {
+                                            translation_assistant.setTag(tags, tagKey, tagValue[attrReg], replace);
+                                        }
+                                    }
+                                }
                             }
                         }
                     }
@@ -74,7 +96,6 @@ translation_assistant = {
             }
         }
 
-
         //If necessary, convert from English TDS to OSM+
         if (tags['Feature Code'] || fcode) {
             if (!tags['Feature Code'] && fcode) {
Clone this wiki locally