Skip Navigation

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.

Tagged: 

This topic contains 5 replies, has 0 voices.

Last updated by colbyH 6 months, 3 weeks ago.

Assisted by: Andreas W..

Author Posts
November 12, 2024 at 8:50 pm #16394104

colbyH

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?

November 12, 2024 at 10:06 pm #16394788

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Here an example about how to obtain all German pages from wpml.org:

https://wpml.org/wp-json/wp/v2/pages/?lang=de

If you would like to query for an original content and knwo if it has translations, you should consider using PHP togther with the folllowing hooks:

https://wpml.org/wpml-hook/wpml_get_element_translations/

https://wpml.org/wpml-hook/wpml_element_trid/

Best regards
Andreas

November 13, 2024 at 6:34 am #16395424

colbyH

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?

Screenshot 2024-11-12 at 23.33.06.png
November 13, 2024 at 4:59 pm #16398804

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

You would use the language code, in this case "en", as language parameter.

Example:
https://wpml.org/wp-json/wp/v2/pages/?lang=en

November 13, 2024 at 6:33 pm #16399213

colbyH

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.

November 13, 2024 at 6:58 pm #16399285

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

You would need to query for the ID of the translated post. Each translation has its proper ID.

Does this solve the issue?

November 13, 2024 at 7:12 pm #16399330

colbyH

Right, it's just not exactly intuitive.