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.

This topic contains 22 replies, has 5 voices.

Last updated by Carlos Rojas 7 months ago.

Assigned support staff: Carlos Rojas.

Author Posts
December 27, 2018 at 4:50 pm #3037949

herryB

I am trying to: Get translated content in the posts REST API.

Link to a page where the issue can be seen: .../en/wp-json/wp/v2/posts?per_page=2&status=publish&_embed

I expected to see: the English version of the content.

Instead, I got: post content itself is shown in the site default language (NL in this case), other items such as 'tags', 'categories', 'authors', etc. are shown in the requested language.

This issue is caused by a change in sitepress-multilingual-cms\inc\functions-load.php at line 104

` if ( $is_admin === true ) {`

is in version 4.1.3 replaced by

` $is_rest = array_key_exists( 'rest_route', $_REQUEST ) || ( false !== strpos( $_SERVER['REQUEST_URI'], 'wp-json' ) );

if ( $is_admin === true || $is_rest ) {`

thus sending the REST API request handling through WPML_Backend_Request i.s.o WPML_Frontend_Request.

I hope this helps.

Kind regards,
Peter

December 27, 2018 at 7:11 pm #3038307

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hello Peter,
Thank you for contacting WPML support. I will do my best to help you solve this issue.

I'm consulting our specialists about this issue.

In the mid-time I kindly ask you to update WPML plugins to the last version and double check if the issue persists.

Looking forward to your answer.

Kind regards,
Carlos

December 27, 2018 at 8:39 pm #3038510

herryB

Hi Carlos,
Thanks for your reply.
All WPML plugins are up to date. Only Sitepress multilingual cms was downgraded from 4.1.3 to 4.1.2 in order to have the REST API show the expected data.
Kind regards,
Peter

December 28, 2018 at 1:49 pm #3040314

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi Peter,
Thank you very much for your message.

I kindly ask you to follow the steps described in this ticket and let me know the result: https://wpml.org/forums/topic/custom-wp-rest-api-routes-that-use-the-lang-parameter-null-its-value/#post-3038593

Looking forward to your answer.
Best regards,
Carlos

December 29, 2018 at 4:47 pm #3042442

herryB

Hi Carlos,

Thanks for your reply.
I've looked into the suggested ticket but alas to no avail.
I did try some switching back and forth and please find my observations below.

We use "Different languages in directories" with "Use directory for default language" with an HTML fille for root url, together with pretty permalinks "/%postname%/".
(I already tested without setting "Use directory for default language" yesterday).
I've switched to "Language name added as a parameter", flushed the permalinks and retested the API output: the same issue persists.

Switch back to original settings with "Different languages in directories" and re-save the permalinks.
Next I performed a couple of tests with these results:

Anonymous session:

  • Retrieve domain.com/wp-json/... and domain.com/nl/wp-json/...
    Result: all data in site default language NL as expected.
  • Retrieve domain.com/xx/wp-json/...
    Result: postdata in site default language NL, post attributes in requested lang=xx, all other data in site default language NL.
  • Retrieve domain.com/xx/wp-json/...&lang=yy - so with *both* language directory and queryparm
    Result: postdata is in lang=yy (and determines which posts are selected), post attributes are in lang=xx, all other data is in lang=yy. Site default data is of no influence.

 
Logged in session:
 

  • Retrieve domain.com/xx/wp-json/...
    Result: postdata is returned in the language that is set in the *Admin language selector*, other post attributes in requested language XX, all other data as set in Admin language selector.
  • Retrieve domain.com/xx/wp-json/...&lang=yy - so with _both_ language directory and queryparm
    Result: postdata is in lang=yy (and determines which posts are selected), post attributes are in lang=xx, all other data is in lang=yy. Admin language selector is of nu influence.

 

Please bear in mind that it's easy to make a mistake..!
I used WP5.0.2, with only WPML plugins activated, theme twentynineteen.
WPML is set up to use NL (default language), EN (about 20 translated posts available), FR (1 test translation), and DE, ES and IT all three still without translations.

I hope this helps.

Kind regards,
Peter

December 31, 2018 at 5:00 pm #3045920

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi Peter,
Thank you very much for your detailed message.

I'm consulting our specialists about this issue and I will get back to you as soon as I have an answer from them.

Thank you very much for your patience and understanding!
Happy New Year!
Best regards,
Carlos

January 2, 2019 at 11:18 am #3048710

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi there,

I kindly ask you to go to WPML -> Languages -> Change the URL format to use the language parameter -> Save the changes -> Re-save the permalinks structure -> Then try adding ?lang=en in the URL does it work?

Regards,
Carlos

January 2, 2019 at 2:57 pm #3049562

juriK-3

I have the same issue with 4.1.3

/ru/wp-json/wp/v2/pages - Doesn’t work and returns default language data

/ru/wp-json/wp/v2/pages?lang=ru - Works, but I will have to modify my React app for this extra param

4.1.2 was working fine without extra lang param. This seems to be critical.

January 6, 2019 at 3:48 pm #3060473

herryB

Hi Carlos,

I've changed the site to use ?lang=xx as you asked for.
Like this the data in the rest api is correctly translated for all situations.

One more thing in addition:
All URL's in the returned data include the language indicator except for the 'guid':'rendered'.
Thus the guid is only correct for the default language. This already was like this before 4.1.3 and seems to be independent of Language URL format.

@jurik-3 Thanks for chipping in.

Hope this helps!

January 7, 2019 at 4:05 pm #3063596

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi,
I'm glad to hear that the issue is solved 🙂

Thank you very much for the information provided regarding the guid.

Please don't hesitate to contact us if you need our help in the future with WPML.
Best regards,
Carlos

January 7, 2019 at 4:06 pm #3063599

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi @jurik-3,

Please follow the recommendation in my previous message and check if the issue is solved on your side.

If the issue persists, please create a new ticket and describe as best as possible the issue.

Best regards,
Carlos

January 7, 2019 at 7:33 pm #3064239

herryB

Hi Carlos,
I'm sorry but changing the URL format is not a solution. There's a reason to choose for one or the other URL format - independent of the usage of the REST API.
The solution that is needed, is that data in the REST API is correct, independent of the selected URL format, isn't it?
Please have a look into making that happen.
Kind regards,
Peter

January 7, 2019 at 11:51 pm #3064620

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi Peter,

I just updated our specialists and I'm waiting an answer from them.

I will get back to you as soon as I have an answer.

Regards,
Carlos

January 8, 2019 at 8:08 am #3065777

Filip Sokołowski

I have exactly the same problem with REST API. Changing the URL structure is also not solution for me, because then I have to rebuild all App logic.

January 8, 2019 at 12:29 pm #3066747

Carlos Rojas
Supporter

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

Timezone: America/Montevideo (GMT-03:00)

Hi Peter,

I'm sorry, it seems I didn't explain myself correctly in my previous messages. Our specialists suggested to add the lang parameter but not to change the URL structure. For instance:
hidden link

Adding the lang parameter solves the issue, could you confirm it works correctly in your case?

Regards,
Carlos