Skip to content

Instantly share code, notes, and snippets.

@luiskabes-arch
Created June 4, 2025 19:40
Show Gist options
  • Save luiskabes-arch/935c815e32061475757b06744c0f878f to your computer and use it in GitHub Desktop.
Save luiskabes-arch/935c815e32061475757b06744c0f878f to your computer and use it in GitHub Desktop.
Render text based on the current language, using the primary and secondary languages.
/**
* Render text based on the current language, using the primary and secondary languages.
* Supports placeholders like %s.
* @param string $primary_language The primary language text to be rendered.
* @param string $secondary_language The secondary language text to be rendered.
* @param mixed $args Additional arguments to replace placeholders in the text.
*/
function localized_text($primary_language, $secondary_language, ...$args) {
// Get the current language
$current_language = apply_filters('wpml_current_language', NULL);
// Get the default (primary) language dynamically
$default_language = apply_filters('wpml_default_language', NULL);
// Determine the language text to use
$text = ($current_language === $default_language) ? $primary_language : $secondary_language;
// Replace placeholders in the text (e.g., %s) with the provided arguments
return vsprintf($text, $args); // vsprintf() allows for dynamic string replacement
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment