From 48fdf581a4cfaf1053bdd864cbdd966683fc67e1 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 23 Jan 2016 15:37:11 +0100 Subject: [PATCH] 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()`. --- class-tgm-plugin-activation.php | 93 +++++++++++++++++++++++++++++++++ phpcs.xml | 2 +- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/class-tgm-plugin-activation.php b/class-tgm-plugin-activation.php index 5ef030e5..5211858b 100755 --- a/class-tgm-plugin-activation.php +++ b/class-tgm-plugin-activation.php @@ -260,6 +260,15 @@ public function __construct() { // Announce that the class is ready, and pass the object (for advanced use). do_action_ref_array( 'tgmpa_init', array( $this ) ); + /* + * Load our text domain and allow for overloading the fall-back file. + * + * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA + * generator on the website.}} + */ + add_action( 'init', array( $this, 'load_textdomain' ), 5 ); + add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 ); + // When the rest of WP has loaded, kick-start the rest of the class. add_action( 'init', array( $this, 'init' ) ); } @@ -430,6 +439,90 @@ public function init() { } } + /** + * Load translations. + * + * @since 2.x.x + * + * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to + * get round the different ways of handling the path and deprecated notices being thrown + * and such. For plugins, the actual file name will be corrected by a filter.}} + * + * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA + * generator on the website.}} + */ + public function load_textdomain() { + if ( is_textdomain_loaded( 'tgmpa' ) ) { + return; + } + + if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) { + // Plugin, we'll need to adjust the file name. + add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 ); + load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); + remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 ); + } else { + load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); + } + } + + /** + * Correct the .mo file name for (must-use) plugins. + * + * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. + * + * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA + * generator on the website.}} + * + * @since 2.x.x + * + * @param string $mofile Full path to the target mofile. + * @param string $domain The domain for which a language file is being loaded. + * @return string $mofile + */ + public function correct_plugin_mofile( $mofile, $domain ) { + // Exit early if not our domain (just in case). + if ( 'tgmpa' !== $domain ) { + return $mofile; + } + return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile ); + } + + /** + * Potentially overload the fall-back translation file for the current language. + * + * WP, by default since WP 3.7, will load a local translation first and if none + * can be found, will try and find a translation in the /wp-content/languages/ directory. + * As this library is theme/plugin agnostic, translation files for TGMPA can exist both + * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. + * + * This method makes sure both directories are checked. + * + * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA + * generator on the website.}} + * + * @since 2.x.x + * + * @param string $mofile Full path to the target mofile. + * @param string $domain The domain for which a language file is being loaded. + * @return string $mofile + */ + public function overload_textdomain_mofile( $mofile, $domain ) { + // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable. + if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) { + return $mofile; + } + + // Current fallback file is not valid, let's try the alternative option. + if ( false !== strpos( $mofile, '/themes/' ) ) { + return str_replace( '/themes/', '/plugins/', $mofile ); + } elseif ( false !== strpos( $mofile, '/plugins/' ) ) { + return str_replace( '/plugins/', '/themes/', $mofile ); + } else { + return $mofile; + } + } + /** * Hook in plugin action link filters for the WP native plugins page. * diff --git a/phpcs.xml b/phpcs.xml index 323edd38..7d22654d 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,7 +4,7 @@ class-tgm-plugin-activation.php example.php - +