Simple Integration
For non-complex, non-SSR frameworks or static site generators (SSG), integrating Locadapt is straightforward. Simply add the following script and stylesheet tags to your HTML:data-start-hidden and a loading indicator to prevent flashes of un-translated content. See Next.js for an example or refer to the Custom Attributes section below.
React
For a standard React application using Create React App, add Locadapt in yourpublic/index.html file:
Svelte
For Svelte applications, add Locadapt in yourindex.html or app.html file:
data-ssr-defer (and possibly defer if the compiler complains) attributes to the script tag:
Next.js (Loading Spinner)
For Next.js, add Locadapt iin yourpages/_document.js or app/layout.tsx file. This example also shows the use of data-start-hidden and a loading indicator to prevent flashes of un-translated content.
Vue.js
For Vue.js applications, add Locadapt in your mainindex.html file:
data-ssr-defer attribute:
Special Data Attributes
In short, special attributes are used to handle the complexities of different frontend frameworks. You saw a few of these in theNext.js section above — copied directly from Locadapt’s site! (Our is particularly complex due to running both SSR and non-SSR versions of Locadapt, as well as an MDX blog.)
data-ssr-defer: Defers Locadapt initialization to the client. Use this for SSR applications. You may otherwise see hydration issues if the server returns translated content, but client generates un-translated content.data-link-prevent-default: Makes Locadapt handle link clicks internally for smooth SPA transitions. If link rewriting is not working, set this.data-start-hidden: Initially hides page content, revealing it after Locadapt finishes initializing. Useful for preventing flashes of untranslated content in complex applications not on thesubdomainschema. We recommend setting a#locadapt-optional-loading-indicatorin your HTML to show while Locadapt initializes. If you use this, you must also wrap your page content in adivwithid="custom-base-content", and set itsstyletodisplay: none;(it will be unset after Locadapt initializes).
data-start-hidden and a loading spinner. By default, this spinner will only be around for a max of 1-2 seconds, as if new translations are required, these will be requested once the spinner is removed.
Example Loading Indicator
Here’s an example of a simple loading indicator you can use:Programmatic Control
For more fine-grained control over Locadapt’s behavior in your frontend application, you can use the Locadapt JavaScript object. This allows you to programmatically update content, change languages, and more. See our API Reference for detailed information on available methods and properties.General Advice
- Place in
<head>: Add the Locadapt script in the<head>of your HTML document. - Server-side Rendering (SSR): Use the
data-ssr-deferattribute for (and only for) SSR applications. - Single Page Applications (SPAs): For complex SPAs, consider using
data-link-prevent-defaultto handle navigation. - Dynamic Content: Locadapt’s mutation observer will handle newly added content automatically if
support_dynamic_contentis enabled in your project settings.
window.locadapt.updateContent(); to translate the content when it has been loaded.