Last active
November 24, 2020 18:10
-
-
Save timotheemoulin/07b085f7d8c9f6fec53a60968b55978a to your computer and use it in GitHub Desktop.
Nasty way to override the core WordPress translations (PHP, gettext, JS, React)
This file contains hidden or 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 | |
// Load the translations and store them in the session | |
/** | |
* Define the string messages to override | |
*/ | |
if ( ! isset( $_SESSION['translation-override']['fr_FR'] ) ) { | |
$_SESSION['translation-override']['fr_FR'] = []; | |
} | |
$_SESSION['translation-override']['fr_FR'] = array_merge( | |
$_SESSION['translation-override']['fr_FR'], | |
[ | |
// original translation => [singular translation, plural translation] | |
'Display featured image' => [ 'Afficher l’image d’accroche' ], | |
] | |
); |
This file contains hidden or 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 | |
// Load the translations and store them in the session | |
// @todo change this to the real file name | |
require_once get_stylesheet_directory() . '/languages/override/fr_FR.php'; | |
/** | |
* Use the gettext filters to override the translations. | |
*/ | |
add_filter( | |
'gettext', | |
/** | |
* @param string $translation current translation | |
* @param string $text original translation | |
* @param string $domain translation domain name | |
* | |
* @return string | |
*/ | |
function ( $translation, $text, $domain ): string { | |
global $locale; | |
return $_SESSION['translation-override'][ $locale ][ $text ][0] ?? $translation; | |
}, | |
10, | |
3 | |
); | |
/** | |
* Use the gettext filters to override the contextual translations. | |
*/ | |
add_filter( | |
'gettext_with_context', | |
/** | |
* @param string $translation current translation | |
* @param string $text original translation | |
* @param string $context translation context | |
* @param string $domain translation domain name | |
* | |
* @return string | |
*/ | |
function ( $translation, $text, $context, $domain ): string { | |
global $locale; | |
return $_SESSION['translation-override'][ $locale ][ $text ][0] ?? $translation; | |
}, | |
10, | |
4 | |
); | |
/** | |
* Use the gettext filters to override the plural translations. | |
*/ | |
add_filter( | |
'ngettext', | |
/** | |
* @param string $translation current translation | |
* @param string $single original singular translation | |
* @param string $plural original plural translation | |
* @param int $number plural number | |
* @param string $domain translation domain name | |
* | |
* @return string | |
*/ | |
function ( $translation, $single, $plural, $number, $domain ): string { | |
global $locale; | |
return $_SESSION['translation-override'][ $locale ][ $single ][1] ?? $translation; | |
}, | |
10, | |
5 | |
); | |
/** | |
* Use the gettext filters to override the plural contextual translations. | |
*/ | |
add_filter( | |
'ngettext_with_context', | |
/** | |
* @param string $translation current translation | |
* @param string $single original singular translation | |
* @param string $plural original plural translation | |
* @param int $number plural number | |
* @param string $context translation context | |
* @param string $domain translation domain name | |
* | |
* @return string | |
*/ | |
function ( $translation, $single, $plural, $number, $context, $domain ): string { | |
global $locale; | |
return $_SESSION['translation-override'][ $locale ][ $single ][1] ?? $translation; | |
}, | |
10, | |
6 | |
); | |
/** | |
* Use the load_script_translations filters to override the translations loaded with JS. | |
*/ | |
add_filter( | |
'load_script_translations', | |
/** | |
* @param string $translations JSON representation of the translation file | |
* @param string $file translation file path | |
* @param string $handle script handle | |
* @param string $domain translation domain | |
* | |
* @return string | |
*/ | |
function ( $translations, $file, $handle, $domain ): string { | |
global $locale; | |
$decoded = json_decode( $translations ); | |
if ( $decoded->locale_data->messages->{''}->lang == 'fr' ) { | |
$decoded->locale_data->messages = (object) array_merge( | |
(array) $decoded->locale_data->messages, | |
$_SESSION['translation-override'][ $locale ] ?? [], | |
); | |
$translations = json_encode( $decoded ); | |
} | |
return $translations; | |
}, | |
10, | |
4 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is only a proof of concept that doesn't address the following issues (at least).