Help Desk

Additional string changing functions

  • Posts: 7 Topics: 3
    | Permalink

    Hello there,

    i just wanted to point out, that the gist on the bottom of the“Change the wording of any bit of text or string” post is somewhat limited in its usability, as it does not include the respective solutions for the ngettext and gettext_with_context filter hooks.
    As i found it very handy for the average user to change specific terms and spellings without the need to fiddle around with translation files and the follow-up work of keeping them up to date on the one hand and the fact that one can keep his modifications in one place ( e.g. themes functions.php ) on the other hand, i post below some additional functions similar to the ones provided by the TEC team for the above mentioned filter hooks.
    They do work for my case, but if maybe someone (e.g. the TEC team) wants to improve them, feel free to do so.
    Maybe its worth to put them also on the respective knowledgebase post.

    Regards
    Niels
    https://gist.github.com/destruktomatik/30717613835c2136a4075076e507e606

    <?php
    /* Additional functions for changing strings in the events calendar and its plugins
     * See https://theeventscalendar.com/knowledgebase/change-the-wording-of-any-bit-of-text-or-string/ for details 
     * on the provided functions by the TEC team
     * 1. - function for changing quantity based spelling
     * 2. - function for changing strings with an attached context
     */
    
    function tribe_custom_theme_numbered_text ( $translation, $single, $plural, $number, $domain ) {
    	//put here desired customizaion of single and plural strings 
    	$custom_single = array(
    		'%d RSVP' => '%d Reservierung'
    	);
    	$custom_plural = array(
    		'%d RSVPs' => '%d Reservierungen'
    	);
    	// If this text domain starts with "tribe-", "the-events-", or "event-" and we have replacement text
        	if( (strpos($domain, 'tribe-') === 0 || strpos($domain, 'the-events-') === 0 || strpos($domain, 'event-') === 0) ) {
    		// If found in the custom text array, replace it
    			if( array_key_exists($translation, $custom_single) ) {
    				$translation = $custom_single[$translation];
    			}
    			if( 1 <  $number && array_key_exists($translation, $custom_plural) ) {
    				$translation = $custom_plural[$translation];
    			}
    	}
        return $translation;
    }
    add_filter('ngettext', 'tribe_custom_theme_numbered_text', 20, 5);
    
    function tribe_custom_theme_text_with_context ( $translation, $text, $context, $domain ) {
    	// example: 'context1'  => [ old_translation1 => new_translation1, old_translation2 => new_translation2  ],
    	$custom_text_with_context = [
    		'form heading'	=>	[
    						'RSVP' => 'Reservierung'
    					],					
    	];
    	
    	if ( (strpos($domain, 'tribe-') === 0 || strpos($domain, 'the-events-') === 0 || strpos($domain, 'event-') === 0) ){
    		if ( array_key_exists($context, $custom_text_with_context) && array_key_exists($translation, $custom_text_with_context[$context]) ) {
    				$translation = $custom_text_with_context[$context][$translation];
    		}
    	}
        return $translation;
    }
    add_filter('gettext_with_context', 'tribe_custom_theme_text_with_context', 20, 4);
    ?>
    • This topic was modified 2 years, 12 months ago by  Niels.
    Posts: 10005 Topics: 1
    | Permalink

    Good evening Niels and welcome back!

    Wow, this is totally awesome AND generous.

    Your timing could not be better. We are currently working hard at improving these aspects.
    I have forwarded this to the appropriate people.

    Rest assured that if we do build on this, you will be credited appropriately.

    You are welcome back in our support forums any time 🙂

    For now, I am going to close this thread.

    Have a great weekend!

    Geoff B.