Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 8, 2024
1 parent c3b9bcc commit 82540e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
const langs = options.definedLanguages;
if (langs && langs[0] && langs[0].lang == language.substring(0, 2).toLowerCase()) {
return new Promise((resolve, reject) => {
reject("Original language is not translatable");
resolve(undefined);
// reject("Original language is not translatable");
})
}
return new Promise(async (resolve) => {
Expand All @@ -90,12 +91,12 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
}

// Initialize cache per page if not exist yet
if (!window.translationCache[window.location.pathname]) {
if (!window.translationCache?.[window.location.pathname]) {
window.translationCache[window.location.pathname] = {};
}

// Initialize language cache if not exist yet
if (!window.translationCache[window.location.pathname][language]) {
if (!window.translationCache?.[window.location.pathname]?.[language]) {
window.translationCache[window.location.pathname][language] = {};
}

Expand All @@ -104,9 +105,9 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
// Check cache for each textNode
cleanTextNodes.forEach((node) => {
const text = node.textContent;
const cacheValues = Object.values(window.translationCache[window.location.pathname][language] || {});
const cacheValues = Object.values(window.translationCache?.[window.location.pathname]?.[language] || {});
if (
!window.translationCache[window.location.pathname][language][text] // check in key
!window.translationCache?.[window.location.pathname]?.[language]?.[text] // check in key
&& !cacheValues.includes(text) // check in value (to handle nodes that already translated)
) {
notInCache.push(text); // If not cached, add to notInCache array
Expand All @@ -132,21 +133,23 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {

notInCache.forEach((text, index) => {
// Cache the new translations
window.translationCache[window.location.pathname][language][text] = response[index] || cacheFromCloudFlare[text] || text;
if (window.translationCache?.[window.location.pathname]?.[language]) {
window.translationCache[window.location.pathname][language][text] = response[index] || cacheFromCloudFlare[text] || text;
}

// If the translation is not available, cache the original text
if (window.translationCache[window.location.pathname][language][text] == "weploy-untranslated") {
if (window.translationCache?.[window.location.pathname]?.[language]?.[text] == "weploy-untranslated") {
window.translationCache[window.location.pathname][language][text] = text;
}
});

// Update textNodes from the cache
cleanTextNodes.forEach((node) => {
const text = node.textContent;
if(window.translationCache[window.location.pathname][language][text]) {
if(window.translationCache?.[window.location.pathname]?.[language]?.[text]) {
// make sure text is still the same before replacing
if(node.textContent == text) {
node.textContent = window.translationCache[window.location.pathname][language][text];
node.textContent = window.translationCache?.[window.location.pathname]?.[language]?.[text];
}
}
});
Expand All @@ -162,8 +165,8 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
// If all translations are cached, directly update textNodes from cache
cleanTextNodes.forEach((node) => {
const text = node.textContent;
if(window.translationCache[window.location.pathname][language][text]) {
node.textContent = window.translationCache[window.location.pathname][language][text];
if(window.translationCache?.[window.location.pathname]?.[language]?.[text]) {
node.textContent = window.translationCache?.[window.location.pathname]?.[language]?.[text];
}
});

Expand Down Expand Up @@ -239,7 +242,7 @@ async function getTranslations(apiKey, optsArgs = {}) {

// save language to local storage & delay 1 second to wait google translate
await Promise.allSettled([
fetchLanguageList(apiKey),
optsArgs.originalLanguage ? getDefinedLanguages(optsArgs.originalLanguage, optsArgs.allowedLanguages) : fetchLanguageList(apiKey),
delay(1000)
]);

Expand Down
1 change: 1 addition & 0 deletions utils/languages/fetchLanguageList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async function fetchLanguageList(apiKey) {
const langs = options.definedLanguages;
if (langs && Array.isArray(langs) && langs.length) return langs;
if (window.weployError) return [];
return [];

const shouldCompressResponse = SHOULD_COMPRESS_PAYLOAD;
const headers = {
Expand Down

0 comments on commit 82540e8

Please sign in to comment.