From bdbdfebcc2e2c41a66b0e7645ec6f7f1d64c5d24 Mon Sep 17 00:00:00 2001 From: Vivien Ngo Date: Sun, 24 Sep 2017 01:42:28 -0400 Subject: [PATCH] Translate card content --- app.js | 3 +++ languages.js | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 5032f38..7391dd7 100644 --- a/app.js +++ b/app.js @@ -244,6 +244,9 @@ var getCards = function() { start( data.feed.entry.map(buildCard).filter(isApprovedCard) ); + + lang = languagesModule && languagesModule.getCurrentLang(); + if (lang && lang != 'es') { languagesModule.translateCards(lang) }; } ); } diff --git a/languages.js b/languages.js index 630da5f..f001597 100644 --- a/languages.js +++ b/languages.js @@ -137,6 +137,8 @@ var languagesModule = (function() { item.textContent = content; } }) + + translateCards(chosenLang); currentLang = chosenLang; updateHistory(); @@ -155,6 +157,22 @@ var languagesModule = (function() { function getCurrentLang() { return currentLang; } + + function translateCards(chosenLang) { + cardTextElements = document.querySelectorAll(".card__title, .card__desc, .badge-verified"); + // Doesn't select ".card__type span, .card__location span" to avoid breaking tag filters + translateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx" + + "&sl=es&tl=" + chosenLang + "&dt=t&q="; + cardTextElements.forEach(function (item) { + $.ajax({ + url: translateUrl + item.textContent, + success: function(data) { + item.textContent = data[0][0][0]; + } + }); + + }); + } // Copied from app.js, but without card filtering function updateHistory() { @@ -177,6 +195,7 @@ var languagesModule = (function() { return { loadLang: publicLoadLanguage, toggleLang: toggleLang, - getCurrentLang: getCurrentLang + getCurrentLang: getCurrentLang, + translateCards: translateCards } })();