Locadapt exposes a window.locadapt object that allows developers to programmatically interact with the translation functionality. This API reference outlines the main methods and properties available for advanced usage and integration.

Locadapt is optimized to work out-of-the box, and only call or rewrite these methods when necessary for advanced or bespoke functionality.

Main Methods

updateContent()

Manually triggers a content update, translating any new or changed content on the page.

window.locadapt.updateContent();

Use this method if Locadapt isn’t picking up on new content / the first page render. For example, if you have dynamically updated text nodes, you may want to call this (our mutationObserver picks up on added nodes, but not text updates by default as for most scenarios this leads to excessive translation requests).

figureOutLanguage()

Detects the current language based on the URL structure.

const languageInfo = window.locadapt.figureOutLanguage();

Returns an array with the language name and its mapping information, or null if not detected.

Updates the canonical and alternate links in the document’s head to reflect the current language and available translations.

window.locadapt.setupCanonicalAndAlternateLinks();

This method is automatically called by Locadapt when the language is changed. You could rewrite this method to change the canonical and alternate links in a custom way, or to add additional functionality.

toggleSelector()

Toggles the visibility of the language selector dropdown.

window.locadapt.toggleSelector();

Useful for creating custom language selection interfaces.

selectLanguage(code: string, nativeName: string)

Programmatically changes the current language.

window.locadapt.selectLanguage('fr', 'Français');

Use this to switch languages without user interaction with the default selector.

Properties

currentTranslateToLanguage

Gets or sets the current target language for translations.

console.log(window.locadapt.currentTranslateToLanguage);
window.locadapt.currentTranslateToLanguage = 'Español';

localStorageData

Provides access to the current Locadapt configuration and translation data.

const config = window.locadapt.localStorageData;

This object contains information about the project settings, available languages, and cached translations. It is a subset of the data stored on Locadapt’s servers, and a mirror of the data present in the actual localStorage of your site by our plugin.

Advanced Usage

For more complex integrations or custom behavior, you can also access and modify internal Locadapt properties and methods. However, use these with caution as they may affect the core functionality:

  • applyTranslations(translations: Map<string, { originalText: string; translatedText: string; }>)
  • collectTranslatableContent()
  • sendTranslationRequest(items: TranslationItem[])

Refer to the window.locadapt object or contact us for detailed information on these advanced methods.

Modifying internal Locadapt functionality may lead to unexpected behavior. Always test thoroughly when using these advanced methods.