Write a JavaScript code that fetches HTML lang attribute and localizes texts by given…
- User
Write a JavaScript code that fetches HTML lang attribute and localizes texts by given strings:
For example:
- Assistant
Certainly! Below is a JavaScript code snippet that fetches the
langattribute from the HTML<html>element and localizes the text content and attributes of your elements based on predefined translations.First, we'll define an object that holds the translations for each language:
var translations = { "en": { "addto": { "title": "Add to", "data-tooltip-text": "Add to", "span": "Add to" } }, "pl-PL": { "addto": { "title": "Dodaj do", "data-tooltip-text": "Dodaj do", "span": "Dodaj do" } } };Next, we'll fetch the
langattribute, and use it to select the appropriate translations. We'll then update the elements accordingly:(function() { // Fetch the lang attribute from the <html> tag var lang = document.documentElement.lang || 'en'; // default to 'en' if not specified // Get the translations for the current language var langTranslations = translations[lang]; // If translations for the current language do not exist, default to 'en' if (!langTranslations) { langTranslations = translations['en']; } // Get the element to be localized by its ID var addtoElement = document.getElementById('addto'); if (addtoElement && langTranslations['addto']) { var addtoTranslations = langTranslations['addto']; // Update the title attribute if (addtoTranslations['title']) { addtoElement.title = addtoTranslations['title']; } // Update the data-tooltip-text attribute if (addtoTranslations['data-tooltip-text']) { addtoElement.setAttribute('data-tooltip-text', addtoTranslations['data-tooltip-text']); } // Update the text content inside the <span> element if (addtoTranslations['span']) { var spanElement = addtoElement.getElementsByTagName('span')[0]; if (spanElement) { spanElement.textContent = addtoTranslations['span']; } } } })();Explanation:
- We use an Immediately Invoked Function Expression (IIFE) to avoid polluting the global namespace.
- The
translationsobject holds all the necessary translated strings organized by language code and element ID. - We obtain the language code from the
<html>element'slangattribute. - If the language code is not supported, we default to English (
'en'). - We select the element with
id="addto"and update its attributes and inner text based on the translations for the current language.
Usage:
Place this script at the end of your HTML body or in an external JavaScript file included at the end of the body to ensure the DOM elements are loaded before the script runs.
Example HTML:
<!DOCTYPE html> <html lang="pl-PL"> <head> <meta charset="UTF-8"> <title>Localized Page</title> </head> <body> <div id="addto" class="watch8-action watch8-button yt-uix-tooltip" title="" data-tooltip-text=""> <span></span> </div> <!-- Include the localization script --> <script src="path/to/your/localization-script.js"></script> </body> </html>When the page loads, the script will automatically set the appropriate texts based on the
langattribute in the<html>tag.Note:
- This code assumes that the element you want to localize has a unique
id. If you have multiple elements or need a more scalable solution, consider using data attributes to mark elements for localization and loop through them accordingly. - Always ensure your translations are properly maintained and secure, particularly if they are user-generated or stored externally.