From a6a1959c09cbef8b59d0574cf86bbfed9132c8bd Mon Sep 17 00:00:00 2001 From: David Larlet Date: Tue, 27 Feb 2024 14:55:55 -0500 Subject: [PATCH] chore: Integrate i18n changes from Leaflet PR See https://github.com/Leaflet/Leaflet/pull/9281 --- umap/static/umap/js/modules/global.js | 2 -- umap/static/umap/js/modules/i18n.js | 35 +++++++++++++++++++ .../umap/js/modules/leaflet-configure.js | 6 ++++ umap/templates/umap/js.html | 12 ++++--- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 umap/static/umap/js/modules/i18n.js create mode 100644 umap/static/umap/js/modules/leaflet-configure.js diff --git a/umap/static/umap/js/modules/global.js b/umap/static/umap/js/modules/global.js index 2d579187..62f3cbbc 100644 --- a/umap/static/umap/js/modules/global.js +++ b/umap/static/umap/js/modules/global.js @@ -1,4 +1,3 @@ -import * as L from '../../vendors/leaflet/leaflet-src.esm.js' import URLs from './urls.js' import Browser from './browser.js' import * as Utils from './utils.js' @@ -8,7 +7,6 @@ import { Request, ServerRequest, RequestError, HTTPError, NOKError } from './req // For the not yet module-compatible JS out there. // Copy the leaflet module, it's expected by leaflet plugins to be writeable. -window.L = { ...L } window.U = { URLs, Request, diff --git a/umap/static/umap/js/modules/i18n.js b/umap/static/umap/js/modules/i18n.js new file mode 100644 index 00000000..d2b6b429 --- /dev/null +++ b/umap/static/umap/js/modules/i18n.js @@ -0,0 +1,35 @@ +// Comes from https://github.com/Leaflet/Leaflet/pull/9281 +import { Util } from '../../vendors/leaflet/leaflet-src.esm.js' + +const locales = {} + +// @property locale: String +// The current locale code, that will be used when translating strings. +export let locale = null + +// @function registerLocale(code: String, locale?: Object): String +// Define localized strings for a given locale, defined by `code`. +export function registerLocale(code, locale) { + locales[code] = Util.extend({}, locales[code], locale) +} +// @function setLocale(code: String): undefined +// Define or change the locale code to be used when translating strings. +export function setLocale(code) { + locale = code +} +// @function translate(string: String, data?: Object): String +// Actually try to translate the `string`, with optionnal variable passed in `data`. +export function translate(string, data) { + if (locale && locales[locale] && locales[locale][string] !== undefined) { + string = locales[locale][string] + } + try { + // Do not fail if some data is missing + // a bad translation should not break the app + string = Util.template(string, data) + } catch (err) { + console.error(err) + } + + return string +} diff --git a/umap/static/umap/js/modules/leaflet-configure.js b/umap/static/umap/js/modules/leaflet-configure.js new file mode 100644 index 00000000..9679f3f4 --- /dev/null +++ b/umap/static/umap/js/modules/leaflet-configure.js @@ -0,0 +1,6 @@ +import * as L from '../../vendors/leaflet/leaflet-src.esm.js' +// Comes from https://github.com/Leaflet/Leaflet/pull/9281 +// TODELETE once it's merged! +import * as i18n from './i18n.js' + +window.L = { ...L, ...i18n } diff --git a/umap/templates/umap/js.html b/umap/templates/umap/js.html index 95e4a675..bcc700f0 100644 --- a/umap/templates/umap/js.html +++ b/umap/templates/umap/js.html @@ -2,6 +2,12 @@ + +{% if locale %} + {% with "umap/locale/"|add:locale|add:".js" as path %} + + {% endwith %} +{% endif %} @@ -39,11 +45,7 @@ -{% if locale %} - {% with "umap/locale/"|add:locale|add:".js" as path %} - - {% endwith %} -{% endif %} +