Skip to content

Commit 70da410

Browse files
committed
Add loading of TGMPA native translation files.
Implemented in a way that the language loading will work independently of whether TGMPA is included in a plugin or a theme. Including compatibility for looking in both the WP_LANG_DIR/plugins/ as well as the WP_LANG_DIR/themes/ directory for translations served by Wp.org. Ignore warnings from PHPCS as we *really* should suppress warning on `is_readable()`.
1 parent 5dbb017 commit 70da410

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

class-tgm-plugin-activation.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ public function __construct() {
260260
// Announce that the class is ready, and pass the object (for advanced use).
261261
do_action_ref_array( 'tgmpa_init', array( $this ) );
262262

263+
/*
264+
* Load our text domain and allow for overloading the fall-back file.
265+
*
266+
* {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
267+
* generator on the website.}}
268+
*/
269+
add_action( 'init', array( $this, 'load_textdomain' ), 5 );
270+
add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
271+
263272
// When the rest of WP has loaded, kick-start the rest of the class.
264273
add_action( 'init', array( $this, 'init' ) );
265274
}
@@ -430,6 +439,90 @@ public function init() {
430439
}
431440
}
432441

442+
/**
443+
* Load translations.
444+
*
445+
* @since 2.x.x
446+
*
447+
* @internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
448+
* get round the different ways of handling the path and deprecated notices being thrown
449+
* and such. For plugins, the actual file name will be corrected by a filter.
450+
*
451+
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
452+
* generator on the website.}}
453+
*/
454+
public function load_textdomain() {
455+
if ( is_textdomain_loaded( 'tgmpa' ) ) {
456+
return;
457+
}
458+
459+
if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
460+
// Plugin, we'll need to adjust the file name.
461+
add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
462+
load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
463+
remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
464+
} else {
465+
load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
466+
}
467+
}
468+
469+
/**
470+
* Correct the .mo file name for (must-use) plugins.
471+
*
472+
* Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
473+
*
474+
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
475+
* generator on the website.}}
476+
*
477+
* @since 2.x.x
478+
*
479+
* @param string $mofile Full path to the target mofile.
480+
* @param string $domain The domain for which a language file is being loaded.
481+
* @return string $mofile
482+
*/
483+
public function correct_plugin_mofile( $mofile, $domain ) {
484+
// Exit early if not our domain (just in case).
485+
if ( 'tgmpa' !== $domain ) {
486+
return $mofile;
487+
}
488+
return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
489+
}
490+
491+
/**
492+
* Potentially overload the fall-back translation file for the current language.
493+
*
494+
* WP, by default since WP 3.7, will load a local translation first and if none
495+
* can be found, will try and find a translation in the /wp-content/languages/ directory.
496+
* As this library is theme/plugin agnostic, translation files for TGMPA can exist both
497+
* in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
498+
*
499+
* This method makes sure both directories are checked.
500+
*
501+
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
502+
* generator on the website.}}
503+
*
504+
* @since 2.x.x
505+
*
506+
* @param string $mofile Full path to the target mofile.
507+
* @param string $domain The domain for which a language file is being loaded.
508+
* @return string $mofile
509+
*/
510+
public function overload_textdomain_mofile( $mofile, $domain ) {
511+
// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
512+
if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
513+
return $mofile;
514+
}
515+
516+
// Current fallback file is not valid, let's try the alternative option.
517+
if ( strpos( $mofile, '/themes/' ) !== false ) {
518+
return str_replace( '/themes/', '/plugins/', $mofile );
519+
} elseif ( strpos( $mofile, '/plugins/' ) !== false ) {
520+
return str_replace( '/plugins/', '/themes/', $mofile );
521+
} else {
522+
return $mofile;
523+
}
524+
}
525+
433526
/**
434527
* Hook in plugin action link filters for the WP native plugins page.
435528
*

phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<file>class-tgm-plugin-activation.php</file>
55
<file>example.php</file>
66
<arg name="report" value="full"/>
7-
<arg value="sp"/>
7+
<arg value="spn"/>
88
<rule ref="WordPress">
99
<exclude name="WordPress.VIP" />
1010
</rule>

0 commit comments

Comments
 (0)