diff --git a/src/main/webapp/js/translate.js b/src/main/webapp/js/translate.js index ea46ff7..95cacc0 100644 --- a/src/main/webapp/js/translate.js +++ b/src/main/webapp/js/translate.js @@ -1,349 +1,346 @@ var defaultTranslationMap = null; var enTranslationMap = null; var i18nIsInitialized; var mathTools = require('./tools/math.js'); function tr2(key, args) { if (key === null) { console.log("ERROR: key was null?"); return ""; } if (defaultTranslationMap === null) { console.log("ERROR: defaultTranslationMap was not initialized?"); return key; } key = key.toLowerCase(); var val = defaultTranslationMap[key]; if (!val && enTranslationMap) val = enTranslationMap[key]; if (!val) return key; return stringFormat(val, args); } function tr(key, args) { if (key !== key.toLowerCase()) console.log("key " + key + " has to be lower case"); return tr2("web." + key, args); } function stringFormat(str, args) { if (typeof args === 'string') args = [args]; if (str.indexOf("%1$s") >= 0) { // With position arguments ala %2$s return str.replace(/\%(\d+)\$s/g, function (match, matchingNum) { matchingNum--; return typeof args[matchingNum] !== 'undefined' ? args[matchingNum] : match; }); } else { // No position so only values ala %s var matchingNum = 0; return str.replace(/\%s/g, function (match) { var val = typeof args[matchingNum] !== 'undefined' ? args[matchingNum] : match; matchingNum++; return val; }); } } function initI18N() { if (global.$) { document.title = tr('document_title') + ' - ' + document.title; $('.pointFlag').each(function () { $(this).attr('title', tr('drag_to_reorder')); }); $('.pointDelete').each(function () { $(this).attr('title', tr('delete_from_route')); }); $('.pointAdd').each(function () { $(this).attr('title', tr('add_destination')); }); var location_points = $("#locationpoints > div.pointDiv > input.pointInput"); var l = location_points.size; $(location_points).each(function (index) { if (index === 0) $(this).attr("placeholder", tr("from_hint")); else if (index === (l - 1)) $(this).attr("placeholder", tr("to_hint")); else $(this).attr("placeholder", tr("via_hint")); }); } } function enhanceTranslations(locale) { // Modify existing English strings. enTranslationMap['web.set_end'] = 'Set as ride end'; enTranslationMap['web.set_intermediate'] = 'Add intermediate point'; enTranslationMap['web.set_start'] = 'Set as ride start'; // Add new English strings. enTranslationMap['web.add_destination'] = 'Add a destination'; enTranslationMap['web.directions_credits'] = 'Directions by %1$s'; enTranslationMap['web.document_title'] = 'Draw my Route'; enTranslationMap['web.exit_fullscreen'] = 'Exit full-screen'; enTranslationMap['web.layer_mapbox_satellite'] = 'Satellite'; enTranslationMap['web.layer_mapbox_streets'] = 'Map'; enTranslationMap['web.layer_opencyclemap'] = 'Cycle Map'; enTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; enTranslationMap['web.layer_swisstopo_mtb'] = 'MTB Routes'; enTranslationMap['web.layer_swisstopo_velo'] = 'Cycle Routes'; enTranslationMap['web.mapbox_style_id'] = 'cio5vq9cy00b8bzm1ymps38kk'; enTranslationMap['web.route_duration_title'] = 'Duration'; enTranslationMap['web.route_elevation_title'] = 'Elevation'; enTranslationMap['web.route_distance_title'] = 'Distance'; enTranslationMap['web.searching_route'] = 'Searching route'; enTranslationMap['web.swisstopo_url'] = 'https://www.swisstopo.admin.ch/en/home.html'; enTranslationMap['web.view_fullscreen'] = 'View full-screen'; if (defaultTranslationMap === undefined) { return; } // Add translations. + defaultTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; defaultTranslationMap['web.mapbox_style_id'] = 'ciscgvq3i002a2zo1v8xm1lxr'; // local language defaultTranslationMap['web.swisstopo_url'] = 'https://www.swisstopo.admin.ch/de/home.html'; switch (locale) { case 'ar': defaultTranslationMap['web.mapbox_style_id'] = 'cjh42x1hy1cdc2rnz26tc5u2n'; break; case 'de_CH': case 'de_DE': case 'de-CH': case 'de-DE': case 'de': defaultTranslationMap['web.add_destination'] = 'Füge ein Ziel hinzu'; defaultTranslationMap['web.directions_credits'] = 'Routen von %1$s'; defaultTranslationMap['web.document_title'] = 'Meine Route zeichnen'; defaultTranslationMap['web.exit_fullscreen'] = 'Vollbildschirm beenden'; defaultTranslationMap['web.layer_mapbox_satellite'] = 'Satelit'; defaultTranslationMap['web.layer_mapbox_streets'] = 'Karte'; defaultTranslationMap['web.layer_opencyclemap'] = 'Fahrradkarte'; - defaultTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; defaultTranslationMap['web.layer_swisstopo_mtb'] = 'MTB Routen'; defaultTranslationMap['web.layer_swisstopo_velo'] = 'Fahrradrouten'; defaultTranslationMap['web.mapbox_style_id'] = 'cio5x9jj1002rmbm7tb9io0ye'; defaultTranslationMap['web.route_distance_title'] = 'Distanz'; defaultTranslationMap['web.route_duration_title'] = 'Dauer'; defaultTranslationMap['web.route_elevation_title'] = 'Erhebung'; defaultTranslationMap['web.searching_route'] = 'Route suchen'; defaultTranslationMap['web.view_fullscreen'] = 'Vollbildschirm anzeigen'; break; case 'es_ES': case 'es-ES': case 'es': // Modify existing Spanish strings. defaultTranslationMap['web.mtb'] = 'BTT'; defaultTranslationMap['web.start_label'] = 'Punto de salida'; defaultTranslationMap['web.end_label'] = 'Punto final'; defaultTranslationMap['web.set_start'] = 'Elegir como punto de salida'; defaultTranslationMap['web.set_intermediate'] = 'Elegir como punto intermedio'; defaultTranslationMap['web.set_end'] = 'Elegir como punto final'; // Add new Spanish strings. defaultTranslationMap['web.add_destination'] = 'Añadir destino'; defaultTranslationMap['web.directions_credits'] = 'Rutas y datos de mapas %1$s'; defaultTranslationMap['web.document_title'] = 'Crear mi ruta'; defaultTranslationMap['web.exit_fullscreen'] = 'Salir del modo pantalla completa'; defaultTranslationMap['web.layer_mapbox_satellite'] = 'Satélite'; defaultTranslationMap['web.layer_mapbox_streets'] = 'Mapa'; defaultTranslationMap['web.layer_opencyclemap'] = 'Rutas ciclables'; - defaultTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; defaultTranslationMap['web.layer_swisstopo_mtb'] = 'Rutas BTT'; defaultTranslationMap['web.layer_swisstopo_velo'] = 'Rutas en bici'; defaultTranslationMap['web.mapbox_style_id'] = 'ciscgw7c2002e2xp8hbjfy75s'; defaultTranslationMap['web.route_duration_title'] = 'Duración'; defaultTranslationMap['web.route_elevation_title'] = 'Desnivel'; defaultTranslationMap['web.route_distance_title'] = 'Distancia'; defaultTranslationMap['web.searching_route'] = 'Buscando ruta'; defaultTranslationMap['web.view_fullscreen'] = 'Pasar a modo pantalla completa'; break; case 'fr_CH': case 'fr_FR': case 'fr-CH': case 'fr-FR': case 'fr': defaultTranslationMap['web.add_destination'] = 'Ajouter une destination'; defaultTranslationMap['web.directions_credits'] = 'Itinéraires de %1$s'; defaultTranslationMap['web.document_title'] = 'Tracer mon parcours'; defaultTranslationMap['web.exit_fullscreen'] = 'Quitter le mode plein écran'; defaultTranslationMap['web.layer_mapbox_satellite'] = 'Satellite'; defaultTranslationMap['web.layer_mapbox_streets'] = 'Carte'; defaultTranslationMap['web.layer_opencyclemap'] = 'Carte vélo'; - defaultTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; defaultTranslationMap['web.layer_swisstopo_mtb'] = 'Itinéraires VTT'; defaultTranslationMap['web.layer_swisstopo_velo'] = 'Itinéraires vélo'; defaultTranslationMap['web.mapbox_style_id'] = 'cio5x7sk9007cydnkr2wjs610'; defaultTranslationMap['web.route_duration_title'] = 'Durée'; defaultTranslationMap['web.route_elevation_title'] = 'Dénivellation'; defaultTranslationMap['web.route_distance_title'] = 'Distance'; defaultTranslationMap['web.searching_route'] = "Recherche de l'itinéraire"; defaultTranslationMap['web.swisstopo_url'] = 'https://www.swisstopo.admin.ch/fr/home.html'; defaultTranslationMap['web.view_fullscreen'] = 'Passer en mode plein écran'; break; case 'it_CH': case 'it_IT': case 'it-CH': case 'it-IT': case 'it': defaultTranslationMap['web.add_destination'] = 'Aggiungi destinazione'; defaultTranslationMap['web.directions_credits'] = 'Direzione verso %1$s'; defaultTranslationMap['web.document_title'] = 'Disegna il perscorso'; defaultTranslationMap['web.exit_fullscreen'] = 'Esci da schermo intero'; defaultTranslationMap['web.layer_mapbox_satellite'] = 'Satellite'; defaultTranslationMap['web.layer_mapbox_streets'] = 'Mappa'; defaultTranslationMap['web.layer_opencyclemap'] = 'Mappa percorso'; - defaultTranslationMap['web.layer_swisstopo_map'] = 'Swisstopo'; defaultTranslationMap['web.layer_swisstopo_mtb'] = 'Percorsi MTB'; defaultTranslationMap['web.layer_swisstopo_velo'] = 'Percorsi bicicletta'; defaultTranslationMap['web.route_distance_title'] = 'Distanza'; defaultTranslationMap['web.route_duration_title'] = 'Durata'; defaultTranslationMap['web.route_elevation_title'] = 'Dislivello'; defaultTranslationMap['web.searching_route'] = 'Cercando percorso'; defaultTranslationMap['web.swisstopo_url'] = 'https://www.swisstopo.admin.ch/it/home.html'; defaultTranslationMap['web.view_fullscreen'] = 'Schermo intero'; break; case 'pt_BR': case 'pt_PT': case 'pt-BR': case 'pt-PT': case 'pt': defaultTranslationMap['web.mapbox_style_id'] = 'cjh42wvhm1biu2snvzhsp2ir3'; break; case 'ru_RU': case 'ru-RU': case 'ru': defaultTranslationMap['web.mapbox_style_id'] = 'ciscgwh8e002j2ymxlk7lla5o'; break; case 'zh_CN': case 'zh_HK': case 'zh_TW': case 'zh-CN': case 'zh-HK': case 'zh-TW': case 'zh': defaultTranslationMap['web.mapbox_style_id'] = 'ciscgwtli002f2xp80z4xv817'; break; default: } } function mToKm(m) { return m / 1000; } function mToFt(m) { return m / 0.3048; } function mToMi(m) { return m / 1609.344; } module.exports.createDistanceString = function (dist, useMiles) { if (!useMiles) { if (dist < 900) return mathTools.round(dist, 1) + ' ' + tr2("m_abbr"); dist = mathTools.round(mToKm(dist), 100); if (dist > 100) dist = mathTools.round(dist, 1); return dist + ' ' + tr2("km_abbr"); } else { if (dist < 152) return mathTools.round(mToFt(dist), 1) + ' ' + tr2("ft_abbr"); dist = mathTools.round(mToMi(dist), 100); if (dist > 100) dist = mathTools.round(dist, 1); return dist + ' ' + tr2("mi_abbr"); } }; module.exports.createEleInfoString = function (ascend, descend, useMiles) { var str = ""; if (ascend > 0 || descend > 0) { if (ascend > 0) { str += " "; if (!useMiles) str += mathTools.round(ascend, 1) + ' ' + tr2("m_abbr"); else str += mathTools.round(mToFt(ascend), 1) + ' ' + tr2("ft_abbr"); } if (descend > 0) { str += "
"; if (!useMiles) str += mathTools.round(descend, 1) + ' ' + tr2("m_abbr"); else str += mathTools.round(mToFt(descend), 1) + ' ' + tr2("ft_abbr"); } } return str; }; module.exports.createTimeString = function (time) { var tmpTime = mathTools.round(time / 60 / 1000, 1000); var resTimeStr; if (tmpTime > 60) { if (tmpTime / 60 > 24) { resTimeStr = mathTools.floor(tmpTime / 60 / 24, 1) + ' ' + tr2("day_abbr"); tmpTime = mathTools.floor(((tmpTime / 60) % 24), 1); if (tmpTime > 0) resTimeStr += ' ' + tmpTime + ' ' + tr2("hour_abbr"); } else { resTimeStr = mathTools.floor(tmpTime / 60, 1) + ' ' + tr2("hour_abbr"); tmpTime = mathTools.floor(tmpTime % 60, 1); if (tmpTime > 0) resTimeStr += ' ' + tmpTime + ' ' + tr2("min_abbr"); } } else resTimeStr = mathTools.round(tmpTime % 60, 1) + ' ' + tr2("min_abbr"); return resTimeStr; }; module.exports.tr = tr; module.exports.tr2 = tr2; module.exports.nanoTemplate = function (template, data) { return template.replace(/\{([\w\.]*)\}/g, function (str, key) { var keys = key.split("."), v = data[keys.shift()]; for (i = 0, l = keys.length; i < l; _i++) v = v[this]; return (typeof v !== "undefined" && v !== null) ? v : ""; }); }; module.exports.init = function (translations) { // Init language. // 1. Determined by Accept-Language header, falls back to 'en' if no translation map available // 2. Can be overwritten by url parameter defaultTranslationMap = translations["default"]; enTranslationMap = translations.en; enhanceTranslations(translations.locale); if (!defaultTranslationMap) defaultTranslationMap = enTranslationMap; i18nIsInitialized = true; initI18N(); }; module.exports.isI18nIsInitialized = function () { return i18nIsInitialized; };