Skip to content

Commit

Permalink
replace links
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 21, 2024
1 parent 9b45659 commit 6a88cae
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
const { getTranslations, isBrowser } = require("./index.js");

if (isBrowser()) {
function replaceLinks(lang) {
// Select all anchor tags
let anchors = document.querySelectorAll('a');

// Loop through all anchor tags
for (let i = 0; i < anchors.length; i++) {
let anchor = anchors[i];

// Check if the link is internal
if (anchor.hostname === window.location.hostname) {
// Create a new URL object
let url = new URL(anchor.href);

// Set the search parameter "lang" to lang
url.searchParams.set('lang', lang);

// Update the href of the anchor tag
anchor.href = url.href;
}
}
}

window.weployScriptTag = document.currentScript;

const translationCache = window.localStorage.getItem("translationCachePerPage");
Expand Down Expand Up @@ -61,6 +83,15 @@ if (isBrowser()) {
}

document.addEventListener("DOMContentLoaded", function() {

// replace links with lang (for SEO purposes)
const search = window.location.search;
const params = new URLSearchParams(search);
const paramsLang = params.get('lang');
if (paramsLang && (paramsLang != originalLang)) {
replaceLinks(paramsLang);
}

getTranslations(apiKey, options)
});
}

0 comments on commit 6a88cae

Please sign in to comment.