• Probably a weird question,

    I’m busy with an image generator, and it requests the title of the page, but I would need it in multiple languages.

    is there a way to get the translation of a string within php, without going to that language?
    For example trp('nl_nl','string');

    Went through the documentation but couldn’t find any mention of it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • This hack kind-of works for me:

    if ( class_exists( 'TRP_Translate_Press' ) ){
    $target_language = get_locale();
    $translator = TRP_Translate_Press::get_trp_instance()->get_component('translation_render');
    $translation = $translator->process_strings(array($original_name),target_language);
    if ($translation) $new_name = $translation[0];}

    But be warned – this hack isn’t stable – you should filter your input – bad input can cause exceptions or even sql injection vulnerabilities (the target language must exist in your translatepress and can’t be the default language). It also may slow down your site (i didn’t test the preformance of this ugly hack)

    i’m waiting for a reply too (hopefully to get better code).

    • This reply was modified 4 years, 9 months ago by robert7k.
    • This reply was modified 4 years, 9 months ago by robert7k.
    • This reply was modified 4 years, 9 months ago by robert7k.
    Thread Starter tlouwet

    (@tlouwet)

    Really nice find @robert7k

    It does the trick.
    (for my use I did change get_locale() to an array and cycled it)

    Small note, ‘target_language’ doesn’t have a $ under $translation in your posted code. But I assume that’s just an example error.

    Sadly I’m not that knowledgeable when it comes to sql injections.
    I guess you mean that I should protect/sanatize the $original_name?
    I’m pulling the page id from a GET[] > sanatize it > get_the_title(). Should be sufficient?

    And I appreciate the help.

    no idea about security.
    Yes, it was a typo. this is a part of the code i currently use:

    $trp_instance = TRP_Translate_Press::get_trp_instance();
    $translator = $trp_instance->get_component('translation_render');
    $translator_original_language = $trp_instance->get_component('settings')->get_setting('default-language');
    if (get_locale()!==$translator_original_language) {
    		$translation = $translator->process_strings(array($original_name),get_locale());
    		if ($translation) $new_name = $translation[0];
    }

    after experimenting with this kind of code just open the translatepress settings to make sure no scary errors show up (wrong code got me errors about missing sql tables!!!)

    • This reply was modified 4 years, 9 months ago by robert7k.
    Thread Starter tlouwet

    (@tlouwet)

    @robert7k and thanks for the lead.

    Noticed the function was getting it’s query from class-query.php,
    wherein function process_strings pretty much does what process_strings does. Though that function does sort of note where the translations were kept.

    $title_fr = $wpdb->get_row("SELECT * from wpvk_trp_dictionary_nl_nl_fr_fr WHERE original = '{$original_name}'")->translated;
    gives me the french translation of $original_name (does require ‘global $wpdb;’)

    Table name will probably be different for you, which you could find in your sql.
    For me (mostlikely for you too), is a table with column names: id, original, translated, status, block_type, original_id.

    Will probably do it that way as for what I’m working on it already uses $wpdb.

    • This reply was modified 4 years, 9 months ago by tlouwet.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get translated string’ is closed to new replies.