(function() {
// Fetch the lang attribute from the HTML tag
const langCode = document.documentElement.lang.split('-')[0] || 'en'; // Default to 'en' if not set
// Localization data for supported languages
const localization = {
"en": {
"addTo": "Add to",
// Add other strings here...
},
"pl": {
"addTo": "Dodaj do",
// Add other strings here...
},
// ... add more languages as needed
};
// StarTube Language Model class
class StarTubeLanguageModel {
constructor(lang) {
this.lang = lang;
// Use the localization for the specified language, or default to English
this.strings = localization[this.lang] || localization['en'];
}
// Getter for 'addTo' string
get addTo() {
return this.strings.addTo;
}
// Add getters for other strings as needed...
}
// Instantiate the language model with the current language code
const startubeLanguageModel = new StarTubeLanguageModel(langCode);
// Localize the 'Add to' button
const addToElement = document.getElementById('addto');
if (addToElement) {
// Set the 'title' attribute
addToElement.setAttribute('title', startubeLanguageModel.addTo);
// Set the 'data-tooltip-text' attribute
addToElement.setAttribute('data-tooltip-text', startubeLanguageModel.addTo);
// Set the text content inside the span
const span = addToElement.querySelector('span');
if (span) {
span.textContent = startubeLanguageModel.addTo;
}
}
// Localize other elements as needed...
})();
Explanation:
-
Fetching the Language Code:
- We get the
lang attribute from the HTML tag using document.documentElement.lang.
- We split the language code at the hyphen to handle cases like
en-US or pl-PL, taking the first part ('en' or 'pl').
-
Localization Data:
- We define an object
localization that holds the translated strings for each supported language.
- Each language code maps to an object containing the localized strings.
-
Creating the Language Model Class:
- We define a
StarTubeLanguageModel class.
- The constructor accepts a language code and sets up the
strings property with the appropriate translations.
- We use getters to access the localized strings (e.g.,
get addTo()).
-
Instantiating the Language Model:
- We create an instance of
StarTubeLanguageModel using the fetched language code.
-
Updating the DOM Elements:
- We find the element with
id="addto" and update its attributes (title, data-tooltip-text) and the text inside its <span> to the localized string.
- You can repeat this process for other elements that need localization.
Usage in HTML:
Your HTML can remain the same:
<div id="addto" class="watch8-action watch8-button yt-uix-tooltip">
<span></span>
</div>
Notes:
- This script should be included after the HTML elements it modifies, or wrapped in a
DOMContentLoaded event listener to ensure elements are available when the script runs.
- You can extend the
localization object and the StarTubeLanguageModel getters to include more strings for localization.
- Ensure that your HTML doesn't hard-code the text that will be replaced; the script will handle inserting the correct localized text.
Example with Additional Strings:
If you have more strings to localize:
const localization = {
"en": {
"addTo": "Add to",
"subscribe": "Subscribe",
// Other strings...
},
"pl": {
"addTo": "Dodaj do",
"subscribe": "Subskrybuj",
// Other strings...
},
// ... other languages
};
class StarTubeLanguageModel {
// ... existing code ...
get subscribe() {
return this.strings.subscribe;
}
// ... other getters ...
}
// Update elements accordingly
const subscribeButton = document.getElementById('subscribe-button');
if (subscribeButton) {
subscribeButton.textContent = startubeLanguageModel.subscribe;
}
By following this pattern, you can systematically localize your web application's interface based on the user's language settings.