Please make sure to update to WPML 4.3.6 and check our list of Known Issues before reporting

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.

Our next available supporter will start replying to tickets in about 9.75 hours from now. Thank you for your understanding.

Tagged: 

This topic contains 29 replies, has 2 voices.

Last updated by Aurelien 2 years, 4 months ago.

Assigned support staff: Bigul.

Author Posts
August 3, 2017 at 12:29 am #1333750

Aurelien

We are currently making a new wordpress multisite website but have issue on the way we can get translated data from WPLM REST API.

Could you please help us on how we can get these datas.

Thanks a lot

August 3, 2017 at 11:25 am #1334059

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi ,

Thank you for contacting the WPML support. We are happy to help you.

Hope you are using the latest version of WPML(3.7.1) and WordPress(4.8). To help you faster, I've enabled debug information for this support ticket. Please see this link for how to get this information from your site and give it to us: http://wpml.org/faq/provide-debug-information-faster-support/

Please explain more about the requirement/workflow for the better understanding.

--
With Regards

Bigul

August 4, 2017 at 8:25 am #1334923

Aurelien

Here it is. Thanks a lot for your help

August 4, 2017 at 3:38 pm #1335421

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi ,

Thank you for the Debug information. I have a request. Please share me more about the requirement for the better understanding.

but have issue on the way we can get translated data from WPML REST API

Please note the following for the expected performance of your site.

1) Increase *WP Memory Limit* to *256*(the minimum required memory limit for WPML is *128*) or more. Please check the following links for more details

https://wpml.org/home/minimum-requirements/

https://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP

2) Increase *MaxInputVars* of the PHP configuration settings to *5000* or more

--
With Regards

Bigul

August 7, 2017 at 12:52 am #1336071

Aurelien

Hello Bigul,

Thanks a lot for your answer. We will proceed to this modification tomorrow and come back to you.

What we would like to know is if it exists WMPL documentation about using rest API and multi-site.

Thanks again for your help.

August 7, 2017 at 12:39 pm #1336428

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Thank you for the updates and waiting for the details.

Now we have only one tutorial about REST API in our documentation. But it is about using WordPress REST API with WooCommerce Multilingual.

https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/

For your kind information, with WPML you can use the WordPress Rest API as usual. If WPML is installed the only thing it should do is to return an extra field with the language of the post. WPML will save the language when creating a new post(language parameter) and also return a list of languages.

--
With Regards

Bigul

August 8, 2017 at 7:42 am #1337210

Aurelien

Hello Bigul,

Thanks for your answer. Unfortunately, your documentation is not really useful in our case as we don't use Woocommerce.
We would like to know how to get Articles and Pages in a WordPress multiwebsite with WPML with Rest API.

Can you please give us an example ?

We have more questions regarding WP Rest API and WPML.

- How did you get translations of any custom fields of a theme options page with WPML and Rest API ?
- How did you get translations of string translation section with WPML and Rest API ?
- How can we add some string translation inside wpml interface without a php wordpress theme ?

Thanks for you help and have a nice day.

August 9, 2017 at 7:46 am #1338140

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

You can read a single post via REST API easily by using the ID. For example like this --
hidden link. But the result will be missing the language information. You can add language information to the result by using *rest_prepare_this-post_type* hook. Please check the following tutorial link and sample code for more details. You can place this code in the functions.php of your theme.

https://developer.wordpress.org/reference/hooks/rest_prepare_this-post_type/

add_filter( 'rest_prepare_post', 'filter_post_json', 10, 3 );
add_filter( 'rest_prepare_page', 'filter_post_json', 10, 3 );

function filter_post_json( $data, $post, $context ) {
    $args = array('element_id' => $post->ID, 'element_type' => $post->post_type );
    $lang = apply_filters( 'wpml_element_language_code', null, $args );
    if( $lang ) {
       $data->data['lang'] = $lang;
    }
    return $data;
}

You have to use the REST API exactly the same as without WPML to get translations of any custom fields of a theme options page. The only difference is that you will have a different Rest URL for each language.

For example, if you want to get the latest posts in the default language, you can use:

hidden link

On the other hand, if you want the latest Spanish posts you can use the Spanish URL:

hidden link

Right now we don't have much documentation about the WPML and REST API integrations. But we are planning to add it in future. As you can see, there is nothing special WPML has to do the REST API is provided by WordPress and you just use one URL or another depending on the language you want.

https://developer.wordpress.org/rest-api/

You can use https://wpml.org/wpml-hook/wpml_translate_single_string/ or https://wpml.org/wpml-hook/wpml_translate_string/ API to get the translated Strings.

Please explain more about the following for a better understanding.

How can we add some string translation inside wpml interface without a php wordpress theme ?

--
With Regards

Bigul

August 10, 2017 at 1:52 am #1338964

Aurelien

Hello Bigul,

Thanks for your reply.
It is a a great news if it's fully supported.
The issue is that when I get data from this endpoint :
hidden link

I get all data for my default language.
And in my case, we will create a react frontend, if I change my language, I should be able to know how to get the translations from the previous endpoint, but I found nothing inside the data who is coming from my endpoint.
So, is it something that we have to write inside the php functions too (as we did for lang attribute) ?

Thanks.

August 10, 2017 at 2:48 pm #1339415

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Thank you for the updates. Hope you can achieve this using *get_next_post* & *get_previous_post* WordPress functions.

Please check the following tutorials and sample code for more details.

https://codex.wordpress.org/Function_Reference/get_next_post

https://codex.wordpress.org/Function_Reference/get_previous_post

add_filter( 'rest_prepare_post', 'filter_posts_next_previous', 10, 3 );

function filter_posts_next_previous( $data, $post, $context ) {
   
    if( get_previous_post() ) {
       $data->data['previous_post'] = get_previous_post();
    }
	
    if( get_next_post() ) {
       $data->data['next_post'] = get_next_post();
    }
	
    return $data;
}

--
With Regards

Bigul

August 11, 2017 at 12:09 am #1339838

Aurelien

Thanks for helping, regarding your post-1339415, your code is only to get next and previous post right ?

Maybe I didn't explain very well, If I visit a post on the default language, I change my language on the front part, I should be able to know how to get the translations from my current post, but I found nothing inside the data who is coming from my endpoint.

So, is it something that we have to write inside the php functions too (as we did for lang attribute) ?

Thanks !

August 11, 2017 at 7:42 am #1339977

Aurelien

I can now register some word inside string translation plugin and after in the backend I have added some translation, but, how to get the correct translation in my custom endpoint ?

So I have a funtion to set the correct language asked with :
$sitepress->switch_lang($language, false);

For fr (who is the default language) I get the correct word because it's the default one.
And if I set english and use this code, it doesn't work, I get the french version yet :

apply_filters('wpml_translate_string', 'myword', 'myword', 'mydomain');

Is is wrong ? I register all words with :
do_action('wpml_register_string', $translation, $translation, $package, $translation, 'LINE');

Thanks.

August 11, 2017 at 2:39 pm #1340350

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

In my previous reply *#post-1339415* -- the code will only append Previous and Next Post details to the WordPress JSON data.

You can call the string's translation like the following. Please check and let me know your feedback.

$custom_textarea     = apply_filters('wpml_translate_single_string', $instance['custom_textarea'], 'Widgets', 'Custom Widget - textarea field', 'en' );
 

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

--
With Regards

Bigul

August 14, 2017 at 9:08 am #1341338

Aurelien

Thanks for your reply.
I found a way to add existing translations slug and id for all pages and posts.
Now it's about string translations, it doesn't work, I guess I didn't follow the correct way, can you please tell me what's wrong with this code please ?

```$translations = array(
'Tout',
'Informations',
'Actualités',
'Agenda',
'Réseaux sociaux',
'Médias'
);

$package = array(
'kind' => 'My-Package',
'name' => 'my_package',
'title' => 'My-Package',
'edit_link' => '',
'view_link' => ''
);

foreach($translations as $translation):
do_action('wpml_register_string', $translation, $translation, $package, $translation, 'LINE');
endforeach;
```

```$t_all = apply_filters('wpml_translate_string', 'Tout', 'Tout', $package, 'en');
```

with this code I just get the default language (Fr) not the english translation.

Thanks a lot !

August 14, 2017 at 2:51 pm #1341791

Bigul
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Please let me know the following for tracking the issue.

1) In which page/section you are using this code. Please explain more about this.

2) Are you able to register the strings(in WPML>>String Translation) for translation using this code?

3) For showing the translation please use the following code.

$t_all = apply_filters('wpml_translate_string', 'Tout', 'Tout', $package);

Please check this tutorial for more details -- https://wpml.org/documentation/support/string-package-translation/

--
With Regards

Bigul