Created
April 29, 2019 12:22
-
-
Save seebz/f3b970c6da59e9b425c60d529aa98c8d to your computer and use it in GitHub Desktop.
Fix theme translations
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 | |
/* | |
Plugin Name: Fix theme translations | |
Description: Try to fix missing theme translations. | |
Author: Seebz | |
Author URI: http://seebz.net/ | |
Version: 1.0 | |
*/ | |
function load_textdomain_mofile_fix( $mofile ) { | |
// Cached value of `/path/to/wordpress/wp-content/themes` | |
static $theme_root = null; | |
if ( is_null( $theme_root ) ) { | |
$theme_root = get_theme_root(); | |
} | |
// Valid (existing) `$mofile` | |
if ( file_exists($mofile) ) { | |
return $mofile; | |
} | |
// Not-a-theme `$mofile` | |
if ( strpos( $mofile, $theme_root ) !== 0 ) { | |
return $mofile; | |
} | |
// Maybe the new (mo)file path | |
$mofile = ftt_locate_mofile( $mofile ); | |
return $mofile; | |
} | |
add_filter( 'load_textdomain_mofile', 'load_textdomain_mofile_fix' ); | |
/** | |
* Lookup for an alternate mofile. | |
* | |
* @param string $mofile Original (missing) (mo)file path | |
* @return string Path of the new (found) (mo)file or original path if none found. | |
*/ | |
function ftt_locate_mofile( $mofile ) { | |
$basename = basename( $mofile ); // fr_FR.mo | |
$locale = basename( $mofile, '.mo' ); // fr_FR | |
// Possible paths | |
$t = strtolower( $locale ); // fr_fr | |
$paths[] = str_replace( $basename, "{$t}.mo", $mofile ); | |
$t = str_replace( '_', '-', $locale ); // fr-FR | |
$paths[] = str_replace( $basename, "{$t}.mo", $mofile ); | |
$t = strtolower( $t ); // fr-fr | |
$paths[] = str_replace( $basename, "{$t}.mo", $mofile ); | |
$t = preg_replace( '`[-_].+$`' , '', $locale ); // fr | |
$paths[] = str_replace( $basename, "{$t}.mo", $mofile ); | |
// Loop over each path | |
foreach( $paths as $path ) { | |
if ( file_exists( $path ) ) { | |
return $path; | |
} | |
} | |
return $mofile; // Fallback to default | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment