Filter uncanny-automator

automator_localized_string

Filters localized strings used throughout the plugin, allowing modification of translations based on the string key.

add_filter( 'automator_localized_string', $callback, 10, 2 );

Description

Fires after a translatable string has been retrieved and before it's returned. Developers can filter this hook to dynamically modify any localized string based on the provided string key. This allows for custom translations or on-the-fly string adjustments.


Usage

add_filter( 'automator_localized_string', 'your_function_name', 10, 2 );

Parameters

$localized_string (mixed)
The `$localized_string` parameter contains the actual translated string that will be output or used.
$string_key (mixed)
This parameter contains the localized string value that will be returned for the given string key.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_localized_string' filter hook.
 * This function intercepts a localized string based on its key and potentially
 * modifies it or provides a custom translation.
 *
 * @param mixed  $localized_string The original localized string to be filtered.
 * @param mixed  $string_key       The key associated with the localized string.
 * @return mixed The potentially modified or custom localized string.
 */
add_filter( 'automator_localized_string', function( $localized_string, $string_key ) {

	// Example: If the string key is 'automator_welcome_message',
	// we want to append a custom greeting.
	if ( 'automator_welcome_message' === $string_key ) {
		// Ensure the original string is a string before concatenation.
		if ( is_string( $localized_string ) ) {
			return $localized_string . ' - Welcome aboard!';
		} else {
			// If it's not a string, return the default or handle as needed.
			return 'Welcome aboard!';
		}
	}

	// Example: For a specific trigger label, provide a more descriptive alternative.
	if ( 'automator_trigger_user_registered' === $string_key ) {
		return 'A new user has signed up for the site.';
	}

	// If the string key doesn't match our specific conditions,
	// return the original localized string unchanged.
	return $localized_string;

}, 10, 2 ); // 10 is the priority, 2 is the number of arguments accepted by the callback function.

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/utilities/class-automator-translations.php:86

public function get( $string_key = null ) {

		if ( isset( $error_messages[ $string_key ] ) ) {
			$localized_string = $this->ls[ $string_key ];
		} else {
			return null;
		}

		/**
		 * Filters the specific string
		 */
		$localized_string = apply_filters_deprecated(
			'uap_localized_string',
			array(
				$localized_string,
				$string_key,
			),
			'3.0',
			'automator_localized_string'
		);

		return apply_filters( 'automator_localized_string', $localized_string, $string_key );
	}


Scroll to Top