From: Tom Hughes Date: Sun, 16 Mar 2025 16:16:22 +0000 (+0000) Subject: Replace use of I18n in javascript with OSM.i18n X-Git-Tag: live~2^2~1 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/e2e61692ea04fc156221082aeddc21ea18facdec?ds=inline;hp=-c Replace use of I18n in javascript with OSM.i18n --- e2e61692ea04fc156221082aeddc21ea18facdec diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index c01a0f14b..5c61b01cc 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -21,13 +21,13 @@ OSM.i18n = I18n; - I18n.default_locale = OSM.DEFAULT_LOCALE; - I18n.locale = locale; + OSM.i18n.default_locale = OSM.DEFAULT_LOCALE; + OSM.i18n.locale = locale; // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]]; if (pluralizer) { - I18n.pluralization[locale] = (count) => [pluralizer(count), "other"]; + OSM.i18n.pluralization[locale] = (count) => [pluralizer(count), "other"]; } OSM.preferred_editor = application_data.preferredEditor; @@ -199,5 +199,5 @@ $(function () { }); $("#edit_tab") - .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip")); + .attr("title", OSM.i18n.t("javascripts.site.edit_disabled_tooltip")); }); diff --git a/app/assets/javascripts/diary_entry.js b/app/assets/javascripts/diary_entry.js index a87eb35f1..bfc3fc012 100644 --- a/app/assets/javascripts/diary_entry.js +++ b/app/assets/javascripts/diary_entry.js @@ -12,7 +12,7 @@ $(function () { } marker = L.marker(e.latlng, { icon: OSM.getUserIcon() }).addTo(map) - .bindPopup(I18n.t("diary_entries.edit.marker_text")); + .bindPopup(OSM.i18n.t("diary_entries.edit.marker_text")); } $("#usemap").click(function (e) { @@ -37,7 +37,7 @@ $(function () { if ($("#latitude").val() && $("#longitude").val()) { marker = L.marker(centre, { icon: OSM.getUserIcon() }).addTo(map) - .bindPopup(I18n.t("diary_entries.edit.marker_text")); + .bindPopup(OSM.i18n.t("diary_entries.edit.marker_text")); } map.on("click", setLocation); diff --git a/app/assets/javascripts/edit/id.js.erb b/app/assets/javascripts/edit/id.js.erb index b85dbeddb..7f111b41e 100644 --- a/app/assets/javascripts/edit/id.js.erb +++ b/app/assets/javascripts/edit/id.js.erb @@ -4,7 +4,7 @@ $(function () { if (!idData.configured) { // eslint-disable-next-line no-alert - alert(I18n.t("site.edit.id_not_configured")); + alert(OSM.i18n.t("site.edit.id_not_configured")); return; } diff --git a/app/assets/javascripts/embed.js.erb b/app/assets/javascripts/embed.js.erb index 899566d39..297259043 100644 --- a/app/assets/javascripts/embed.js.erb +++ b/app/assets/javascripts/embed.js.erb @@ -9,13 +9,13 @@ const OSM = { i18n: I18n }; if (navigator.languages) { - I18n.locale = navigator.languages[0]; + OSM.i18n.locale = navigator.languages[0]; } else if (navigator.language) { - I18n.locale = navigator.language; + OSM.i18n.locale = navigator.language; } -I18n.default_locale = <%= I18n.default_locale.to_json %>; -I18n.fallbacks = true; +OSM.i18n.default_locale = <%= I18n.default_locale.to_json %>; +OSM.i18n.fallbacks = true; window.onload = function () { const args = Object.fromEntries(new URLSearchParams(location.search)); @@ -58,7 +58,7 @@ window.onload = function () { L.Control.OSMReportAProblem = L.Control.Attribution.extend({ options: { position: "bottomright", - prefix: `${I18n.t("javascripts.embed.report_problem")}` + prefix: `${OSM.i18n.t("javascripts.embed.report_problem")}` }, onAdd: function (map) { diff --git a/app/assets/javascripts/heatmap.js b/app/assets/javascripts/heatmap.js index 0670480d3..fccc9a7b0 100644 --- a/app/assets/javascripts/heatmap.js +++ b/app/assets/javascripts/heatmap.js @@ -16,7 +16,7 @@ document.addEventListener("DOMContentLoaded", () => { const colorScheme = document.documentElement.getAttribute("data-bs-theme") ?? "auto"; const rangeColors = ["#14432a", "#166b34", "#37a446", "#4dd05a"]; const startDate = new Date(Date.now() - (365 * 24 * 60 * 60 * 1000)); - const monthNames = I18n.t("date.abbr_month_names"); + const monthNames = OSM.i18n.t("date.abbr_month_names"); const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); @@ -86,13 +86,13 @@ document.addEventListener("DOMContentLoaded", () => { } function getTooltipText(date, value) { - const localizedDate = I18n.l("date.formats.long", date); + const localizedDate = OSM.i18n.l("date.formats.long", date); if (value > 0) { - return I18n.t("javascripts.heatmap.tooltip.contributions", { count: value, date: localizedDate }); + return OSM.i18n.t("javascripts.heatmap.tooltip.contributions", { count: value, date: localizedDate }); } - return I18n.t("javascripts.heatmap.tooltip.no_contributions", { date: localizedDate }); + return OSM.i18n.t("javascripts.heatmap.tooltip.no_contributions", { date: localizedDate }); } function getTheme() { diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 9220e1205..3924316c1 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -245,7 +245,7 @@ $(function () { }) .catch(() => { // eslint-disable-next-line no-alert - alert(I18n.t("site.index.remote_failed")); + alert(OSM.i18n.t("site.index.remote_failed")); }); function sendRemoteEditCommand(url) { @@ -266,7 +266,7 @@ $(function () { .removeAttr("title") .tooltip({ placement: "bottom", - title: I18n.t("javascripts.edit_help") + title: OSM.i18n.t("javascripts.edit_help") }) .tooltip("show"); @@ -280,7 +280,7 @@ $(function () { page.pushstate = page.popstate = function () { map.setSidebarOverlaid(true); - document.title = I18n.t("layouts.project_name.title"); + document.title = OSM.i18n.t("layouts.project_name.title"); }; page.load = function () { diff --git a/app/assets/javascripts/index/contextmenu.js b/app/assets/javascripts/index/contextmenu.js index 6f85a5252..4c157e6e0 100644 --- a/app/assets/javascripts/index/contextmenu.js +++ b/app/assets/javascripts/index/contextmenu.js @@ -1,6 +1,6 @@ OSM.initializeContextMenu = function (map) { map.contextmenu.addItem({ - text: I18n.t("javascripts.context.directions_from"), + text: OSM.i18n.t("javascripts.context.directions_from"), callback: function directionsFromHere(e) { const latlng = OSM.cropLocation(e.latlng, map.getZoom()); @@ -12,7 +12,7 @@ OSM.initializeContextMenu = function (map) { }); map.contextmenu.addItem({ - text: I18n.t("javascripts.context.directions_to"), + text: OSM.i18n.t("javascripts.context.directions_to"), callback: function directionsToHere(e) { const latlng = OSM.cropLocation(e.latlng, map.getZoom()); @@ -24,7 +24,7 @@ OSM.initializeContextMenu = function (map) { }); map.contextmenu.addItem({ - text: I18n.t("javascripts.context.add_note"), + text: OSM.i18n.t("javascripts.context.add_note"), callback: function addNoteHere(e) { const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom()); @@ -33,7 +33,7 @@ OSM.initializeContextMenu = function (map) { }); map.contextmenu.addItem({ - text: I18n.t("javascripts.context.show_address"), + text: OSM.i18n.t("javascripts.context.show_address"), callback: function describeLocation(e) { const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom()); @@ -42,7 +42,7 @@ OSM.initializeContextMenu = function (map) { }); map.contextmenu.addItem({ - text: I18n.t("javascripts.context.query_features"), + text: OSM.i18n.t("javascripts.context.query_features"), callback: function queryFeatures(e) { const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom()); @@ -51,7 +51,7 @@ OSM.initializeContextMenu = function (map) { }); map.contextmenu.addItem({ - text: I18n.t("javascripts.context.centre_map"), + text: OSM.i18n.t("javascripts.context.centre_map"), callback: function centreMap(e) { map.panTo(e.latlng); } diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index e3f6afa46..a4b3a928b 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -117,7 +117,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch if (json.length === 0) { input.addClass("is-invalid"); // eslint-disable-next-line no-alert - alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value })); + alert(OSM.i18n.t("javascripts.directions.errors.no_place", { place: endpoint.value })); return; } diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index d2741d02b..057933453 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -72,11 +72,11 @@ OSM.Directions = function (map) { function formatTotalDistance(m) { if (m < 1000) { - return I18n.t("javascripts.directions.distance_m", { distance: Math.round(m) }); + return OSM.i18n.t("javascripts.directions.distance_m", { distance: Math.round(m) }); } else if (m < 10000) { - return I18n.t("javascripts.directions.distance_km", { distance: (m / 1000.0).toFixed(1) }); + return OSM.i18n.t("javascripts.directions.distance_km", { distance: (m / 1000.0).toFixed(1) }); } else { - return I18n.t("javascripts.directions.distance_km", { distance: Math.round(m / 1000) }); + return OSM.i18n.t("javascripts.directions.distance_km", { distance: Math.round(m / 1000) }); } } @@ -84,18 +84,18 @@ OSM.Directions = function (map) { if (m < 5) { return ""; } else if (m < 200) { - return I18n.t("javascripts.directions.distance_m", { distance: String(Math.round(m / 10) * 10) }); + return OSM.i18n.t("javascripts.directions.distance_m", { distance: String(Math.round(m / 10) * 10) }); } else if (m < 1500) { - return I18n.t("javascripts.directions.distance_m", { distance: String(Math.round(m / 100) * 100) }); + return OSM.i18n.t("javascripts.directions.distance_m", { distance: String(Math.round(m / 100) * 100) }); } else if (m < 5000) { - return I18n.t("javascripts.directions.distance_km", { distance: String(Math.round(m / 100) / 10) }); + return OSM.i18n.t("javascripts.directions.distance_km", { distance: String(Math.round(m / 100) / 10) }); } else { - return I18n.t("javascripts.directions.distance_km", { distance: String(Math.round(m / 1000)) }); + return OSM.i18n.t("javascripts.directions.distance_km", { distance: String(Math.round(m / 1000)) }); } } function formatHeight(m) { - return I18n.t("javascripts.directions.distance_m", { distance: Math.round(m) }); + return OSM.i18n.t("javascripts.directions.distance_m", { distance: Math.round(m) }); } function formatTime(s) { @@ -164,13 +164,13 @@ OSM.Directions = function (map) { } const distanceText = $("

").append( - I18n.t("javascripts.directions.distance") + ": " + formatTotalDistance(route.distance) + ". " + - I18n.t("javascripts.directions.time") + ": " + formatTime(route.time) + "."); + OSM.i18n.t("javascripts.directions.distance") + ": " + formatTotalDistance(route.distance) + ". " + + OSM.i18n.t("javascripts.directions.time") + ": " + formatTime(route.time) + "."); if (typeof route.ascend !== "undefined" && typeof route.descend !== "undefined") { distanceText.append( $("
"), - I18n.t("javascripts.directions.ascend") + ": " + formatHeight(route.ascend) + ". " + - I18n.t("javascripts.directions.descend") + ": " + formatHeight(route.descend) + "."); + OSM.i18n.t("javascripts.directions.ascend") + ": " + formatHeight(route.ascend) + ". " + + OSM.i18n.t("javascripts.directions.descend") + ": " + formatHeight(route.descend) + "."); } const turnByTurnTable = $("") @@ -219,18 +219,18 @@ OSM.Directions = function (map) { downloadURL = URL.createObjectURL(blob); $("#directions_content").append(`

${ - I18n.t("javascripts.directions.download") + OSM.i18n.t("javascripts.directions.download") }

`); $("#directions_content").append("

" + - I18n.t("javascripts.directions.instructions.courtesy", { link: chosenEngine.creditline }) + + OSM.i18n.t("javascripts.directions.instructions.courtesy", { link: chosenEngine.creditline }) + "

"); }).catch(function () { map.removeLayer(polyline); if (reportErrors) { - $("#directions_content").html("
" + I18n.t("javascripts.directions.errors.no_route") + "
"); + $("#directions_content").html("
" + OSM.i18n.t("javascripts.directions.errors.no_route") + "
"); } }).finally(function () { controller = null; diff --git a/app/assets/javascripts/index/directions/fossgis_osrm.js b/app/assets/javascripts/index/directions/fossgis_osrm.js index 2fb835453..b2586e68e 100644 --- a/app/assets/javascripts/index/directions/fossgis_osrm.js +++ b/app/assets/javascripts/index/directions/fossgis_osrm.js @@ -109,21 +109,21 @@ } else if (step.ref) { name = "" + step.ref + ""; } else { - name = I18n.t(instrPrefix + "unnamed"); + name = OSM.i18n.t(instrPrefix + "unnamed"); namedRoad = false; } if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) { - instText += I18n.t(template, { name: name }); + instText += OSM.i18n.t(template, { name: name }); } else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) { if (step.maneuver.exit) { if (step.maneuver.exit <= 10) { - instText += I18n.t(template + "_with_exit_ordinal", { exit: I18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name }); + instText += OSM.i18n.t(template + "_with_exit_ordinal", { exit: OSM.i18n.t(instrPrefix + "exit_counts." + numToWord(step.maneuver.exit)), name: name }); } else { - instText += I18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name }); + instText += OSM.i18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name }); } } else { - instText += I18n.t(template + "_without_exit", { name: name }); + instText += OSM.i18n.t(template + "_without_exit", { name: name }); } } else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) { const params = {}; @@ -133,9 +133,9 @@ if (Object.keys(params).length > 0) { template = template + "_with_" + Object.keys(params).join("_"); } - instText += I18n.t(template, params); + instText += OSM.i18n.t(template, params); } else { - instText += I18n.t(template + "_without_exit", { name: name }); + instText += OSM.i18n.t(template + "_without_exit", { name: name }); } return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry]; }) diff --git a/app/assets/javascripts/index/directions/fossgis_valhalla.js b/app/assets/javascripts/index/directions/fossgis_valhalla.js index 8bfb1908f..13fc020c2 100644 --- a/app/assets/javascripts/index/directions/fossgis_valhalla.js +++ b/app/assets/javascripts/index/directions/fossgis_valhalla.js @@ -97,7 +97,7 @@ costing: costing, directions_options: { units: "km", - language: I18n.currentLocale() + language: OSM.i18n.currentLocale() } }) }); diff --git a/app/assets/javascripts/index/directions/graphhopper.js b/app/assets/javascripts/index/directions/graphhopper.js index 14b721d23..6a1043fbf 100644 --- a/app/assets/javascripts/index/directions/graphhopper.js +++ b/app/assets/javascripts/index/directions/graphhopper.js @@ -57,7 +57,7 @@ // https://graphhopper.com/api/1/docs/routing/ const query = new URLSearchParams({ vehicle: vehicleType, - locale: I18n.currentLocale(), + locale: OSM.i18n.currentLocale(), key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn", elevation: false, instructions: true, diff --git a/app/assets/javascripts/index/home.js b/app/assets/javascripts/index/home.js index 7e297b724..597b68eff 100644 --- a/app/assets/javascripts/index/home.js +++ b/app/assets/javascripts/index/home.js @@ -18,12 +18,12 @@ OSM.Home = function (map) { }); marker = L.marker(OSM.home, { icon: OSM.getUserIcon(), - title: I18n.t("javascripts.home.marker_title") + title: OSM.i18n.t("javascripts.home.marker_title") }).addTo(map); } else { $("#browse_status").html( $("
").text( - I18n.t("javascripts.home.not_set") + OSM.i18n.t("javascripts.home.not_set") ) ); } diff --git a/app/assets/javascripts/index/layers/data.js b/app/assets/javascripts/index/layers/data.js index c0fffd72d..6452c2a41 100644 --- a/app/assets/javascripts/index/layers/data.js +++ b/app/assets/javascripts/index/layers/data.js @@ -51,15 +51,15 @@ OSM.initializeDataLayer = function (map) { $("
").append( $("
").append( $("

") - .text(I18n.t("browse.start_rjs.load_data")), + .text(OSM.i18n.t("browse.start_rjs.load_data")), $("
").append( $("