Skip to content

Commit 0ad90db

Browse files
committed
gpqr-translate-qr-scanner.js: Added snippet to translate QR Scanner Interface.
1 parent 0df84b6 commit 0ad90db

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Gravity Perks // GP QR Code // Translate QR Scanner Interface to Spanish
3+
* https://gravitywiz.com/documentation/gravity-forms-qr-code/
4+
*
5+
* Instructions:
6+
*
7+
* 1. Install this snippet with our free Custom JavaScript plugin.
8+
* https://gravitywiz.com/gravity-forms-code-chest/
9+
*/
10+
( function( $ ) {
11+
12+
const translations = new Map([
13+
["Request Camera Permissions", "Solicitar permisos de cámara"],
14+
["Requesting camera permissions...", "Solicitando permisos de cámara..."],
15+
["No camera found", "No se encontró ninguna cámara"],
16+
["Stop Scanning", "Detener escaneo"],
17+
["Start Scanning", "Iniciar escaneo"],
18+
["Scan an Image File", "Escanear un archivo de imagen"],
19+
["Scan using camera directly", "Escanear usando la cámara directamente"],
20+
["Choose Image", "Elegir imagen"],
21+
["Choose Another", "Elegir otra"],
22+
["No image choosen", "Ninguna imagen seleccionada"],
23+
["Choose Image - No image choosen", "Elegir imagen - Ninguna imagen seleccionada"],
24+
["Or drop an image to scan", "O arrastra una imagen para escanear"],
25+
["Camera based scan", "Escaneo basado en cámara"],
26+
["File based scan", "Escaneo basado en archivo"],
27+
["Powered by ", "Desarrollado por "],
28+
["Report issues", "Informar de problemas"],
29+
["Camera access is only supported in secure context like https or localhost.", "El acceso a la cámara solo está disponible en un entorno seguro como HTTPS o localhost."]
30+
]);
31+
32+
/**
33+
* Translate text inside a node
34+
*/
35+
function translateNode( node ) {
36+
37+
// Text nodes
38+
if ( node.nodeType === Node.TEXT_NODE ) {
39+
const text = node.nodeValue.trim();
40+
if ( translations.has( text ) ) {
41+
node.nodeValue = node.nodeValue.replace( text, translations.get( text ) );
42+
}
43+
return;
44+
}
45+
46+
if ( node.nodeType !== Node.ELEMENT_NODE ) {
47+
return;
48+
}
49+
50+
// Translate common attributes
51+
["placeholder", "aria-label", "title", "value"].forEach( attr => {
52+
if ( node.hasAttribute && node.hasAttribute( attr ) ) {
53+
const value = node.getAttribute( attr );
54+
if ( translations.has( value ) ) {
55+
node.setAttribute( attr, translations.get( value ) );
56+
}
57+
}
58+
});
59+
60+
// Translate direct text content (buttons, spans, divs)
61+
if ( node.childNodes && node.childNodes.length ) {
62+
node.childNodes.forEach( child => translateNode( child ) );
63+
}
64+
}
65+
66+
/**
67+
* Run translation on existing DOM
68+
*/
69+
function translateExistingDOM() {
70+
translateNode( document.body );
71+
}
72+
73+
/**
74+
* Observe dynamically added elements
75+
*/
76+
function observeDOM() {
77+
const observer = new MutationObserver( mutations => {
78+
mutations.forEach( mutation => {
79+
mutation.addedNodes.forEach( node => translateNode( node ) );
80+
});
81+
});
82+
83+
observer.observe( document.body, {
84+
childList: true,
85+
subtree: true
86+
});
87+
}
88+
89+
/**
90+
* Init (works for Gravity Forms + dynamic popups)
91+
*/
92+
function initTranslator() {
93+
translateExistingDOM();
94+
observeDOM();
95+
}
96+
97+
initTranslator();
98+
99+
}( jQuery ));

0 commit comments

Comments
 (0)