[Resolved] Access translations via WordPress REST API
This thread is resolved. Here is a description of the problem and solution.
Problem: The client is using a decoupled website with a Next.js/React frontend and a WordPress backend, attempting to access different translations of a post via the WordPress REST API. They are facing issues when adding the query parameter 'lang=de' to fetch German translations, as no translation information appears in the JSON object. Solution: To fetch all pages in a specific language, such as German, from your WordPress site, you can use the following API endpoint:
/wp-json/wp/v2/pages/?lang=de
If you need to check if the original content has translations, consider using PHP with the following WPML hooks:
Remember, each translation has its own ID, so you might need to query for the ID of the translated post to access specific translations.
If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. We also highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum at https://wpml.org/forums/forum/english-support/.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Background of the issue:
We have a decoupled website with a Next.js/React frontend and a WordPress backend hosted on a separate server. I am trying to access different translations of a post via the standard WordPress REST API (/wp-json/v2/post-type).
Symptoms:
When I add the query parameter 'lang=de' to try to grab the existing German translation of the page, nothing happens or changes. I am not seeing any translation information in the '_links' property or a 'wpml' property in the JSON object.
Questions:
How can I find out what translations are available for a post via the WordPress REST API?
Is there a pattern or setting I missed in the documentation for accessing translations?
Is there a reasonable workaround for accessing translations via the REST API?
Do I need to build this feature myself to access translations?
Looks like I made a mistake in my original post, where I said I was using "lang=de" when instead I was actually using "lang=de_DE". While it's nice to see my German translations come up without using the region code, I can't get my UK translations using any of "en_GB", "en_gb", "en-GB", or "en-gb". I figure this is due to me also using US translations (locale: en_US, code: en), which is the default language in my configuration. Does WPML support accessing locale specific translations via REST?
For anyone else that's running still running into issues, I realized that part of the issue I was having with my UK translations is that the slug of the page I was testing happened to be the same between the US and German translations while the UK one was different. I'm not sure how that happened, but after changing it to match, things started working as they were with the German translations on my site.
One other thing I'm noticing—when I query for a specific post using its ID, i.e. /wp-json/wp/v2/pages/1234?lang=de, I only get the original language, not the translated. When I do a search by slug, i.e. /wp-json/wp/v2/pages?slug=home&lang=de, I do get the translated version. I don't know if this is intentional or not, but it seems I need to query by slug to flip between languages.