Created
June 1, 2021 13:46
-
-
Save davidwebca/649c2ab8080cf42a940e1aa25921757e to your computer and use it in GitHub Desktop.
Translate regular gettext and underscore functions with Polylang's strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('gettext', function($translation, $text, $domain) { | |
if(is_admin()) { | |
return $translation; | |
} | |
if($domain == 'mythemedomain') { | |
if(function_exists('icl_register_string')){ | |
$slug = sanitize_title($text); | |
icl_register_string($domain, $slug, $text); | |
$newtranslation = icl_t($domain, $slug, $text); | |
if($newtranslation != $text) { | |
$translation = $newtranslation; | |
} | |
} | |
} | |
return $translation; | |
}, 10, 3); | |
add_filter('gettext_with_context', function($translation, $text, $context, $domain) { | |
if(is_admin()) { | |
return $translation; | |
} | |
if($domain == 'mythemedomain') { | |
if(function_exists('icl_register_string')){ | |
$slug = sanitize_title($text.'_'.$context); | |
icl_register_string($domain, $slug, $text); | |
$newtranslation = icl_t($domain, $slug, $text); | |
if($newtranslation != $text) { | |
$translation = $newtranslation; | |
} | |
} | |
} | |
return $translation; | |
}, 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment