Madhurima Mindscape

Modules, Materials, Moments, Memories, and More

🏡 Home 📑 Saved 📂 Blogs

Automatic Website Translation with translate.js

November 26, 2026

Calculating reading time...

Creating a multilingual website can make content accessible to visitors from different parts of the world. However, implementing internationalization manually often requires maintaining language files, configuring translations, and writing additional JavaScript to switch between languages.

Fortunately, JavaScript libraries such as translate.js simplify this process by providing automatic webpage translation without requiring developers to rewrite their existing HTML content.

In this tutorial, we will explore translate.js, an open-source JavaScript library developed by xnx3. It provides automatic HTML translation with a minimal JavaScript setup and offers numerous customization options for controlling translation behavior.

We will also explore how I integrated this library into my personal blog website, Madhurima Mindscape, using a floating language button, a searchable language dropdown, and localStorage to remember visitors' language preferences.

You can also try the live implementation right here on this website! Simply click the floating language button to open the language panel, then expand the language dropdown to browse or search for your preferred language. This lets you experience how translate.js works in practice and see how easily it can translate the webpage content.

GitHub Repository: xnx3/translate

Official Documentation: translate.js Documentation

"A multilingual website should make content accessible without making the translation implementation unnecessarily complicated."

🌐 1. What is translate.js?

translate.js is an open-source JavaScript library designed to provide automatic multilingual translation for websites.

Its main objective is to simplify website internationalization by allowing developers to translate webpage content with a minimal amount of JavaScript code.

According to its GitHub repository, the library provides several useful capabilities:

  • Automatic HTML content translation.
  • Language detection and language switching.
  • Translation without manually modifying existing page content.
  • Custom translation terminology.
  • Dynamic webpage content monitoring.
  • Translation service customization.
  • Browser-side translation caching.
  • SEO-oriented translation functionality.

One of its biggest advantages is that developers can get started with only a few JavaScript instructions instead of building an entire translation system from scratch.

The library also provides an extensive collection of extension instructions for more precise control over translation behavior.

💡 My tip: If you want to introduce multilingual support into an existing static HTML website, explore translate.js before building a complete translation system manually.

📌 2. Why Use translate.js?

Traditional internationalization implementations often require developers to maintain separate translations for different languages and manually connect those translations with webpage elements.

translate.js offers an alternative approach by automatically identifying and translating webpage text through its translation service.

Some important advantages include:

  • Minimal JavaScript: Basic translation can be initialized with just a few configuration instructions.
  • No Manual HTML Replacement: Existing webpage content does not need to be rewritten into separate language templates.
  • Multiple Languages: Visitors can select from the languages supported by the configured translation service.
  • Custom Translation Rules: Developers can define terminology, ignore specific content, and customize translation behavior.
  • Dynamic Content Support: The library can monitor DOM changes and translate dynamically rendered content.
  • Flexible Integration: It can be integrated into existing HTML, CSS, and JavaScript projects.

The library also supports different translation service configurations. For example, client.edge can be selected without configuring your own translation server.

💡 My tip: Choose the translation service according to your application's requirements. For production websites, also evaluate service availability, reliability, privacy, and usage limits.

🛠️ 3. Setting Up translate.js

Before implementing translation, we need to load the translate.js library into our webpage.

Step 1: Include the JavaScript Library

Add the following script to your HTML page. It can be placed near the end of the body section.

<script src="https://res.zvo.cn/translate/translate.js"></script>

Alternatively, you can download the library from its GitHub repository and host the file within your own project.

Step 2: Configure the Original Language

Specify the original language used by your website. In my personal blog, the original content is written in English.

translate.language.setLocal('english');

This tells the library which language should be treated as the local language of the webpage.

Step 3: Select a Translation Service

Next, configure the translation service. In my implementation, I used the client.edge translation service.

translate.service.use('client.edge');

Step 4: Execute Translation

Finally, initialize the translation process:

translate.execute();

The basic configuration can therefore be written as follows:

<script src="https://res.zvo.cn/translate/translate.js"></script>

<script>
    translate.language.setLocal('english');

    translate.service.use('client.edge');

    translate.execute();
</script>

These instructions provide the foundation for translating webpage content. Additional configuration can be added before calling translate.execute().

Official reference: translate.js GitHub Repository

⚙️ 4. Fine-Tuning and Advanced Customization

One of the most useful aspects of translate.js is its extensive collection of fine-tuning instructions.

These instructions allow developers to control different parts of the translation process, customize the language selector, define translation rules, monitor dynamic content, and integrate custom translation functionality.

Instead of relying only on the default translation behavior, we can configure the library according to the requirements of our website.

Language Selection and Configuration

  • translate.selectLanguageTag.xxx: Customize the language selection dropdown, including its appearance, available languages, and display behavior.
  • translate.changeLanguage('english'): Switch the displayed webpage language manually.
  • translate.language.setLocal('english'): Specify the original language of the webpage.
  • translate.language.setDefaultTo('english'): Set the default target translation language.
  • translate.setAutoDiscriminateLocalLanguage(): Automatically identify and switch according to the language used by the visitor.
  • translate.language.getCurrent(): Retrieve the language currently displayed.
  • translate.language.getLocal(): Retrieve the original webpage language.
  • translate.language.translateLanguagesRange = [xxx]: Restrict translation to a specified range of languages.
  • translate.language.setUrlParamControl(): Control the displayed language through URL parameters.

Element and Content Control

  • translate.setDocuments(documents): Specify which webpage elements should be translated.
  • translate.execute(eles): Execute translation for specified elements.
  • translate.reset(): Restore translated webpage content to its original state.
  • translate.ignore.text.push('你好'): Exclude specific text from translation.
  • translate.ignore.id.push('test'): Ignore elements with a specified ID.
  • translate.ignore.class.push('test'): Ignore elements with a specified CSS class.
  • translate.ignore.tag.push('span'): Exclude specified HTML tags from translation.
  • translate.element.tagAttribute['input'] = ['value', 'data-value']: Configure additional HTML attributes that should be translated.
  • translate.whole.enableAll(): Enable whole-element translation to help maintain semantic fluency.

Translation Terminology and Manual Operations

  • translate.nomenclature.append(...): Define custom terminology and preferred translation results.
  • translate.request.translateText(...): Manually request translation of text through the translation interface.
  • translate.language.recognition('xxx'): Identify and analyze the language of a string.
  • translate.language.textTranslateReplace(...): Translate and replace specific words within a sentence.
  • translate.js.transObject(...): Translate JavaScript objects and code-related content.
  • translate.images.add(...): Add images to the image translation process.
  • translate.offline.append(...): Configure offline translation data and automatically generated translation settings.

Dynamic Content and Event Monitoring

  • translate.listener.start(): Monitor dynamically rendered webpage content and automatically translate detected changes.
  • translate.request.listener.start(): Monitor AJAX requests and trigger translation when requests complete.
  • translate.network.use(): Enable network request interception and translation.
  • translate.listener.renderTaskFinish = function() {}: Execute custom functionality after translation tasks finish rendering.
  • translate.lifecycle.execute.xxx.push(...): Register custom lifecycle actions during translation.
  • translate.listener.execute.xxx.push(...): Customize event execution during translation.

Translation Service and Network Configuration

  • translate.service.use('client.edge'): Select the translation service.
  • translate.request.setHost(...): Specify a custom translation service domain.
  • translate.request.api.translate = 'xxx': Customize the translation API endpoint.
  • translate.request.appendParams = ...: Add custom parameters to translation requests.
  • translate.request.response: Customize translation API response processing.
  • translate.request.sse.start(): Enable Server-Sent Events support for the translation service.

Cache, Performance, and Other Controls

  • translate.storage = ...: Customize the browser-side translation cache.
  • translate.language.clearCacheLanguage(): Clear previously stored translation language information.
  • translate.language.translateLocal = true: Force translation of content written in the local language.
  • translate.language.getTranslateAreaText(): Retrieve original text from the translation area.
  • translate.language.recognitionAlgorithm: Customize the language identification strategy.
  • translate.progress.api.startUITip(): Display a translation progress interface.
  • translate.visual.webPageLoadTranslateBeforeHiddenText(): Hide original text temporarily until translation is completed.
  • translate.element.iframe.use(...): Configure translation functionality for iframe content.
  • translate.selectionTranslate.start(): Enable mouse-based word selection and translation.
  • translate.time.execute.start(): Enable translation performance monitoring.
  • translate.debug: Enable debugging and diagnostic functionality.
  • translate.isTranslateExecute(): Check whether translation has already been executed.

These are selected instructions from the library's extension capabilities. Their exact arguments and configuration requirements should be checked in the official documentation before implementation.

For the complete list of customization options, visit: Official translate.js Documentation .

💡 My tip: You do not need to configure every available instruction. Start with basic translation and introduce additional controls only when your website requires them.

💻 5. My Practical Use Case: Adding a Floating Language Selector to My Personal Blog

After exploring the library, I decided to use translate.js in my personal blog website, Madhurima Mindscape.

My goal was to provide visitors with an easy way to translate blog content without changing the existing page layout or introducing a complicated language configuration system.

Instead of displaying a traditional language selection dropdown permanently on the page, I implemented a floating language button positioned at the bottom-right corner.

When visitors click this button, a language selection panel appears. The panel contains a custom searchable dropdown where visitors can search for languages using their English names or native language names.

The implementation includes:

  • A floating circular language button.
  • A Font Awesome language icon.
  • A custom language selection panel.
  • A searchable dropdown with English and native language names.
  • Alphabetically sorted language options.
  • A scrollable language list.
  • A hidden native select element for translate.js integration.
  • Selected language highlighting.
  • LocalStorage-based language preference persistence.
  • Automatic restoration of the previously selected language.
  • Responsive styling for mobile devices.

The implementation is divided into three main parts: HTML, CSS, and JavaScript.

Step 1: Adding the HTML Structure

First, I added a floating language button and a separate container for the translation selector.

<!-- Language Toggle Button -->
<button id="language-toggle"
        class="language-toggle ignore"
        type="button">

    <i class="fa-solid fa-language language-icon"></i>

    <span class="language-text">Lang</span>

</button>

<div id="translate-container"
     class="translate-container">
</div>

The language-toggle button is responsible for opening and closing the language selection panel.

The translate-container element is where the custom language selector is dynamically generated using JavaScript.

I also added the ignore class to the floating button so that it remains unaffected by automatic translation.

Step 2: Styling the Language Selector with CSS

I created a separate stylesheet named translate.css to keep the language selector styling independent from the rest of the website.

The main styling focuses on the floating button and the language selection panel.

Important CSS features include:

  • Fixed Positioning: The language button remains at the bottom-right corner of the screen.
  • Circular Design: A 55px by 55px button with a circular border radius.
  • Layer Management: High z-index values keep the selector visible above other webpage elements.
  • Dropdown Panel: A white floating panel with rounded corners and a subtle shadow.
  • Searchable Options: Styling for the language search input and scrollable options list.
  • Interactive States: Hover, selected, and keyboard focus styling for better usability.
  • Responsive Design: Adjustments for smaller mobile screens.

Here is a simplified example of the main CSS styling:

/* Floating Language Button */

.language-toggle {
    position: fixed;
    right: 25px;
    bottom: 25px;

    width: 55px;
    height: 55px;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    border: none;
    border-radius: 50%;

    background: var(--h2-color);
    color: var(--post-action-color);

    cursor: pointer;
    z-index: 9999;

    box-shadow: 0 3px 12px rgba(0,0,0,.18);

    transition: .25s ease;
}

/* Language Selection Panel */

.translate-container {
    display: none;

    position: fixed;
    right: 24px;
    bottom: 80px;

    width: 280px;
    max-width: calc(100vw - 32px);

    padding: 12px;

    background: #fff;
    border-radius: 12px;

    box-shadow: 0 4px 20px rgba(0,0,0,.15);

    z-index: 9998;
}

/* Show Panel */

.translate-container.active {
    display: block;
}

/* Searchable Language Options */

.language-dropdown-options {
    max-height: 240px;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Mobile Responsiveness */

@media (max-width: 480px) {

    .language-toggle {
        right: 16px;
        bottom: 24px;
    }

    .translate-container {
        right: 16px;
        bottom: 90px;

        width: 240px;
    }

    .language-dropdown-options {
        max-height: 200px;
    }

}

The complete stylesheet also includes additional styling for the dropdown trigger, search input, individual language options, selected states, scrollbar, and accessibility.

You can explore the complete CSS implementation here:

View translate.css on GitHub

Step 3: Initializing translate.js in JavaScript

Next, I configured translate.js to use English as the original webpage language and selected the client.edge translation service.

// Set original webpage language
translate.language.setLocal('english');

// Select translation service
translate.service.use('client.edge');

// Set language selector container
translate.selectLanguageTag.documentId =
    'translate-container';

translate.selectLanguageTag.show = true;

// Ignore language selector during translation
translate.ignore.id.push('translate-container');

// Monitor dynamically rendered content
translate.listener.start();

I also used the library's custom UI functionality through translate.selectLanguageTag.customUI to build my own searchable language dropdown instead of relying on the default selector.

The custom dropdown is generated dynamically from the language list provided by translate.js. I created the language options using JavaScript and added their English and native language names.

To make language selection easier, the dropdown also includes a search input that filters the available options as visitors type.

Step 4: Remembering Language Preferences

Another important feature I implemented was remembering the visitor's selected language across page visits.

For this, I used the browser's localStorage API.

const LANGUAGE_STORAGE_KEY = 'preferredLanguage';

// Retrieve saved language
function getSavedLanguage() {
    return localStorage.getItem(LANGUAGE_STORAGE_KEY);
}

// Save selected language
function savePreferredLanguage(languageId) {
    localStorage.setItem(
        LANGUAGE_STORAGE_KEY,
        languageId
    );
}

Whenever a visitor selects a language, its language ID is saved in localStorage. When the page is opened again, JavaScript retrieves that saved preference and restores the selected language through the translation library.

I also added error handling around localStorage operations so that the translation interface can continue working if browser storage is unavailable.

Step 5: Connecting the Custom Dropdown with translate.js

The custom dropdown uses a hidden native select element to communicate with translate.js.

When a visitor selects a language, JavaScript:

  • Updates the hidden select element's value.
  • Saves the selected language in localStorage.
  • Dispatches a change event.
  • Calls the library's language selection handler.
  • Updates the visible selected language.
  • Closes the dropdown and resets the search input.

The important integration instruction is:

select.addEventListener('change', function(event) {

    translate.selectLanguageTag.selectOnChange(event);

});

This allows the custom interface to use translate.js for the actual language switching functionality.

Step 6: Connecting the Floating Button

Finally, I connected the floating language button to the translation container.

const button =
    document.getElementById('language-toggle');

const container =
    document.getElementById('translate-container');

if (button && container) {

    button.addEventListener('click', function() {

        container.classList.toggle('active');

    });

}

Clicking the button toggles the visibility of the language selection panel. Visitors can then open the searchable dropdown and select their preferred language.

I also implemented outside-click handling to close the dropdown when visitors interact elsewhere on the page.

The complete JavaScript implementation contains additional logic for language sorting, searching, saved preference restoration, dropdown accessibility, and translation initialization.

You can explore the complete JavaScript source here:

View translate.js on GitHub

💡 My tip: Keeping the translation library configuration, custom dropdown logic, and CSS styling organized makes the implementation easier to maintain and customize.

📂 6. Understanding the Project File Structure

In my personal blog, I separated the language selector into dedicated CSS and JavaScript files instead of placing everything directly inside the main HTML document.

The relevant project structure looks like this:

Madhurima-Mindscape/
│
├── css/
│   └── translate.css
│
├── js/
│   └── translate.js
│
└── blog.html

The HTML document contains the floating button and translation container. The CSS file manages the appearance and responsive layout, while the JavaScript file handles translation configuration and user interactions.

Complete source files:

This separation also makes it easier to reuse the language selector across different pages of the website.

📱 7. Responsive Design and User Experience

A language selector should be convenient to use on both desktop and mobile devices.

For this reason, I included responsive CSS rules to adjust the floating button and language selection panel on smaller screens.

The mobile implementation includes:

  • Adjusted button positioning.
  • Responsive language panel width.
  • Reduced dropdown height for smaller screens.
  • Scrollable language options.
  • Touch-friendly language selection buttons.
  • Keyboard focus styling for accessibility.

I also used a search input so that visitors do not need to scroll through the entire language list to find a particular language.

The selected language is highlighted, making it easier to identify the currently active translation.

💡 My tip: When implementing a language selector with many options, a searchable dropdown provides a much better experience than a long, unfiltered list.

⚠️ 8. Important Considerations and Limitations

Although translate.js makes website translation easier, there are a few things developers should consider before using it.

  • Translation Quality: Automatic translations may not always preserve the exact meaning or context of the original content.
  • Translation Service Availability: The selected translation service must be accessible for translation requests to work successfully.
  • Dynamic Content: Content rendered after the initial translation may require the appropriate monitoring configuration.
  • Custom Terminology: Brand names, technical terms, and specialized vocabulary may require custom translation rules.
  • Browser Storage: Saved language preferences depend on browser storage availability and the visitor's storage settings.
  • SEO Considerations: Automatic client-side translation should not be treated as a replacement for properly localized pages when implementing a multilingual SEO strategy.
  • Privacy and External Services: Review the selected translation service's policies before sending website content for translation.
  • Testing: Test translated layouts carefully, particularly for right-to-left languages and languages with longer text.

It is also important to remember that automatic translation is different from professionally localized content. Important legal, technical, or business information may require human review.

🔍 9. Exploring the Project and Documentation

translate.js provides considerably more functionality than basic webpage translation. Its customization instructions make it possible to adapt the library to different website requirements.

If you want to explore more advanced features, I recommend going through the official documentation and GitHub repository.

Useful Resources:

Exploring the original library alongside a practical implementation is a useful way to understand how its different features can be applied to real websites.

💜 Making Websites Multilingual with translate.js

Building multilingual functionality does not always require a complicated internationalization system. JavaScript libraries such as translate.js provide a convenient way to introduce automatic translation into existing websites.

In this tutorial, we explored the basic setup of translate.js, its translation service configuration, language selection functionality, and several advanced customization instructions.

We also explored my practical implementation in Madhurima Mindscape, where I created a floating language button with a searchable dropdown, responsive styling, and localStorage-based language preference persistence.

What I particularly like about this implementation is that it keeps the language selector separate from the main website layout while providing visitors with an accessible way to switch between languages.

If you are developing a personal blog, portfolio, documentation website, or another HTML-based project, translate.js is worth exploring for its flexible translation and customization capabilities.

"Good web development is not just about adding functionality. It is also about making that functionality accessible and convenient for everyone."

Want to explore the complete implementation? Check out the translate.js library and my personal blog's source code.

Explore translate.js

Explore Madhurima Mindscape