Skip to content

Instantly share code, notes, and snippets.

@timotheemoulin
Last active November 24, 2020 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timotheemoulin/07b085f7d8c9f6fec53a60968b55978a to your computer and use it in GitHub Desktop.
Save timotheemoulin/07b085f7d8c9f6fec53a60968b55978a to your computer and use it in GitHub Desktop.
Nasty way to override the core WordPress translations (PHP, gettext, JS, React)
<?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' ],
]
);
<?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
);
@timotheemoulin
Copy link
Author

timotheemoulin commented May 8, 2020

This is only a proof of concept that doesn't address the following issues (at least).

  • context of the translation is not taken into account (so every "Original content" string will have the same translation)
  • handle for the JS integration is also not verified, so there is the same problem as with the "context"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment