Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak: Replace translate() with gettext filters in dynamic translation function #1028

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions wpo-ips-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,34 +1051,55 @@ function wpo_wcpdf_escape_url_path_or_base64( string $url_path_or_base64 ): stri
* @return string
*/
function wpo_wcpdf_dynamic_translate( string $string, string $textdomain ): string {
$log_enabled = isset( WPO_WCPDF()->settings->debug_settings['log_missing_translations'] );
$log_message = "Missing translation for: {$string} in textdomain: {$textdomain}";
static $cache = array();
static $logged = array();
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved

$cache_key = md5( $textdomain . '::' . $string );
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
$log_enabled = ! empty( WPO_WCPDF()->settings->debug_settings['log_missing_translations'] );
$multilingual_class = '\WPO\WC\PDF_Invoices_Pro\Multilingual_Full';
$translation = '';

if ( empty( $string ) ) {
if ( $log_enabled ) {
wcpdf_log_error( $log_message, 'warning' );
// Return early if empty string
if ( '' === $string ) {
if ( $log_enabled && ! isset( $logged[ $cache_key ] ) ) {
wcpdf_log_error( "Skipping translation for empty string in textdomain: {$textdomain}", 'warning' );
$logged[ $cache_key ] = true;
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
}
return $string;
}

// Check for multilingual support class
// Check cache
if ( isset( $cache[ $cache_key ] ) ) {
return $cache[ $cache_key ];
}

// Attempt to get a translation from multilingual class
if ( class_exists( $multilingual_class ) && method_exists( $multilingual_class, 'maybe_get_string_translation' ) ) {
$translation = $multilingual_class::maybe_get_string_translation( $string, $textdomain );
}

// If multilingual didn't change the string, fall back to native translate()
if ( ( empty( $translation ) || $translation === $string ) && function_exists( 'translate' ) ) {
$translation = translate( $string, $textdomain );
// If not translated yet, allow custom filter & then fallback to WP filters
if ( empty( $translation ) || $translation === $string ) {
$filtered = apply_filters( 'wpo_wcpdf_gettext', $string, $textdomain );

if ( ! empty( $filtered ) && $filtered !== $string ) {
$translation = $filtered;
} else {
// standard WP gettext filters
$translation = apply_filters( 'gettext', $string, $string, $textdomain );
$translation = apply_filters( "gettext_{$textdomain}", $translation, $string, $textdomain );
}
}

// Log missing translations for debugging if it's still untranslated
if ( $translation === $string && $log_enabled ) {
wcpdf_log_error( $log_message, 'warning' );
// Log a warning if no translation is found and debug logging is enabled
if ( $translation === $string && $log_enabled && ! isset( $logged[ $cache_key ] ) ) {
wcpdf_log_error( "Missing translation for: {$string} in textdomain: {$textdomain}", 'warning' );
$logged[ $cache_key ] = true;
}

return $translation ?: $string;
// Store in cache and return
$cache[ $cache_key ] = $translation ?: $string;
return $cache[ $cache_key ];
}

/**
Expand Down
Loading