No translation using register_rest_api
-
Hi,
I have a problem with the translation. I am adding a field to WP rest api using “register_rest_field”. This field is a post meta element retrieved using “get_post_meta” – from what I noticed it doesn’t matter where the value comes from.
The point is that the fields added by register_rest_field are not translated.
I tried to get around this by using the “trp_translate” function, unfortunately here I don’t get translation either – the function itself works because it adds a span element with “data-no-translation”.Is there any way to translate the translation of such elements in rest?
Part of code I am using to add field to rest api from function.php:function my_meta_is_protected_meta($protected, $meta_key) { $meta_keys = ['_genesis_title', '_genesis_description']; if (in_array($meta_key, $meta_keys)) { return false; } return $protected; } add_filter("is_protected_meta", "my_meta_is_protected_meta", 10, 2); function slug_get_post_meta_cb( $object, $field_name, $request ) { // return get_post_meta( $object[ 'id' ], '_genesis_title' ); return trp_translate( "Text to translate", "en_US" ); } function slug_update_post_meta_cb( $value, $object, $field_name ) { return update_post_meta( $object[ 'id' ], $field_name, $value ); } register_rest_field( 'post', 'tsf_metadesc', array( 'get_callback' => 'slug_get_post_meta_cb', 'schema' => null, ) );-
This topic was modified 2 years, 5 months ago by
zakzag.
-
This topic was modified 2 years, 5 months ago by
-
Hello there,
Thank you for reaching us!
The code using trp_translate should work. However, you need to make sure you have a translation for that exact string inside TranslatePress. To check this you can go to Settings -> Advanced -> Debug -> Show regular strings tab in String Translation. Then go to Translate Site -> String translation -> Regular and search for your string there.
If it’s not translated there, it won’t be returned by trp_translate.
If there’s anything else, please let me know!
Kind Regards,
Thanks for your answer!
Unfortunately, even though the translation is available in the settings (as you mention and as you can see on screenshot) I am still not getting the correct translation.https://pasteboard.co/mGtWxC6JCPMo.jpg
Interestingly if I use the default api without “en” in the url – then the translation works.
-
This reply was modified 2 years, 5 months ago by
zakzag.
If you still need help on this topic, please, reply to this thread in order that my colleague to continue the investigation on the subject.
Yes, I am still looking for a solution.
-
This reply was modified 2 years, 5 months ago by
The topic ‘No translation using register_rest_api’ is closed to new replies.