about summary refs log tree commit diff stats
path: root/src/assets/javascripts/localise.js
blob: c0936873839fd607753892db4e1ef5c71461cdf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
window.browser = window.browser || window.chrome

function localisePage() {
	/**
	 * @param {string} tag
	 */
	function getMessage(tag) {
		return tag.replace(/__MSG_(\w+)__/g, (_match, v1) => {
			return v1 ? browser.i18n.getMessage(v1) : null
		})
	}

	const elements = document.querySelectorAll("[data-localise]")
	for (let i in elements)
		if (elements.hasOwnProperty(i)) {
			const obj = elements[i]
			const tag = obj.getAttribute("data-localise").toString()
			const msg = getMessage(tag)
			if (msg && msg !== tag) obj.textContent = msg
		}

	const placeholders = document.querySelectorAll("[data-localise-placeholder]")
	for (let i in placeholders)
		if (placeholders.hasOwnProperty(i)) {
			const obj = placeholders[i]
			const tag = obj.getAttribute("data-localise-placeholder").toString()
			const msg = getMessage(tag)
			if (msg && msg !== tag) obj.placeholder = msg
		}
}

export default {
	localisePage,
}