From 2ae58f43135c76ecf9c2202ea191e6f0858d9d58 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 1 Aug 2020 14:12:27 -0300 Subject: [PATCH] traducir en cualquier evento --- public/client_site_i18n.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/public/client_site_i18n.js b/public/client_site_i18n.js index 5f6beb90..c24b918b 100644 --- a/public/client_site_i18n.js +++ b/public/client_site_i18n.js @@ -1,19 +1,15 @@ -document.addEventListener('DOMContentLoaded', function() { - const available_locales = ["es","en"]; +const clientSideI18n = () => { + const available_locales = ['es','en'] + const current_locale = navigator.languages.map(l => l.split('-')[0]).find(l => available_locales.includes(l)) - let current_locale = navigator.languages.map(function(language) { - language.split('-')[0]; - }).find(function(locale) { - return available_locales.includes(available_locales); - }); + if (!current_locale) current_locale = available_locales[0] - if (current_locale == undefined) current_locale = available_locales[0]; + available_locales.forEach(locale => { + if (locale == current_locale) return - available_locales.forEach(function(locale) { - if (locale == current_locale) return; + document.querySelectorAll(`[lang=${locale}]`).forEach(el => el.remove()) + }) +} - document.querySelectorAll('[lang='+locale+']').forEach(function(el) { - el.parentNode.removeChild(el); - }); - }); -}); +document.addEventListener('DOMContentLoaded', clientSideI18n) +document.addEventListener('turbolinks:load', clientSideI18n)