Multilang is based on Polylang plugin on WP side, and NextJS is handling the routing and fetching of the correct content based on the pathname.
We've created a plop script that simplifies the process of migrating between single language and multilanguage setups.
Run the script that will update the code and files depending on if your website supports multilang or not :
💡 You will need to have Docker running before executing this script, by running
npm run startinwordpressfolder
⚠ You may need to change
spck_wpcontainer name with your project's container name inside thegenerators/lang-migration-generator.jsfile.
npm run generate:language-migrationFollow the prompts to choose the migration type and enter the required information.
After running the migration script, you may need to re-run the provision.sh script to activate/deactivate the appropriate plugins depending on your multilang configuration.
On WP admin, set the languages and the default one in Polylang settings
If working on a multilang website :
- Use language-aware data fetching in GraphQL queries
- Use the
useLocalehook to get the currentlocale, and thedictionaryobject to get the static strings translations - If you need to add static strings translations, set them on the
[language].jsonfile which is located inside thesrc/i18n/dictionaries/folder. The locales used in here are the ones set on WP Polylang plugin. - Any component that needs to use the
useLocalehook will need to be a Client component - For the language switcher, we use the
<link rel="alternate" hrefLang="x" href="url">tags to get the available translations on each page - The 404 page won't have a header or footer, as we can't know which language to use when fetching the datas in this specific case.
If you'd like to go further on how the multilang script is working, here are the steps it follows :
-
Single language to multilanguage
This migration type will:
- Create a
[lang]folder in the app directory - Create a
layout.tsxfile in the[lang]folder based on the template set ingenerators/templates/lang-migration - Update the root
layout.tsxfile for multilanguage support - Move the
[[...uri]]folder into the[lang]folder - Update
src/configs.jsonto setisMultilangtotrueandstaticLangto the specified default locale - Update
wordpress/scripts/provision.shto setIS_MULTILANGtotrue - Require WP plugins through Composer needed for the multilang to work properly (Polylang, etc..)
- Create a
-
Multilanguage to single language
This migration type will:
- Update the root
layout.tsxfile for single language support - Move the
[[...uri]]folder out of the[lang]folder - Remove the
[lang]folder - Update
src/configs.jsonto setisMultilangtofalseandstaticLangto the specified default locale - Update
wordpress/scripts/provision.shto setIS_MULTILANGtofalse - Remove WP plugins through Composer that are not needed anymore
- Update the root