Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Created June 1, 2021 13:46
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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