Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Camwyn/65dee90024815a6943cf9f32e46e9682 to your computer and use it in GitHub Desktop.
Save Camwyn/65dee90024815a6943cf9f32e46e9682 to your computer and use it in GitHub Desktop.
This is an example of a functions.php file with custom text added. It tries to be comprehensive and address all translation methods (contextual and singular/plural)
<?php
/*
* EXAMPLE OF CHANGING ANY TEXT (STRING) IN THE EVENTS CALENDAR
* See the codex to learn more about WP text domains:
* http://codex.wordpress.org/Translating_WordPress#Localization_Technology
* Example Tribe domains: 'tribe-events-calendar', 'tribe-events-calendar-pro'...
*/
function tribe_custom_theme_text ( $translation, $text, $domain ) {
// If this text domain doesn't start with "tribe-", "the-events-", or "event-" bail.
if( (strpos($domain, 'tribe-') !== 0 || strpos($domain, 'the-events-') !== 0 || strpos($domain, 'event-') !== 0) ) {
return $translation;
}
// Put your custom text here in a key => value pair
// Example: 'Text you want to change' => 'This is what it will be changed to'
// The text you want to change is the key, and it is case-sensitive
// The text you want to change it to is the value
// You can freely add or remove key => values, but make sure to separate them with a comma
// This example changes the label "Venue" to "Location", and "Related Events" to "Similar Events"
$custom_text = array(
'Venue' => 'Location',
'Related %s' => 'Similar %s',
);
// If we don't have replacement text in our array, return the original (translated) text.
if ( empty( $custom_text[$translation] ) ) {
return $translation;
}
return $custom_text[$translation];
}
function tribe_custom_theme_text_plurals ( $translation, $single, $plural, $number, $domain ) {
// If this text domain doesn't start with "tribe-", "the-events-", or "event-" bail.
if( (strpos($domain, 'tribe-') !== 0 || strpos($domain, 'the-events-') !== 0 || strpos($domain, 'event-') !== 0) ) {
return $translation;
}
/** If you want to use the number in your logic, this is where you'd do it.
* Make sure you return as part of this, so you don't call the function at the end and undo your changes!
*/
// If we're not doing any logic up above, just make sure your desired changes are in the $custom_text array above (in the `tribe_custom_theme_text` filter. )
if ( 1 === $number ) {
return tribe_custom_theme_text ( $translation, $single, $domain );
} else {
return tribe_custom_theme_text ( $translation, $plural, $domain );
}
}
function tribe_custom_theme_text_with_context ( $translation, $text, $context, $domain ) {
// If this text domain doesn't start with "tribe-", "the-events-", or "event-" bail.
if( (strpos($domain, 'tribe-') !== 0 || strpos($domain, 'the-events-') !== 0 || strpos($domain, 'event-') !== 0) ) {
return $translation;
}
/** If you want to use the context in your logic, this is where you'd do it.
* Make sure you return as part of this, so you don't call the function at the end and undo your changes!
* Example (here, we don't want to do anything when the context is "edit", but if it's "view" we want to change it to "Tribe"):
* if ( 'edit' === strtolower( $context ) ) {
* return $translation;
* } elseif( 'view' === strtolower( $context ) ) {
* return "Tribe";
* }
*
* Feel free to use the same logic we use in tribe_custom_theme_text() above for key => value pairs for this logic.
*/
// If we're not doing any logic up above, just make sure your desired changes are in the $custom_text array above (in the `tribe_custom_theme_text` filter. )
return tribe_custom_theme_text ( $translation, $text, $domain );
}
function tribe_custom_theme_text_plurals_with_context ( $translation, $single, $plural, $number, $context, $domain ) {
// If this text domain doesn't start with "tribe-", "the-events-", or "event-" bail.
if( (strpos($domain, 'tribe-') !== 0 || strpos($domain, 'the-events-') !== 0 || strpos($domain, 'event-') !== 0) ) {
return $translation;
}
/** If you want to use the context in your logic, this is where you'd do it.
* Make sure you return as part of this, so you don't call the function at the end and undo your changes!
* Example (here, we don't want to do anything when the context is "edit", but if it's "view" we want to change it to "Tribe"):
* if ( 'edit' === strtolower( $context ) ) {
* return $translation;
* } elseif( 'view' === strtolower( $context ) ) {
* return "cat";
* }
*
* You'd do something as well here for singular/plural. This could get complicated quickly if it has to interact with context as well.
* Example:
* if ( 1 === $number ) {
* return "cat";
* } else {
* return "cats";
* }
* Feel free to use the same logic we use in tribe_custom_theme_text() above for key => value pairs for this logic.
*/
// If we're not doing any logic up above, just make sure your desired changes are in the $custom_text array above (in the `tribe_custom_theme_text` filter. )
if ( 1 === $number ) {
return tribe_custom_theme_text ( $translation, $single, $domain );
} else {
return tribe_custom_theme_text ( $translation, $plural, $domain );
}
}
// base
add_filter('gettext', 'tribe_custom_theme_text', 20, 3);
// Plural-aware translations
add_filter('ngettext', 'tribe_custom_theme_text_plurals', 20, 5);
// translations with context
add_filter('gettext_with_context', 'tribe_custom_theme_text_with_context', 20, 4);
// Plural-aware translations with context
add_filter('ngettext_with_context', 'tribe_custom_theme_text_plurals_with_context', 20, 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment