chore: Integrate i18n changes from Leaflet PR

See https://github.com/Leaflet/Leaflet/pull/9281
This commit is contained in:
David Larlet 2024-02-27 14:55:55 -05:00 committed by Yohan Boniface
parent fa0208519e
commit a6a1959c09
4 changed files with 48 additions and 7 deletions

View file

@ -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,

View file

@ -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
}

View file

@ -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 }

View file

@ -2,6 +2,12 @@
<script type="module"
src="{% static 'umap/vendors/leaflet/leaflet-src.esm.js' %}"
defer></script>
<script type="module" src="{% static 'umap/js/modules/leaflet-configure.js' %}" defer></script>
{% if locale %}
{% with "umap/locale/"|add:locale|add:".js" as path %}
<script src="{% static path %}" defer></script>
{% endwith %}
{% endif %}
<script type="module" src="{% static 'umap/js/modules/global.js' %}" defer></script>
<script src="{% static 'umap/vendors/editable/Path.Drag.js' %}" defer></script>
<script src="{% static 'umap/vendors/editable/Leaflet.Editable.js' %}" defer></script>
@ -39,11 +45,7 @@
<script src="{% static 'umap/vendors/colorbrewer/colorbrewer.js' %}" defer></script>
<script src="{% static 'umap/vendors/simple-statistics/simple-statistics.min.js' %}"
defer></script>
{% if locale %}
{% with "umap/locale/"|add:locale|add:".js" as path %}
<script src="{% static path %}" defer></script>
{% endwith %}
{% endif %}
<script src="{% static 'umap/js/umap.core.js' %}" defer></script>
<script src="{% static 'umap/js/umap.autocomplete.js' %}" defer></script>
<script src="{% static 'umap/js/umap.popup.js' %}" defer></script>