Support » Plugin: TranslatePress - Translate Multilingual sites » How to get content in specific language programmatically?

  • Resolved Alaa Rihan

    (@alaa-rihan)


    Hi,

    I’m trying to get content by specific language programmatically for example getting the content like this:
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;

    So $content will be in the default language in this case, how to get the french translation of it?

    and the same question for post meta (custom fields), like this:
    $custom_field = get_post_meta( $post_id, ‘custom_field’, true ) );

    How to get the french translation of $custom_field?

    Best Regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Cristian Antohe

    (@sareiodata)

    Hi Alaa,

    TranslatePress uses a different method for translating content that is completely different from WPML or Polylang.

    We’re translating the final output from your theme, regardless of where that content is coming from. We’re not creating new posts for the translation.

    So while you can’t output the translated content, just output the normal content, and on the french side it will just replace those strings with the translated ones.

    Now, if you need this for an API, we currently don’t have support for such a thing. But we will have in the future.

    Hello,

    Thank you for your explanation,

    Actually I need to output both languages in an excel file as a report so I need to get the original text and the translated text too..

    As I see you use gettext wp function for the translation so could you guide me to get the the translation for the post content and post meta by code please?

    Plugin Author Cristian Antohe

    (@sareiodata)

    Hi,

    You can use a function like this:

    
    // Function to translate any content with TranslatePress
    // $locale is an existing locale defined in TranslatePress. Ex: en_EN
    function trp_content_translation($content, $locale){
        if (class_exists('TRP_Translation_Render')){
    
            $trp = TRP_Translate_Press::get_trp_instance();
            $render = $trp->get_component('translation_render');;
    
            global $TRP_LANGUAGE;
            $orig_lang = $TRP_LANGUAGE;
            $TRP_LANGUAGE = $locale;
    
            $content = $render->translate_page($content);
    
            $TRP_LANGUAGE = $orig_lang;
        }
    
        return $content;
    }
    

    Then do something like echo trp_content_translation($content, ‘fr_FR’); where $content can be anything that was already translated.

    If a translation exists for those strings, it will return it.

    Hi,

    Great, thank you very much, I will try it…

    Best regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get content in specific language programmatically?’ is closed to new replies.