NextJs
Easily integrate LingoJS into your Next.js app using the <Script> component for dynamic translation.
Last updated
Easily integrate LingoJS into your Next.js app using the <Script> component for dynamic translation.
Last updated
First, include the LingoJS script in your Next.js project by using the Script
component from next/script
in the pages/_app.js
or any page/component where you want the translation functionality to be initialized.
In the example above, the Script
component is used to load the LingoJS script asynchronously. Once the script has loaded, the onLoad
handler initializes LingoJS using your provided projectKey
.
To change the language dynamically, you can call the window.Lingojs.changeLanguage()
function. It takes the ISO 639-1 language code as an argument.
For example, to change the language to French, you would call:
LingoJS will automatically detect text content on your page and send it to the dashboard for translation. However, if you have content that should not be translated, you can use the translate
attribute to control translation behavior.
Disabling Translation for an Element:
To prevent certain elements from being translated, add the translate="no"
attribute to them:
Handling Dynamic Variables:
For dynamic variables (content that shouldn't be translated within a sentence), use the <var></var>
tag. This allows the dynamic content to be shown in the dashboard but keeps it empty for translation purposes.
This ensures that the dynamic content won't be misinterpreted or altered in the translation process.
Remember Language: If you want the language choice to persist (i.e., for users to see the same language on returning visits), you can set rememberLanguage: true
in the initialization.
Translation Widget: If you want to display a translation widget (for users to change the language on their own), you can enable it by setting showWidget: true
.
Do not modify HTML tags in the LingoJS dashboard. The translations are automatically generated to ensure they retain the proper attributes (e.g., classes, styles) from the DOM.
Ensure that your projectKey
is correctly configured for your LingoJS project.
App.js
:When you edit a translation in the LingoJS dashboard, do not modify the HTML tags (like <div>
, <span>
, or <p>
) within the translation interface. These tags are automatically generated by LingoJS as decorators to maintain their structure and ensure proper styling and functionality on your site.
Changing these HTML elements in the dashboard could lead to issues, such as:
Loss of formatting: The translations might not retain their original structure or styling.
Breaking functionality: Tags like <div>
, <span>
, or other elements might carry important attributes (like class
or id
), which are necessary for your layout and interactivity.
LingoJS automatically generates these HTML elements in the translation process, which are then reused in your website’s DOM once the translations are applied. Modifying these tags in the dashboard would disrupt this process, causing translations to break or misalign with the intended design.
By following these steps, you can successfully integrate LingoJS into your Next.js project. LingoJS will automatically handle text translations and allow dynamic language switching through the API.