• On this page this part is still in italian. But the translations were working before.

    “L’appartamento si compone di: soggiorno con angolo cottura e divano letto doppio, 1 camera da letto tripla, 1 camera doppia, 1 bagno con doccia. 2 balconi, 1 posto auto”

    All the description is inserted by a shortcode but certains fields work and others dont

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

Viewing 15 replies - 1 through 15 (of 39 total)
  • Moderator bcworkz

    (@bcworkz)

    Please show us the shortcode handler function for this content.

    Thread Starter sacconi

    (@sacconi)

    <div class="entry-content"><span class="description"><?php echo do_shortcode( '[description]' ); ?></span> 
    <?php  
    

    this is the return of all the functions that get data from my custom fields

    return '<span class="incipit_description">'. $category . ' '. $terms . '</span>' . ' ' . __( 'for','sacconicase' )  .' '. $numero   .' '. __( 'persons, ','sacconicase' ). ' '  . $mq_str . $pianoapp_str . $ascensore_str . implode(', ', $output_distanze ).'.' . ' ' . $introapp_str . $descsogg_str . $camera1_str . $camera1desc_str .  $camera2_str . $camera2desc_str . $bathrooms_str . $terrazzo_str  .$giardinoprivato_str . $parking_str . $nopostoauto  . __( 'Facilities','sacconicase' ) . ': ' . trim( $output, $separator ) .'. ' . $output2 .  $sianimali  . ' ' . $refurbish_output . $content ;
    }
    

    custom fields are in a different file that is included in functions.php

    Moderator bcworkz

    (@bcworkz)

    Maybe take one problem at a time then. The fix for one will likely indicate what’s wrong with the others. Show me the source code that assigns content to one of the variables in the return statement that does not have German translation like it should. Maybe one of these: $introapp_str . $descsogg_str? Which is responsible for this content: “L’appartamento si compone di: soggiorno con angolo cottura e divano letto doppio“?

    Thread Starter sacconi

    (@sacconi)

    this is the code of the first problematic field: https://pastebin.com/0g9xKH2j in appartamenti.php included in functions.php

    then I have the following in functions.php

    $introapp = get_post_meta( $post->ID, 'intro', true );
    $introapp_str = empty( $introapp ) ? '' : "$introapp: ";
    
    Moderator bcworkz

    (@bcworkz)

    Looks OK. Does $post->ID return a valid ID? You might try get_the_ID() instead.

    I think the trouble is when you edit the post for other reasons, the selected value gets saved in Italian, overwriting whatever you’ve done earlier for German. The meta key name ought to include a language element that’s extracted from locale so each language has its own unique key name, such as “intro_de”, “intro_en”, “intro_fr”, etc. This will prevent Italian from overriding the values. You might end up with a meta key “intro_it”, but you needn’t use it.

    Thread Starter sacconi

    (@sacconi)

    the selected value is in english, not in italian. In fact if you see this page https://test.sacconicase.com/appartamento-5-posti-a-sabbiadoro-di-fronte-alla-terrazza-a-mare/ you can see one sentence still in english “balcony with direct or lateral sea view” at the 3rd line, this because I havent translated it yet. So, the translations work for italian but not for german. I’m asking also the support of the translation plugin but I guess they’ll tell me the problem is in my code. I should force the code to better read the locale, is it possible to do it inside the meta field?

    Moderator bcworkz

    (@bcworkz)

    If you’re getting English values then gettext isn’t working or the strings do not exist in the .po and .mo files.

    You can use key names like “intro_de”, “intro_en”, “intro_fr”, etc. in save_metadata_intro() by getting the locale and extracting the language code. Let’s say the language code is assigned to $lang. You can then do:
    update_post_meta($postid, "intro_$lang", $_REQUEST['intro']);

    TBH, this entire approach is flawed. You’d be saving the same multiple translations for all posts. Wasteful and inefficient. IMO it’d be far better to save the kind of property in one field and manage translations on output from a common source, be it gettext, an option array, or hard coded.

    All you should be saving is whether the property is an apartment, house, or bungalow. The rest of the phrase “The %s consists of” is the same regardless of property type and can be managed elsewhere. The last option with living and bathroom description belongs elsewhere. The 1 room for both living and sleeping really belongs with the bedroom data. For example, if the bedroom count is 0, something could say somewhere “1 room for living/sleeping”. At least in English North America that’s called a “studio” or “efficiency” apartment. Not so sure about elsewhere.

    Similarly, the bath description should be managed with the bathroom count.

    At the very least, you should just save one of appartamento, casa, or bungalow with each property’s meta data. The correct phrase can be managed on output with gettext. Similar to:
    if ('appartamento' == $post_meta ) echo __('L\'appartamento si compone di', 'sacconicase');

    Thread Starter sacconi

    (@sacconi)

    No, I cant use the bathroom count, because 1 bathroom can have a shower box, another a bath, another both of them, I need more options; gettext is working. I put the admin language in german and I see the sencences in german, the problem is that when I go back to italian I see the sentences in german. There are a lot of options and I need a selector, it’s not possible to make the locale working for this code? Do you read the german “Die Ferienwohnung besteht aus” here? https://test.sacconicase.com/lignano-sabbiadoro-alloggio-frontemare-con-piscina-piano-al-5-piano/ or do I have a caching problem? Maybe a translation is cached and I cant easily go from one language to another?

    Thread Starter sacconi

    (@sacconi)

    I found another solution, I created a new taxonomy “intro” and I’ll get the sentences from there. For this taxonomy I already have a field in german, with my translation, the name of the field is “intro_de”. My problem is how to print in my shortcode the term of this new taxonomy. I tryed things such as $terms2 = get_object_term_cache( $post->ID, 'intro' );

    or $terms2 = [ get_the_terms( $post->ID, 'intro') [0]->term_id ] ;

    the purpose is getting the taxonomy term and then I put $terms2 in the final concatenation

    The filter for german (but I could do it for all the languages with $lang) will be

    add_filter('get_the_archive_title', function( $title_archive2) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
    
       $title_archive_de = get_term_meta( get_queried_object()->term_id, 'intro_de', true );
    
         if ( ! empty( $title_archive_de )) {
            return $title_archive_de;
         } else {
            return $title_archive2;
         }
       } else {
         return $title_archive2;
       }
    }, 10, 2 );
    

    I suppose I have to change “get_the_archive_title” with something more appropriate, but what? 🙂

    • This reply was modified 2 years, 2 months ago by sacconi.
    • This reply was modified 2 years, 2 months ago by sacconi.
    • This reply was modified 2 years, 2 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    The German persists in that page you linked to. There’s an issue with the code responsible for that phrase.

    It’s probably not a good idea to get terms directly from cache unless you have fallback code to get the data from the DB should the cache not have the term. WP code already does this for us where feasible, so there’s little reason to directly access WP object cache.

    I don’t know what filter to suggest without more information. What code is responsible for outputting the term you want translated?

    Thread Starter sacconi

    (@sacconi)

    I dont want to get terms from cache. I’m trying to get the term name with some code

    $terms2 = get_the_terms($post->ID, 'intro', true);

    ‘intro’ is the taxonomy name. I have to get the selected term for each post

    Moderator bcworkz

    (@bcworkz)

    OK, but get_the_terms() does not accept a third parameter. Attempting to pass a third parameter results in an error. An array of term objects will be returned, even if there’s only 1 term assigned. You can get the first term’s name with something like:
    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;

    However, attempting to get a post’s term in filter ‘get_the_archive_title’ is illogical because an archive consists of multiple posts, there’s no single post to get terms for. How to get the right post ID depends on the specific filter used. Again:

    I don’t know what filter to suggest without more information. What code is responsible for outputting the term you want translated?

    Thread Starter sacconi

    (@sacconi)

    The code is working! Before filtering I have to solve another problem. I’d like to see all the terms of the taxonomy below the taxonomy terms box, if I cant do it maybe it’s not a good solution. Or maybe I can have a selector in my post edit page selecting all the taxonomy terms?

    • This reply was modified 2 years, 2 months ago by sacconi.
    Thread Starter sacconi

    (@sacconi)

    If the above problem is solutioned I think I have found the element for the filter.

    while ( have_posts() ) :
    			the_post();
    
    			get_template_part( 'template-parts/content', get_post_type() );

    the shortcode is inside the content

    Moderator bcworkz

    (@bcworkz)

    If this is all for shortcode content, there may not be a need for a filter. Instead appropriate HTML will be concatenated to what ever the final return value is in the shortcode handler function.

    I’d like to see all the terms of the taxonomy below the taxonomy terms box

    That’s probably possible, but which taxonomy terms box are you referring to? The taxonomy sidebar box in the post editor? If so, maybe not below the box, but within it under everything else. If you make the taxonomy hierarchical, it’ll list all the terms (up to a certain number) by default. You don’t have to make use of the hierarchy.

    maybe I can have a selector in my post edit page selecting all the taxonomy terms

    If for post editor taxonomy sidebar box, you can probably add a button or check box that says something like “Select all terms”. It’d likely need to be done with JavaScript.

Viewing 15 replies - 1 through 15 (of 39 total)

The topic ‘Problems with gettext in custom fields’ is closed to new replies.