• This code let me switch the description from italian to german, I’d like to have as output not only the custom meta field content, but at the beginning of the description you should read the category (name of the destination) and the taxonomy (typology of accomodation); these terms too should be translated, in particular the taxonomy term. At the moment 2 different shortcodes and 2 different custom fields do this job (and throw css the category name and the taxonomy term are in a different colour and are bold), but I have to fill to many fields each time I insert a new apartment. Here is the code to modify

    //SHORTCODE DESCRIZIONE IN TEDESCO
    
    add_shortcode('description', 'translate_desc');
    function translate_desc() {
      global $post;
      $german = get_post_meta( $post->ID, 'description_de', true );
      if ( 'de_DE' == get_locale() && ! empty( $german )){
         return $german;
      } else {
         return get_post_meta( $post->ID, 'description_it', true );
      }
    }
    

    First of all I would create 2 other variables and declare them, such as $category-german and $tipology-german, than in the “return” I should have something like $category-german, $tipology-german, $german; the return for italian too should be different. Keeping in mind that I could add other languages. Not so many but al least english

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I have to fill too many fields each time

    I sympathize, but the only alternative I know of would be to utilize some sort of translation API to auto-populate the translated meta fields any time you add a term, description, or title in Italian. Machine translations don’t always do a good job, but the result is usually understandable even if not totally correct.

    For shortcode handlers, you return the complete HTML of whatever the shortcode is supposed to do. If $category-german, $tipology-german, $german all belong together in the same HTML, then that will work. But if each needs to be in a different part of the page you’ll likely need different shortcodes or an altered template. I don’t know what sort of HTML you need, but here’s an example:
    return '<span style="color: red; font-weight: 700;">'. "$category-german, $tipology-german, $german" . '</span>';

    Of course your code would need to get the values for $category-german and $tipology-german. If you are translating to multiple languages, maybe including “german” in variable names would be confusing. Maybe use “local” or “translated” or nothing extra instead?

    To get various translations based on locale, you should extract out the language part from the actual locale because the same language might have multiple locales, such as en_US, en_GB, en_AU, etc. Assign the extracted language to a var for use in the meta key name. For example:

    
    $lang = substr( get_locale(), 0, 2 );
    $desc_trans = get_post_meta( $post->ID, "description_$lang", true );
    // repeat similar for other meta values

    This way you don’t need if/else statements to get the right translation for any language you’ve translated for. However, you might want to still do an empty() check and fall back to something in case the specific translation is not available.

    • This reply was modified 2 years, 5 months ago by bcworkz. Reason: code format fixed
Viewing 1 replies (of 1 total)

The topic ‘Modifying a function’ is closed to new replies.