Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Created June 1, 2021 13:46
Show Gist options
  • Save davidwebca/649c2ab8080cf42a940e1aa25921757e to your computer and use it in GitHub Desktop.
Save davidwebca/649c2ab8080cf42a940e1aa25921757e to your computer and use it in GitHub Desktop.
Translate regular gettext and underscore functions with Polylang's strings
<?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